|
| 1 | +package by.jprof.telegram.bot.eval.evaluator |
| 2 | + |
| 3 | +import by.jprof.telegram.bot.eval.dto.EvalEvent |
| 4 | +import by.jprof.telegram.bot.eval.dto.EvalResponse |
| 5 | +import by.jprof.telegram.bot.eval.dto.Language |
| 6 | +import by.jprof.telegram.bot.eval.evaluator.config.jsonModule |
| 7 | +import com.amazonaws.services.lambda.runtime.Context |
| 8 | +import com.amazonaws.services.lambda.runtime.RequestStreamHandler |
| 9 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 10 | +import kotlinx.serialization.decodeFromString |
| 11 | +import kotlinx.serialization.json.Json |
| 12 | +import kotlinx.serialization.json.encodeToStream |
| 13 | +import org.apache.logging.log4j.LogManager |
| 14 | +import org.koin.core.component.KoinComponent |
| 15 | +import org.koin.core.component.inject |
| 16 | +import org.koin.core.context.startKoin |
| 17 | +import java.io.InputStream |
| 18 | +import java.io.OutputStream |
| 19 | + |
| 20 | +@ExperimentalSerializationApi |
| 21 | +@Suppress("unused") |
| 22 | +class Evaluator : RequestStreamHandler, KoinComponent { |
| 23 | + companion object { |
| 24 | + private val logger = LogManager.getLogger(Evaluator::class.java) |
| 25 | + } |
| 26 | + |
| 27 | + init { |
| 28 | + startKoin { |
| 29 | + modules( |
| 30 | + jsonModule |
| 31 | + ) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private val json: Json by inject() |
| 36 | + |
| 37 | + override fun handleRequest(input: InputStream, output: OutputStream, context: Context) { |
| 38 | + val payload = input.bufferedReader().use { it.readText() } |
| 39 | + |
| 40 | + logger.debug("Payload: {}", payload) |
| 41 | + |
| 42 | + val evalEvent = json.decodeFromString<EvalEvent>(payload) |
| 43 | + |
| 44 | + logger.debug("Parsed event: {}", evalEvent) |
| 45 | + |
| 46 | + val evalResponse = EvalResponse(Language.UNKNOWN) |
| 47 | + |
| 48 | + output.buffered().use { json.encodeToStream(evalResponse, it) } |
| 49 | + } |
| 50 | +} |
0 commit comments