From 4adfc6467ca3ccc1a32d95ae3de43417629c02f9 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 16 Mar 2025 03:11:42 +0100 Subject: [PATCH] Split configs --- .../interfaceLayer/ReadTheGameState.kt | 2 +- .../kotlin/eventDemo/business/entity/Card.kt | 2 +- .../eventDemo/business/entity/GameId.kt | 2 +- .../eventDemo/business/entity/Player.kt | 4 +- .../notification/CommandErrorNotification.kt | 2 +- .../CommandSuccessNotification.kt | 2 +- .../notification/ItsTheTurnOfNotification.kt | 2 +- .../business/notification/Notification.kt | 2 +- .../PlayerAsJoinTheGameNotification.kt | 2 +- .../PlayerAsPlayACardNotification.kt | 2 +- .../PlayerHavePassNotification.kt | 2 +- .../PlayerWasChoseTheCardColorNotification.kt | 2 +- .../PlayerWasReadyNotification.kt | 2 +- .../notification/PlayerWinNotification.kt | 2 +- .../TheGameWasStartedNotification.kt | 2 +- .../WelcomeToTheGameNotification.kt | 2 +- .../notification/YourNewCardNotification.kt | 2 +- .../eventDemo/configuration/Configure.kt | 8 ++ .../eventDemo/configuration/ConfigureDI.kt | 65 ------------- .../configuration/ConfigureSerialization.kt | 93 ------------------- .../{ => business}/ConfigureGameListener.kt | 2 +- .../configuration/injection/ConfigureDI.kt | 11 +++ .../{ => injection}/ConfigureDIAction.kt | 4 +- .../injection/ConfigureDIBusiness.kt | 17 ++++ .../injection/ConfigureDIInfrastructure.kt | 25 +++++ .../injection/ConfigureDILibs.kt | 17 ++++ .../configuration/{ => ktor}/ConfigureAuth.kt | 4 +- .../configuration/{ => ktor}/ConfigureHttp.kt | 2 +- .../configuration/ktor/ConfigureKoin.kt | 14 +++ .../ktor/ConfigureSerialization.kt | 35 +++++++ .../{ => ktor}/ConfigureWebSockets.kt | 2 +- .../{ => route}/DeclareHttpRoutes.kt | 2 +- .../{ => route}/DeclareWebSocketsGameRoute.kt | 2 +- .../serializer/CommandIdSerializer.kt | 23 +++++ .../serializer/GameIdSerializer.kt | 24 +++++ .../serializer/PlayerIdSerializer.kt | 24 +++++ .../serializer/UUIDSerializer.kt | 23 +++++ .../kotlin/eventDemo/libs/command/Command.kt | 2 +- .../app/command/GameCommandHandlerTest.kt | 2 +- .../projection/GameStateRepositoryTest.kt | 2 +- .../eventDemo/app/query/GameStateRouteTest.kt | 2 +- .../eventDemo/app/query/GameStateTest.kt | 2 +- .../eventDemo/app/query/TestHttpClient.kt | 2 +- 43 files changed, 254 insertions(+), 191 deletions(-) delete mode 100644 src/main/kotlin/eventDemo/configuration/ConfigureDI.kt delete mode 100644 src/main/kotlin/eventDemo/configuration/ConfigureSerialization.kt rename src/main/kotlin/eventDemo/configuration/{ => business}/ConfigureGameListener.kt (85%) create mode 100644 src/main/kotlin/eventDemo/configuration/injection/ConfigureDI.kt rename src/main/kotlin/eventDemo/configuration/{ => injection}/ConfigureDIAction.kt (84%) create mode 100644 src/main/kotlin/eventDemo/configuration/injection/ConfigureDIBusiness.kt create mode 100644 src/main/kotlin/eventDemo/configuration/injection/ConfigureDIInfrastructure.kt create mode 100644 src/main/kotlin/eventDemo/configuration/injection/ConfigureDILibs.kt rename src/main/kotlin/eventDemo/configuration/{ => ktor}/ConfigureAuth.kt (95%) rename src/main/kotlin/eventDemo/configuration/{ => ktor}/ConfigureHttp.kt (97%) create mode 100644 src/main/kotlin/eventDemo/configuration/ktor/ConfigureKoin.kt create mode 100644 src/main/kotlin/eventDemo/configuration/ktor/ConfigureSerialization.kt rename src/main/kotlin/eventDemo/configuration/{ => ktor}/ConfigureWebSockets.kt (92%) rename src/main/kotlin/eventDemo/configuration/{ => route}/DeclareHttpRoutes.kt (87%) rename src/main/kotlin/eventDemo/configuration/{ => route}/DeclareWebSocketsGameRoute.kt (93%) create mode 100644 src/main/kotlin/eventDemo/configuration/serializer/CommandIdSerializer.kt create mode 100644 src/main/kotlin/eventDemo/configuration/serializer/GameIdSerializer.kt create mode 100644 src/main/kotlin/eventDemo/configuration/serializer/PlayerIdSerializer.kt create mode 100644 src/main/kotlin/eventDemo/configuration/serializer/UUIDSerializer.kt diff --git a/src/main/kotlin/eventDemo/adapter/interfaceLayer/ReadTheGameState.kt b/src/main/kotlin/eventDemo/adapter/interfaceLayer/ReadTheGameState.kt index eca7a91..4868456 100644 --- a/src/main/kotlin/eventDemo/adapter/interfaceLayer/ReadTheGameState.kt +++ b/src/main/kotlin/eventDemo/adapter/interfaceLayer/ReadTheGameState.kt @@ -2,7 +2,7 @@ package eventDemo.adapter.interfaceLayer import eventDemo.business.entity.GameId import eventDemo.business.event.projection.GameStateRepository -import eventDemo.configuration.GameIdSerializer +import eventDemo.configuration.serializer.GameIdSerializer import io.ktor.http.HttpStatusCode import io.ktor.resources.Resource import io.ktor.server.application.call diff --git a/src/main/kotlin/eventDemo/business/entity/Card.kt b/src/main/kotlin/eventDemo/business/entity/Card.kt index 93952c6..c42196b 100644 --- a/src/main/kotlin/eventDemo/business/entity/Card.kt +++ b/src/main/kotlin/eventDemo/business/entity/Card.kt @@ -1,6 +1,6 @@ package eventDemo.business.entity -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/entity/GameId.kt b/src/main/kotlin/eventDemo/business/entity/GameId.kt index 366ed83..e87d311 100644 --- a/src/main/kotlin/eventDemo/business/entity/GameId.kt +++ b/src/main/kotlin/eventDemo/business/entity/GameId.kt @@ -1,6 +1,6 @@ package eventDemo.business.entity -import eventDemo.configuration.GameIdSerializer +import eventDemo.configuration.serializer.GameIdSerializer import eventDemo.libs.event.AggregateId import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/entity/Player.kt b/src/main/kotlin/eventDemo/business/entity/Player.kt index 97cab7a..fccb8f2 100644 --- a/src/main/kotlin/eventDemo/business/entity/Player.kt +++ b/src/main/kotlin/eventDemo/business/entity/Player.kt @@ -1,7 +1,7 @@ package eventDemo.business.entity -import eventDemo.configuration.PlayerIdSerializer -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.PlayerIdSerializer +import eventDemo.configuration.serializer.UUIDSerializer import eventDemo.libs.event.AggregateId import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/CommandErrorNotification.kt b/src/main/kotlin/eventDemo/business/notification/CommandErrorNotification.kt index 74701ad..507e26a 100644 --- a/src/main/kotlin/eventDemo/business/notification/CommandErrorNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/CommandErrorNotification.kt @@ -1,6 +1,6 @@ package eventDemo.business.notification -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import eventDemo.libs.command.Command import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/CommandSuccessNotification.kt b/src/main/kotlin/eventDemo/business/notification/CommandSuccessNotification.kt index 20cc95b..44132c2 100644 --- a/src/main/kotlin/eventDemo/business/notification/CommandSuccessNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/CommandSuccessNotification.kt @@ -1,6 +1,6 @@ package eventDemo.business.notification -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import eventDemo.libs.command.CommandId import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/ItsTheTurnOfNotification.kt b/src/main/kotlin/eventDemo/business/notification/ItsTheTurnOfNotification.kt index 2556070..554134b 100644 --- a/src/main/kotlin/eventDemo/business/notification/ItsTheTurnOfNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/ItsTheTurnOfNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/Notification.kt b/src/main/kotlin/eventDemo/business/notification/Notification.kt index 2b8c078..aa1dc3b 100644 --- a/src/main/kotlin/eventDemo/business/notification/Notification.kt +++ b/src/main/kotlin/eventDemo/business/notification/Notification.kt @@ -1,6 +1,6 @@ package eventDemo.business.notification -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerAsJoinTheGameNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerAsJoinTheGameNotification.kt index 6c4fd36..fd58427 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerAsJoinTheGameNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerAsJoinTheGameNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerAsPlayACardNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerAsPlayACardNotification.kt index 348722a..8305681 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerAsPlayACardNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerAsPlayACardNotification.kt @@ -2,7 +2,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Card import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerHavePassNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerHavePassNotification.kt index 8dadbb0..0c826bf 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerHavePassNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerHavePassNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerWasChoseTheCardColorNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerWasChoseTheCardColorNotification.kt index 56bc30a..0d1e7f8 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerWasChoseTheCardColorNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerWasChoseTheCardColorNotification.kt @@ -2,7 +2,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Card import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerWasReadyNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerWasReadyNotification.kt index b5be34f..764d221 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerWasReadyNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerWasReadyNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/PlayerWinNotification.kt b/src/main/kotlin/eventDemo/business/notification/PlayerWinNotification.kt index ad065a0..91783a1 100644 --- a/src/main/kotlin/eventDemo/business/notification/PlayerWinNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/PlayerWinNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/TheGameWasStartedNotification.kt b/src/main/kotlin/eventDemo/business/notification/TheGameWasStartedNotification.kt index 2d34dde..4d650ac 100644 --- a/src/main/kotlin/eventDemo/business/notification/TheGameWasStartedNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/TheGameWasStartedNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Card -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/WelcomeToTheGameNotification.kt b/src/main/kotlin/eventDemo/business/notification/WelcomeToTheGameNotification.kt index b883597..edd7809 100644 --- a/src/main/kotlin/eventDemo/business/notification/WelcomeToTheGameNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/WelcomeToTheGameNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Player -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/business/notification/YourNewCardNotification.kt b/src/main/kotlin/eventDemo/business/notification/YourNewCardNotification.kt index d15c2a2..640e9e5 100644 --- a/src/main/kotlin/eventDemo/business/notification/YourNewCardNotification.kt +++ b/src/main/kotlin/eventDemo/business/notification/YourNewCardNotification.kt @@ -1,7 +1,7 @@ package eventDemo.business.notification import eventDemo.business.entity.Card -import eventDemo.configuration.UUIDSerializer +import eventDemo.configuration.serializer.UUIDSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/main/kotlin/eventDemo/configuration/Configure.kt b/src/main/kotlin/eventDemo/configuration/Configure.kt index a403143..822f704 100644 --- a/src/main/kotlin/eventDemo/configuration/Configure.kt +++ b/src/main/kotlin/eventDemo/configuration/Configure.kt @@ -1,5 +1,13 @@ package eventDemo.configuration +import eventDemo.configuration.business.configureGameListener +import eventDemo.configuration.ktor.configureHttpRouting +import eventDemo.configuration.ktor.configureKoin +import eventDemo.configuration.ktor.configureSecurity +import eventDemo.configuration.ktor.configureSerialization +import eventDemo.configuration.ktor.configureWebSockets +import eventDemo.configuration.route.declareHttpGameRoute +import eventDemo.configuration.route.declareWebSocketsGameRoute import io.ktor.server.application.Application import org.koin.ktor.ext.get diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureDI.kt b/src/main/kotlin/eventDemo/configuration/ConfigureDI.kt deleted file mode 100644 index 6e93e8a..0000000 --- a/src/main/kotlin/eventDemo/configuration/ConfigureDI.kt +++ /dev/null @@ -1,65 +0,0 @@ -package eventDemo.configuration - -import eventDemo.adapter.infrastructureLayer.event.GameEventBusInMemory -import eventDemo.adapter.infrastructureLayer.event.GameEventStoreInMemory -import eventDemo.adapter.infrastructureLayer.event.projection.GameStateRepositoryInMemory -import eventDemo.business.command.GameCommandActionRunner -import eventDemo.business.command.GameCommandHandler -import eventDemo.business.command.command.GameCommand -import eventDemo.business.event.GameEventBus -import eventDemo.business.event.GameEventHandler -import eventDemo.business.event.GameEventStore -import eventDemo.business.event.eventListener.PlayerNotificationEventListener -import eventDemo.business.event.projection.GameStateRepository -import eventDemo.libs.command.CommandRunnerController -import eventDemo.libs.command.CommandStreamChannel -import eventDemo.libs.event.VersionBuilder -import eventDemo.libs.event.VersionBuilderLocal -import eventDemo.libs.event.projection.SnapshotConfig -import io.ktor.server.application.Application -import io.ktor.server.application.install -import org.koin.core.module.dsl.singleOf -import org.koin.dsl.bind -import org.koin.dsl.module -import org.koin.ktor.plugin.Koin -import org.koin.logger.slf4jLogger - -fun Application.configureKoin() { - install(Koin) { - slf4jLogger() - modules(appKoinModule) - } -} - -val appKoinModule = - module { - single { - GameEventBusInMemory() - } bind GameEventBus::class - - single { - GameEventStoreInMemory() - } bind GameEventStore::class - - single { - GameStateRepositoryInMemory(get(), get(), snapshotConfig = SnapshotConfig()) - } bind GameStateRepository::class - - single { - CommandStreamChannel(get()) - } - single { - CommandRunnerController() - } - single { - GameCommandHandler(get(), get(), get(), get()) - } - - singleOf(::VersionBuilderLocal) bind VersionBuilder::class - singleOf(::GameEventHandler) - singleOf(::GameCommandActionRunner) - singleOf(::PlayerNotificationEventListener) - - // Actions - configureActions() - } diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureSerialization.kt b/src/main/kotlin/eventDemo/configuration/ConfigureSerialization.kt deleted file mode 100644 index b9e302a..0000000 --- a/src/main/kotlin/eventDemo/configuration/ConfigureSerialization.kt +++ /dev/null @@ -1,93 +0,0 @@ -package eventDemo.configuration - -import eventDemo.business.entity.GameId -import eventDemo.business.entity.Player -import eventDemo.libs.command.CommandId -import io.ktor.serialization.kotlinx.json.json -import io.ktor.server.application.Application -import io.ktor.server.application.install -import io.ktor.server.plugins.contentnegotiation.ContentNegotiation -import kotlinx.serialization.KSerializer -import kotlinx.serialization.descriptors.PrimitiveKind -import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.encoding.Decoder -import kotlinx.serialization.encoding.Encoder -import kotlinx.serialization.json.Json -import kotlinx.serialization.modules.SerializersModule -import java.util.UUID - -fun Application.configureSerialization() { - install(ContentNegotiation) { - json( - defaultJsonSerializer(), - ) - } -} - -fun defaultJsonSerializer(): Json = - Json { - serializersModule = - SerializersModule { - contextual(UUID::class) { UUIDSerializer } - contextual(GameId::class) { GameIdSerializer } - contextual(CommandId::class) { CommandIdSerializer } - contextual(Player.PlayerId::class) { PlayerIdSerializer } - } - } - -object CommandIdSerializer : KSerializer { - override fun deserialize(decoder: Decoder): CommandId = - CommandId(decoder.decodeString()) - - override fun serialize( - encoder: Encoder, - value: CommandId, - ) { - encoder.encodeString(value.toString()) - } - - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CommandId", PrimitiveKind.STRING) -} - -object PlayerIdSerializer : KSerializer { - override fun deserialize(decoder: Decoder): Player.PlayerId = - Player.PlayerId(UUID.fromString(decoder.decodeString())) - - override fun serialize( - encoder: Encoder, - value: Player.PlayerId, - ) { - encoder.encodeString(value.id.toString()) - } - - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("PlayerId", PrimitiveKind.STRING) -} - -object GameIdSerializer : KSerializer { - override fun deserialize(decoder: Decoder): GameId = - GameId(UUID.fromString(decoder.decodeString())) - - override fun serialize( - encoder: Encoder, - value: GameId, - ) { - encoder.encodeString(value.id.toString()) - } - - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("GameId", PrimitiveKind.STRING) -} - -object UUIDSerializer : KSerializer { - override fun deserialize(decoder: Decoder): UUID = - UUID.fromString(decoder.decodeString()) - - override fun serialize( - encoder: Encoder, - value: UUID, - ) { - encoder.encodeString(value.toString()) - } - - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING) -} diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureGameListener.kt b/src/main/kotlin/eventDemo/configuration/business/ConfigureGameListener.kt similarity index 85% rename from src/main/kotlin/eventDemo/configuration/ConfigureGameListener.kt rename to src/main/kotlin/eventDemo/configuration/business/ConfigureGameListener.kt index d24a64c..71ecdef 100644 --- a/src/main/kotlin/eventDemo/configuration/ConfigureGameListener.kt +++ b/src/main/kotlin/eventDemo/configuration/business/ConfigureGameListener.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.business import eventDemo.business.event.eventListener.ReactionEventListener import io.ktor.server.application.Application diff --git a/src/main/kotlin/eventDemo/configuration/injection/ConfigureDI.kt b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDI.kt new file mode 100644 index 0000000..e03a1ab --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDI.kt @@ -0,0 +1,11 @@ +package eventDemo.configuration.injection + +import org.koin.dsl.module + +val appKoinModule = + module { + configureDIBusiness() + configureDIInfrastructure() + configureDILibs() + configureDICommandActions() + } diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureDIAction.kt b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIAction.kt similarity index 84% rename from src/main/kotlin/eventDemo/configuration/ConfigureDIAction.kt rename to src/main/kotlin/eventDemo/configuration/injection/ConfigureDIAction.kt index fbfe9e0..14c3dba 100644 --- a/src/main/kotlin/eventDemo/configuration/ConfigureDIAction.kt +++ b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIAction.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.injection import eventDemo.business.command.action.ICantPlay import eventDemo.business.command.action.IWantToJoinTheGame @@ -10,7 +10,7 @@ import org.koin.core.module.dsl.singleOf /** * Configure all actions */ -fun Module.configureActions() { +fun Module.configureDICommandActions() { singleOf(::IWantToPlayCard) singleOf(::IamReadyToPlay) singleOf(::IWantToJoinTheGame) diff --git a/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIBusiness.kt b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIBusiness.kt new file mode 100644 index 0000000..3aa9cf7 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIBusiness.kt @@ -0,0 +1,17 @@ +package eventDemo.configuration.injection + +import eventDemo.business.command.GameCommandActionRunner +import eventDemo.business.command.GameCommandHandler +import eventDemo.business.event.GameEventHandler +import eventDemo.business.event.eventListener.PlayerNotificationEventListener +import org.koin.core.module.Module +import org.koin.core.module.dsl.singleOf + +fun Module.configureDIBusiness() { + single { + GameCommandHandler(get(), get(), get(), get()) + } + singleOf(::GameEventHandler) + singleOf(::GameCommandActionRunner) + singleOf(::PlayerNotificationEventListener) +} diff --git a/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIInfrastructure.kt b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIInfrastructure.kt new file mode 100644 index 0000000..1d7b59b --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDIInfrastructure.kt @@ -0,0 +1,25 @@ +package eventDemo.configuration.injection + +import eventDemo.adapter.infrastructureLayer.event.GameEventBusInMemory +import eventDemo.adapter.infrastructureLayer.event.GameEventStoreInMemory +import eventDemo.adapter.infrastructureLayer.event.projection.GameStateRepositoryInMemory +import eventDemo.business.event.GameEventBus +import eventDemo.business.event.GameEventStore +import eventDemo.business.event.projection.GameStateRepository +import eventDemo.libs.event.projection.SnapshotConfig +import org.koin.core.module.Module +import org.koin.dsl.bind + +fun Module.configureDIInfrastructure() { + single { + GameEventBusInMemory() + } bind GameEventBus::class + + single { + GameEventStoreInMemory() + } bind GameEventStore::class + + single { + GameStateRepositoryInMemory(get(), get(), snapshotConfig = SnapshotConfig()) + } bind GameStateRepository::class +} diff --git a/src/main/kotlin/eventDemo/configuration/injection/ConfigureDILibs.kt b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDILibs.kt new file mode 100644 index 0000000..034b9e2 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/injection/ConfigureDILibs.kt @@ -0,0 +1,17 @@ +package eventDemo.configuration.injection + +import eventDemo.business.command.command.GameCommand +import eventDemo.libs.command.CommandRunnerController +import eventDemo.libs.command.CommandStreamChannel +import eventDemo.libs.event.VersionBuilder +import eventDemo.libs.event.VersionBuilderLocal +import org.koin.core.module.Module +import org.koin.core.module.dsl.singleOf +import org.koin.dsl.bind + +fun Module.configureDILibs() { + single { + CommandStreamChannel(CommandRunnerController()) + } + singleOf(::VersionBuilderLocal) bind VersionBuilder::class +} diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureAuth.kt b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureAuth.kt similarity index 95% rename from src/main/kotlin/eventDemo/configuration/ConfigureAuth.kt rename to src/main/kotlin/eventDemo/configuration/ktor/ConfigureAuth.kt index 362ea00..fc73f96 100644 --- a/src/main/kotlin/eventDemo/configuration/ConfigureAuth.kt +++ b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureAuth.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.ktor import com.auth0.jwt.JWT import com.auth0.jwt.algorithms.Algorithm @@ -37,7 +37,7 @@ fun Application.configureSecurity() { null } } - challenge { defaultScheme, realm -> + challenge { _, _ -> call.respond(HttpStatusCode.Unauthorized, "Token is not valid or has expired") } } diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureHttp.kt b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureHttp.kt similarity index 97% rename from src/main/kotlin/eventDemo/configuration/ConfigureHttp.kt rename to src/main/kotlin/eventDemo/configuration/ktor/ConfigureHttp.kt index d34e1c6..5d68bff 100644 --- a/src/main/kotlin/eventDemo/configuration/ConfigureHttp.kt +++ b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureHttp.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.ktor import io.ktor.http.HttpHeaders import io.ktor.http.HttpMethod diff --git a/src/main/kotlin/eventDemo/configuration/ktor/ConfigureKoin.kt b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureKoin.kt new file mode 100644 index 0000000..91912d9 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureKoin.kt @@ -0,0 +1,14 @@ +package eventDemo.configuration.ktor + +import eventDemo.configuration.injection.appKoinModule +import io.ktor.server.application.Application +import io.ktor.server.application.install +import org.koin.ktor.plugin.Koin +import org.koin.logger.slf4jLogger + +fun Application.configureKoin() { + install(Koin) { + slf4jLogger() + modules(appKoinModule) + } +} diff --git a/src/main/kotlin/eventDemo/configuration/ktor/ConfigureSerialization.kt b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureSerialization.kt new file mode 100644 index 0000000..f2d0ab9 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureSerialization.kt @@ -0,0 +1,35 @@ +package eventDemo.configuration.ktor + +import eventDemo.business.entity.GameId +import eventDemo.business.entity.Player +import eventDemo.configuration.serializer.CommandIdSerializer +import eventDemo.configuration.serializer.GameIdSerializer +import eventDemo.configuration.serializer.PlayerIdSerializer +import eventDemo.configuration.serializer.UUIDSerializer +import eventDemo.libs.command.CommandId +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import kotlinx.serialization.json.Json +import kotlinx.serialization.modules.SerializersModule +import java.util.UUID + +fun Application.configureSerialization() { + install(ContentNegotiation) { + json( + defaultJsonSerializer(), + ) + } +} + +fun defaultJsonSerializer(): Json = + Json { + serializersModule = + SerializersModule { + contextual(UUID::class) { UUIDSerializer } + contextual(GameId::class) { GameIdSerializer } + contextual(CommandId::class) { CommandIdSerializer } + contextual(Player.PlayerId::class) { PlayerIdSerializer } + } + } diff --git a/src/main/kotlin/eventDemo/configuration/ConfigureWebSockets.kt b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureWebSockets.kt similarity index 92% rename from src/main/kotlin/eventDemo/configuration/ConfigureWebSockets.kt rename to src/main/kotlin/eventDemo/configuration/ktor/ConfigureWebSockets.kt index b759f84..9439eca 100644 --- a/src/main/kotlin/eventDemo/configuration/ConfigureWebSockets.kt +++ b/src/main/kotlin/eventDemo/configuration/ktor/ConfigureWebSockets.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.ktor import io.ktor.server.application.Application import io.ktor.server.application.install diff --git a/src/main/kotlin/eventDemo/configuration/DeclareHttpRoutes.kt b/src/main/kotlin/eventDemo/configuration/route/DeclareHttpRoutes.kt similarity index 87% rename from src/main/kotlin/eventDemo/configuration/DeclareHttpRoutes.kt rename to src/main/kotlin/eventDemo/configuration/route/DeclareHttpRoutes.kt index 28cbb46..94450a9 100644 --- a/src/main/kotlin/eventDemo/configuration/DeclareHttpRoutes.kt +++ b/src/main/kotlin/eventDemo/configuration/route/DeclareHttpRoutes.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.route import eventDemo.adapter.interfaceLayer.readTheGameState import io.ktor.server.application.Application diff --git a/src/main/kotlin/eventDemo/configuration/DeclareWebSocketsGameRoute.kt b/src/main/kotlin/eventDemo/configuration/route/DeclareWebSocketsGameRoute.kt similarity index 93% rename from src/main/kotlin/eventDemo/configuration/DeclareWebSocketsGameRoute.kt rename to src/main/kotlin/eventDemo/configuration/route/DeclareWebSocketsGameRoute.kt index 2a81afb..5acc602 100644 --- a/src/main/kotlin/eventDemo/configuration/DeclareWebSocketsGameRoute.kt +++ b/src/main/kotlin/eventDemo/configuration/route/DeclareWebSocketsGameRoute.kt @@ -1,4 +1,4 @@ -package eventDemo.configuration +package eventDemo.configuration.route import eventDemo.adapter.interfaceLayer.gameWebSocket import eventDemo.business.command.GameCommandHandler diff --git a/src/main/kotlin/eventDemo/configuration/serializer/CommandIdSerializer.kt b/src/main/kotlin/eventDemo/configuration/serializer/CommandIdSerializer.kt new file mode 100644 index 0000000..ebad2dd --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/serializer/CommandIdSerializer.kt @@ -0,0 +1,23 @@ +package eventDemo.configuration.serializer + +import eventDemo.libs.command.CommandId +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +object CommandIdSerializer : KSerializer { + override fun deserialize(decoder: Decoder): CommandId = + CommandId(decoder.decodeString()) + + override fun serialize( + encoder: Encoder, + value: CommandId, + ) { + encoder.encodeString(value.toString()) + } + + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CommandId", PrimitiveKind.STRING) +} diff --git a/src/main/kotlin/eventDemo/configuration/serializer/GameIdSerializer.kt b/src/main/kotlin/eventDemo/configuration/serializer/GameIdSerializer.kt new file mode 100644 index 0000000..88fa44a --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/serializer/GameIdSerializer.kt @@ -0,0 +1,24 @@ +package eventDemo.configuration.serializer + +import eventDemo.business.entity.GameId +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.util.UUID + +object GameIdSerializer : KSerializer { + override fun deserialize(decoder: Decoder): GameId = + GameId(UUID.fromString(decoder.decodeString())) + + override fun serialize( + encoder: Encoder, + value: GameId, + ) { + encoder.encodeString(value.id.toString()) + } + + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("GameId", PrimitiveKind.STRING) +} diff --git a/src/main/kotlin/eventDemo/configuration/serializer/PlayerIdSerializer.kt b/src/main/kotlin/eventDemo/configuration/serializer/PlayerIdSerializer.kt new file mode 100644 index 0000000..57c1a72 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/serializer/PlayerIdSerializer.kt @@ -0,0 +1,24 @@ +package eventDemo.configuration.serializer + +import eventDemo.business.entity.Player +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.util.UUID + +object PlayerIdSerializer : KSerializer { + override fun deserialize(decoder: Decoder): Player.PlayerId = + Player.PlayerId(UUID.fromString(decoder.decodeString())) + + override fun serialize( + encoder: Encoder, + value: Player.PlayerId, + ) { + encoder.encodeString(value.id.toString()) + } + + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("PlayerId", PrimitiveKind.STRING) +} diff --git a/src/main/kotlin/eventDemo/configuration/serializer/UUIDSerializer.kt b/src/main/kotlin/eventDemo/configuration/serializer/UUIDSerializer.kt new file mode 100644 index 0000000..6328f10 --- /dev/null +++ b/src/main/kotlin/eventDemo/configuration/serializer/UUIDSerializer.kt @@ -0,0 +1,23 @@ +package eventDemo.configuration.serializer + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.util.UUID + +object UUIDSerializer : KSerializer { + override fun deserialize(decoder: Decoder): UUID = + UUID.fromString(decoder.decodeString()) + + override fun serialize( + encoder: Encoder, + value: UUID, + ) { + encoder.encodeString(value.toString()) + } + + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING) +} diff --git a/src/main/kotlin/eventDemo/libs/command/Command.kt b/src/main/kotlin/eventDemo/libs/command/Command.kt index fed17d0..39b966c 100644 --- a/src/main/kotlin/eventDemo/libs/command/Command.kt +++ b/src/main/kotlin/eventDemo/libs/command/Command.kt @@ -1,6 +1,6 @@ package eventDemo.libs.command -import eventDemo.configuration.CommandIdSerializer +import eventDemo.configuration.serializer.CommandIdSerializer import kotlinx.serialization.Serializable import java.util.UUID diff --git a/src/test/kotlin/eventDemo/app/command/GameCommandHandlerTest.kt b/src/test/kotlin/eventDemo/app/command/GameCommandHandlerTest.kt index e1bb64d..f65b1ea 100644 --- a/src/test/kotlin/eventDemo/app/command/GameCommandHandlerTest.kt +++ b/src/test/kotlin/eventDemo/app/command/GameCommandHandlerTest.kt @@ -10,7 +10,7 @@ import eventDemo.business.event.eventListener.ReactionEventListener import eventDemo.business.notification.CommandSuccessNotification import eventDemo.business.notification.Notification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.appKoinModule +import eventDemo.configuration.injection.appKoinModule import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.equals.shouldBeEqual diff --git a/src/test/kotlin/eventDemo/app/event/projection/GameStateRepositoryTest.kt b/src/test/kotlin/eventDemo/app/event/projection/GameStateRepositoryTest.kt index 533fe88..12dbb6f 100644 --- a/src/test/kotlin/eventDemo/app/event/projection/GameStateRepositoryTest.kt +++ b/src/test/kotlin/eventDemo/app/event/projection/GameStateRepositoryTest.kt @@ -5,7 +5,7 @@ import eventDemo.business.entity.Player import eventDemo.business.event.GameEventHandler import eventDemo.business.event.event.NewPlayerEvent import eventDemo.business.event.projection.GameStateRepository -import eventDemo.configuration.appKoinModule +import eventDemo.configuration.injection.appKoinModule import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.equals.shouldBeEqual diff --git a/src/test/kotlin/eventDemo/app/query/GameStateRouteTest.kt b/src/test/kotlin/eventDemo/app/query/GameStateRouteTest.kt index 1ca6913..97f80fc 100644 --- a/src/test/kotlin/eventDemo/app/query/GameStateRouteTest.kt +++ b/src/test/kotlin/eventDemo/app/query/GameStateRouteTest.kt @@ -11,7 +11,7 @@ import eventDemo.business.event.event.PlayerReadyEvent import eventDemo.business.event.projection.GameState import eventDemo.business.event.projection.GameStateRepository import eventDemo.configuration.configure -import eventDemo.configuration.makeJwt +import eventDemo.configuration.ktor.makeJwt import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.equals.shouldBeEqual diff --git a/src/test/kotlin/eventDemo/app/query/GameStateTest.kt b/src/test/kotlin/eventDemo/app/query/GameStateTest.kt index e6595b8..e10de81 100644 --- a/src/test/kotlin/eventDemo/app/query/GameStateTest.kt +++ b/src/test/kotlin/eventDemo/app/query/GameStateTest.kt @@ -22,7 +22,7 @@ import eventDemo.business.notification.PlayerAsPlayACardNotification import eventDemo.business.notification.PlayerWasReadyNotification import eventDemo.business.notification.TheGameWasStartedNotification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.appKoinModule +import eventDemo.configuration.injection.appKoinModule import eventDemo.libs.event.projection.ProjectionSnapshotRepositoryInMemory import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize diff --git a/src/test/kotlin/eventDemo/app/query/TestHttpClient.kt b/src/test/kotlin/eventDemo/app/query/TestHttpClient.kt index 8dee0fd..13bb1f5 100644 --- a/src/test/kotlin/eventDemo/app/query/TestHttpClient.kt +++ b/src/test/kotlin/eventDemo/app/query/TestHttpClient.kt @@ -1,5 +1,5 @@ package eventDemo.app.query -import eventDemo.configuration.defaultJsonSerializer +import eventDemo.configuration.ktor.defaultJsonSerializer import io.ktor.client.HttpClient import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.serialization.kotlinx.json.json