1
1
package me.madhead.tyzenhaus.launcher.fly.modules
2
2
3
+ import io.ktor.serialization.kotlinx.json.json
3
4
import io.ktor.server.application.Application
4
5
import io.ktor.server.application.install
6
+ import io.ktor.server.auth.Authentication
7
+ import io.ktor.server.auth.bearer
5
8
import io.ktor.server.metrics.micrometer.MicrometerMetrics
6
9
import io.ktor.server.plugins.callloging.CallLogging
7
10
import io.ktor.server.plugins.compression.Compression
11
+ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
8
12
import io.ktor.server.plugins.defaultheaders.DefaultHeaders
9
13
import io.ktor.server.routing.routing
10
14
import io.micrometer.prometheus.PrometheusMeterRegistry
15
+ import java.time.Instant
16
+ import java.util.UUID
11
17
import me.madhead.tyzenhaus.launcher.fly.koin.configModule
12
18
import me.madhead.tyzenhaus.launcher.fly.koin.dbModule
13
19
import me.madhead.tyzenhaus.launcher.fly.koin.jsonModule
14
20
import me.madhead.tyzenhaus.launcher.fly.koin.metricsModule
15
21
import me.madhead.tyzenhaus.launcher.fly.koin.pipelineModule
22
+ import me.madhead.tyzenhaus.launcher.fly.koin.serviceModule
16
23
import me.madhead.tyzenhaus.launcher.fly.koin.telegramModule
17
24
import me.madhead.tyzenhaus.launcher.fly.routes.metrics
18
25
import me.madhead.tyzenhaus.launcher.fly.routes.miniApp
19
26
import me.madhead.tyzenhaus.launcher.fly.routes.miniAppAPI
20
27
import me.madhead.tyzenhaus.launcher.fly.routes.webhook
28
+ import me.madhead.tyzenhaus.launcher.fly.security.APITokenPrincipal
29
+ import me.madhead.tyzenhaus.repository.APITokenRepository
21
30
import org.koin.ktor.ext.get
31
+ import org.koin.ktor.ext.inject
22
32
import org.koin.ktor.plugin.Koin
23
33
34
+ /* *
35
+ * Initializes and configures the application.
36
+ */
24
37
fun Application.tyzenhaus () {
25
38
install(DefaultHeaders )
26
39
install(CallLogging )
@@ -29,18 +42,38 @@ fun Application.tyzenhaus() {
29
42
install(Koin ) {
30
43
modules(
31
44
configModule(environment.config),
45
+ dbModule,
46
+ serviceModule,
47
+ pipelineModule,
32
48
metricsModule,
33
49
telegramModule,
34
50
jsonModule,
35
- pipelineModule,
36
- dbModule,
37
51
)
38
52
}
39
53
54
+ install(ContentNegotiation ) {
55
+ json(this @tyzenhaus.get())
56
+ }
57
+
40
58
install(MicrometerMetrics ) {
41
59
this .registry = this @tyzenhaus.get<PrometheusMeterRegistry >()
42
60
}
43
61
62
+ install(Authentication ) {
63
+ bearer(" api" ) {
64
+ authenticate { credential ->
65
+ val token = try {
66
+ UUID .fromString(credential.token)
67
+ } catch (_: Exception ) {
68
+ return @authenticate null
69
+ }
70
+ val tokenRepository by inject<APITokenRepository >()
71
+
72
+ tokenRepository.get(token)?.takeIf { it.validUntil > Instant .now() }?.let { APITokenPrincipal (it.groupId, it.scope) }
73
+ }
74
+ }
75
+ }
76
+
44
77
routing {
45
78
webhook()
46
79
metrics()
0 commit comments