|
1 | 1 | package by.jprof.telegram.bot.runners.lambda |
2 | 2 |
|
| 3 | +import by.jprof.telegram.bot.core.UpdateProcessingPipeline |
| 4 | +import by.jprof.telegram.bot.runners.lambda.config.envModule |
| 5 | +import by.jprof.telegram.bot.runners.lambda.config.pipelineModule |
3 | 6 | import com.amazonaws.services.lambda.runtime.Context |
4 | 7 | import com.amazonaws.services.lambda.runtime.RequestHandler |
5 | 8 | import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent |
6 | 9 | import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse |
| 10 | +import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy |
| 11 | +import kotlinx.serialization.json.Json |
| 12 | +import org.apache.logging.log4j.LogManager |
| 13 | +import org.koin.core.component.KoinComponent |
| 14 | +import org.koin.core.component.inject |
| 15 | +import org.koin.core.context.startKoin |
7 | 16 |
|
8 | 17 | @Suppress("unused") |
9 | | -class JProf : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> { |
10 | | - override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse { |
11 | | - return APIGatewayV2HTTPResponse |
| 18 | +class JProf : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse>, KoinComponent { |
| 19 | + companion object { |
| 20 | + private val logger = LogManager.getLogger(JProf::class.java) |
| 21 | + private val OK = APIGatewayV2HTTPResponse |
12 | 22 | .builder() |
13 | 23 | .withStatusCode(200) |
14 | | - .withBody("Hello, world!") |
| 24 | + .withHeaders(mapOf( |
| 25 | + "Content-Type" to "application/json" |
| 26 | + )) |
| 27 | + .withBody("{}") |
15 | 28 | .build() |
16 | 29 | } |
| 30 | + |
| 31 | + init { |
| 32 | + startKoin { |
| 33 | + modules(envModule, pipelineModule) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private val json: Json by inject() |
| 38 | + private val pipeline: UpdateProcessingPipeline by inject() |
| 39 | + |
| 40 | + override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse { |
| 41 | + logger.debug("Incoming request: {}", input) |
| 42 | + |
| 43 | + val update = json.decodeFromString(UpdateDeserializationStrategy, input.body ?: return OK) |
| 44 | + |
| 45 | + logger.debug("Parsed update: {}", update) |
| 46 | + |
| 47 | + pipeline.process(update) |
| 48 | + |
| 49 | + return OK |
| 50 | + } |
17 | 51 | } |
0 commit comments