Skip to content

Commit

Permalink
Convention: Module is a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Dec 20, 2024
1 parent 2d63064 commit 7a27d0a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 81 deletions.
37 changes: 1 addition & 36 deletions komok-app/src/main/kotlin/io/heapy/komok/Application.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,8 @@
@file:JvmName("Application")
package io.heapy.komok

import io.heapy.komok.infra.http.server.ServerModule
import io.heapy.komok.infra.uptime.JvmUptime
import io.heapy.komok.tech.logging.Logger
import io.heapy.komok.infra.uptime.JvmUptimeModule
import io.heapy.komok.tech.di.lib.Module
import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.*

fun main() {
createApplicationModule {}
createKomokApplicationModule {}
.komokApplication
.run()
}

class KomokApplication(
private val server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>,
private val jvmUptime: JvmUptime,
) {
fun run() {
val uptime = jvmUptime.uptime()
log.info("Application started. JVM running for $uptime")
server.start(wait = true)
}

private companion object : Logger()
}

@Module
open class ApplicationModule(
private val serverModule: ServerModule,

private val jvmUptimeModule: JvmUptimeModule,
) {
open val komokApplication by lazy {
KomokApplication(
server = serverModule.server,
jvmUptime = jvmUptimeModule.jvmUptime,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.heapy.komok.business.entrypoint

import io.heapy.komok.infra.uptime.JvmUptime
import io.heapy.komok.tech.logging.Logger
import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.EmbeddedServer

class KomokApplication(
private val server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>,
private val jvmUptime: JvmUptime,
) {
fun run() {
val uptime = jvmUptime.uptime()
log.info("Application started. JVM running for $uptime")
server.start(wait = true)
}

private companion object : Logger()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.heapy.komok.business.entrypoint

import io.heapy.komok.infra.http.server.ServerModule
import io.heapy.komok.infra.uptime.JvmUptimeModule
import io.heapy.komok.tech.di.lib.Module

@Module
open class KomokApplicationModule(
private val serverModule: ServerModule,
private val jvmUptimeModule: JvmUptimeModule,
) {
open val komokApplication by lazy {
KomokApplication(
server = serverModule.server,
jvmUptime = jvmUptimeModule.jvmUptime,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.heapy.komok.business.login

import io.heapy.komok.infra.jwt.JwtService
import io.heapy.komok.server.common.KomokRoute
import io.ktor.server.request.receive
import io.ktor.server.routing.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.heapy.komok.infra.http.server

import io.heapy.komok.business.login.JwtModule
import io.heapy.komok.infra.jwt.JwtModule
import io.heapy.komok.metrics.MetricsModule
import io.heapy.komok.server.common.KomokServerFeatures
import io.heapy.komok.tech.di.lib.Module
Expand Down
33 changes: 33 additions & 0 deletions komok-app/src/main/kotlin/io/heapy/komok/infra/jwt/JwtModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.heapy.komok.infra.jwt

import io.heapy.komok.business.login.JwtConfiguration
import io.heapy.komok.infra.time.TimeSourceModule
import io.heapy.komok.tech.config.ConfigurationModule
import io.heapy.komok.tech.di.lib.Module

@Module
open class JwtModule(
private val configurationModule: ConfigurationModule,
private val timeSourceModule: TimeSourceModule,
) {
open val jwtConfiguration: JwtConfiguration by lazy {
configurationModule
.config
.read(
deserializer = JwtConfiguration.serializer(),
path = "jwt",
)
.also { config ->
require(config.secret.length >= 128) {
"Secret must be at least 128 characters long"
}
}
}

open val jwtService: JwtService by lazy {
DefaultJwtService(
jwtConfiguration = jwtConfiguration,
timeSource = timeSourceModule.timeSource,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.heapy.komok.business.login
package io.heapy.komok.infra.jwt

import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.heapy.komok.infra.time.TimeSource
import io.heapy.komok.auth.common.User
import io.heapy.komok.infra.time.TimeSourceModule
import io.heapy.komok.tech.config.ConfigurationModule
import io.heapy.komok.tech.di.lib.Module
import io.heapy.komok.business.login.JwtConfiguration
import java.time.temporal.ChronoUnit

interface JwtService {
Expand All @@ -15,7 +13,7 @@ interface JwtService {
): String
}

private class DefaultJwtService(
internal class DefaultJwtService(
private val jwtConfiguration: JwtConfiguration,
private val timeSource: TimeSource,
) : JwtService {
Expand All @@ -42,29 +40,3 @@ private class DefaultJwtService(
}
}

@Module
open class JwtModule(
private val configurationModule: ConfigurationModule,
private val timeSourceModule: TimeSourceModule,
) {
open val jwtConfiguration: JwtConfiguration by lazy {
configurationModule
.config
.read(
deserializer = JwtConfiguration.serializer(),
path = "jwt",
)
.also { config ->
require(config.secret.length >= 128) {
"Secret must be at least 128 characters long"
}
}
}

open val jwtService: JwtService by lazy {
DefaultJwtService(
jwtConfiguration = jwtConfiguration,
timeSource = timeSourceModule.timeSource,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.heapy.komok.infra.uptime

import io.heapy.komok.tech.di.lib.Module
import java.lang.management.ManagementFactory
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
Expand All @@ -9,17 +8,10 @@ interface JvmUptime {
fun uptime(): Duration
}

private class DefaultJvmUptime : JvmUptime {
internal class DefaultJvmUptime : JvmUptime {
override fun uptime(): Duration {
return ManagementFactory.getRuntimeMXBean()
.uptime
.milliseconds
}
}

@Module
open class JvmUptimeModule {
open val jvmUptime: JvmUptime by lazy {
DefaultJvmUptime()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.heapy.komok.infra.uptime

import io.heapy.komok.tech.di.lib.Module

@Module
open class JvmUptimeModule {
open val jvmUptime: JvmUptime by lazy {
DefaultJvmUptime()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.heapy.komok.business.login
package io.heapy.komok.infra.jwt

import io.heapy.komok.KomokBaseTest
import io.heapy.komok.TestTimeSource
import io.heapy.komok.UnitTest
import io.heapy.komok.auth.common.User
import io.heapy.komok.business.login.JwtConfiguration
import io.heapy.komok.tech.config.buildMockKomokConfiguration
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.assertThrows
import java.time.Instant
import kotlin.time.Duration.Companion.minutes
Expand Down Expand Up @@ -48,7 +49,7 @@ class JwtServiceTest : KomokBaseTest {
User(id = "1"),
)

assertEquals(
Assertions.assertEquals(
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdWRpZW5jZSIsImlzcyI6Imlzc3VlciIsImlkIjoiMSIsImV4cCI6MTcyMDE1OTcyOH0.8cFIbUkrFEDTMEstyUKzMM_Knlictchm1ejqQOwZrAGgyOPuSVx5rJ-IZb7QI1aLBHBRrfVJP_oQ_6TU2_EYFw",
token,
)
Expand Down Expand Up @@ -81,7 +82,7 @@ class JwtServiceTest : KomokBaseTest {
module.jwtService
}

assertEquals(
Assertions.assertEquals(
"Secret must be at least 128 characters long",
exception.message,
)
Expand Down

0 comments on commit 7a27d0a

Please sign in to comment.