Skip to content

Commit

Permalink
feat(project)!: cleanup micronaut configurations and dependencies, fi…
Browse files Browse the repository at this point in the history
…xed micronaut problems with serializations
  • Loading branch information
alemazzo committed May 7, 2024
1 parent e2dd408 commit 9c58d74
Show file tree
Hide file tree
Showing 36 changed files with 136 additions and 134 deletions.
11 changes: 0 additions & 11 deletions .deepsource.toml

This file was deleted.

7 changes: 6 additions & 1 deletion build-logic/src/main/kotlin/micronaut-base.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
plugins {
id("kotlin-base")
id("org.jetbrains.kotlin.jvm")
id("org.jetbrains.kotlin.plugin.allopen")
id("com.google.devtools.ksp")
id("com.github.johnrengelman.shadow")
id("io.micronaut.minimal.application")
id("io.micronaut.application")
id("io.micronaut.aot")
id("io.micronaut.test-resources")
}
Expand All @@ -17,8 +18,10 @@ dependencies {
compileOnly(it)
testImplementation(it)
}
testImplementation(catalog.getLibrary("mockito-kotlin"))
runtimeOnly(catalog.getLibrary("logback"))
runtimeOnly(catalog.getLibrary("jackson-kotlin"))
runtimeOnly(catalog.getLibrary("snakeyaml"))
}

java { sourceCompatibility = JavaVersion.asVersion }
Expand All @@ -41,4 +44,6 @@ micronaut {
deduceEnvironment = true
optimizeNetty = true
}

}

14 changes: 14 additions & 0 deletions build-logic/src/main/kotlin/micronaut-full.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("micronaut-base")
}

val catalog: VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

dependencies {
ksp(catalog.getLibrary("micronaut-data-document-processor"))
implementation(catalog.getLibrary("micronaut-data-mongodb"))
runtimeOnly(catalog.getLibrary("mongodb-driver-sync"))
implementation(catalog.getLibrary("micronaut-kafka"))
}


4 changes: 0 additions & 4 deletions friendships-service/application.yml

This file was deleted.

11 changes: 2 additions & 9 deletions friendships-service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
plugins {
id("micronaut-base")
id("micronaut-full")
id("java-test-fixtures")
}

application { mainClass.set("piperkt.services.friendships.ApplicationKt") }

detekt {
config.setFrom("./detekt-config.yml")
}
detekt { config.setFrom("./detekt-config.yml") }

dependencies {
ksp("io.micronaut.data:micronaut-data-document-processor")
implementation("io.micronaut.data:micronaut-data-mongodb")
testImplementation(testFixtures(project(":commons")))
runtimeOnly("org.mongodb:mongodb-driver-sync")
implementation(project(":commons"))
testImplementation("org.mockito.kotlin:mockito-kotlin:5.3.1")
// Kafka
implementation("io.micronaut.kafka:micronaut-kafka")
// Jwt
implementation("io.micronaut.security:micronaut-security-jwt")
implementation("io.micronaut:micronaut-retry")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,3 @@ import io.micronaut.runtime.Micronaut.run
fun main(vararg args: String) {
run(*args)
}

// LEVEL -> WHATS -> DEPENDENCIES
// --------------------------------
// DOMAIN -> Language:
// - Entities
// - Value Objects
// - Repositories
// - Factories
// APPLICATION -> Domain
// - Services
// INTERFACES ->
// - Controller (Indepedent of the framework)
// INFRASTRUCTURE
4 changes: 4 additions & 0 deletions friendships-service/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
micronaut:
http:
client:
read-timeout: 60s
3 changes: 0 additions & 3 deletions friendships-service/src/main/resources/application.properties

This file was deleted.

6 changes: 6 additions & 0 deletions friendships-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
application:
name: "Piperkt Friendships Service"
version: "1.0.0"
mongodb:
package-names:
- piperkt.services.friendships.infrastructure.persistence
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import io.micronaut.http.annotation.PathVariable
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.QueryValue
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.Retryable
import piperkt.services.friendships.interfaces.web.api.interactions.FriendshipApi

@Client("/")
@Retryable
interface FriendshipHttpClient {

@Post("friends/requests/send/")
Expand Down
14 changes: 11 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ micronaut = "4.3.8"
micronautBuildTools = "2.5.1"
shadow = "8.1.1"
spotless = "6.25.0"
mockito-kotlin = "5.3.1"

[libraries]
cucumber-bom = { module = "io.cucumber:cucumber-bom", version.ref = "cucumber-bom" }
Expand All @@ -28,13 +29,20 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref =
kotlin-stdlibJdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
konsist = { module = "com.lemonappdev:konsist", version.ref = "konsist" }
kotest = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockito-kotlin" }
logback = { module = "ch.qos.logback:logback-classic" }
snakeyaml = { module = "org.yaml:snakeyaml" }
micronaut-buildtools = { module = "io.micronaut.testresources:micronaut-test-resources-build-tools", version.ref = "micronautBuildTools" }
micronaut-httpClient = { module = "io.micronaut:micronaut-http-client" }
micronaut-httpValidation = { module = "io.micronaut:micronaut-http-validation" }
micronaut-kotlinRuntime = { module = "io.micronaut.kotlin:micronaut-kotlin-runtime" }
micronaut-serdeJackson = { module = "io.micronaut.serde:micronaut-serde-jackson" }
micronaut-serdeProcessor = { module = "io.micronaut.serde:micronaut-serde-processor" }
micronaut-kafka = { module = "io.micronaut.kafka:micronaut-kafka" }
micronaut-data-mongodb = { module = "io.micronaut.data:micronaut-data-mongodb" }
micronaut-data-document-processor = { module = "io.micronaut.data:micronaut-data-document-processor" }
mongodb-driver-sync = { module = "org.mongodb:mongodb-driver-sync" }


[plugins]
allOpen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }
Expand All @@ -52,6 +60,6 @@ shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }

[bundles]
cucumberJunit = [ "cucumber-java", "cucumber-junit" ]
micronaut-ksp = [ "micronaut-httpValidation", "micronaut-serdeProcessor" ]
micronaut-kotlin = [ "kotlin-reflect", "kotlin-stdlibJdk8", "micronaut-kotlinRuntime", "micronaut-serdeJackson", ]
cucumberJunit = ["cucumber-java", "cucumber-junit"]
micronaut-ksp = ["micronaut-httpValidation", "micronaut-serdeProcessor"]
micronaut-kotlin = ["kotlin-reflect", "kotlin-stdlibJdk8", "micronaut-kotlinRuntime", "micronaut-serdeJackson"]
4 changes: 0 additions & 4 deletions multimedia-service/application.yml

This file was deleted.

10 changes: 1 addition & 9 deletions multimedia-service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
plugins {
id("micronaut-base")
id("micronaut-full")
id("java-test-fixtures")
kotlin("plugin.serialization") version "1.9.24"
}

application { mainClass.set("piperkt.services.multimedia.ApplicationKt") }

dependencies {
implementation(project(":commons"))
testImplementation(testFixtures(project(":commons")))
ksp("io.micronaut.data:micronaut-data-document-processor")
implementation("io.micronaut.data:micronaut-data-mongodb")
runtimeOnly("org.mongodb:mongodb-driver-sync")
// Kafka
implementation("io.micronaut.kafka:micronaut-kafka")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.3.1")
implementation("com.corundumstudio.socketio:netty-socketio:2.0.9")
implementation("io.socket:socket.io-client:2.1.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
annotationProcessor("io.micronaut.security:micronaut-security-annotations")
implementation("io.micronaut.security:micronaut-security-jwt")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package piperkt.services.multimedia.interfaces.websockets

import kotlinx.serialization.Serializable
import io.micronaut.serde.annotation.Serdeable

sealed interface MultimediaProtocolMessage {

@Serializable data class JoinMessage(val sessionId: String) : MultimediaProtocolMessage
@Serdeable data class JoinMessage(val sessionId: String) : MultimediaProtocolMessage

@Serializable
@Serdeable
data class OfferMessage(val from: String, val to: String, val offer: String) :
MultimediaProtocolMessage

@Serializable
@Serdeable
data class AnswerMessage(val from: String, val to: String, val answer: String) :
MultimediaProtocolMessage

@Serializable
@Serdeable
data class IceCandidateMessage(val from: String, val to: String, val candidate: String) :
MultimediaProtocolMessage

@Serializable
@Serdeable
data class UserJoined(val sessionId: String, val userId: String) : MultimediaProtocolMessage
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class SocketIOConfiguration {

class MultimediaSocketIOServer(
private val sessionService: SessionService,
val configuration: SocketIOConfiguration = SocketIOConfiguration(),
val objectMapper: JsonMapper,
val configuration: SocketIOConfiguration = SocketIOConfiguration()
) {

private val socketIoConfig =
Configuration().apply { port = configuration.port }.apply { isNeedClientAuth = false }
private val server = SocketIOServer(socketIoConfig)
Expand All @@ -33,19 +35,26 @@ class MultimediaSocketIOServer(
fun start() {
server.addConnectListener { client -> runCatching { onConnect(client) } }
server.addDisconnectListener { client -> runCatching { onDisconnect(client) } }
server.on(JOIN.event) { client, message: MultimediaProtocolMessage.JoinMessage, _ ->
server.on(objectMapper, JOIN.event) {
client,
message: MultimediaProtocolMessage.JoinMessage,
_ ->
events.add(message)
runCatching { onJoin(client, message) }
}
server.on(OFFER.event) { _, message: MultimediaProtocolMessage.OfferMessage, _ ->
server.on(objectMapper, OFFER.event) { _, message: MultimediaProtocolMessage.OfferMessage, _
->
events.add(message)
runCatching { onOffer(message) }
}
server.on(ANSWER.event) { _, message: MultimediaProtocolMessage.AnswerMessage, _ ->
server.on(objectMapper, ANSWER.event) {
_,
message: MultimediaProtocolMessage.AnswerMessage,
_ ->
events.add(message)
runCatching { onAnswer(message) }
}
server.on(ICE_CANDIDATE.event) {
server.on(objectMapper, ICE_CANDIDATE.event) {
_,
message: MultimediaProtocolMessage.IceCandidateMessage,
_ ->
Expand Down Expand Up @@ -93,7 +102,8 @@ class MultimediaSocketIOServer(
val username = client.getUsername() ?: return client.notAuthenticated()
val sessionId = joinMessage.sessionId
clientToSessionId[username] = sessionId
roomOf(sessionId).sendEvent(USER_JOIN.event, UserJoined(sessionId, username).toJson())
val serialized = objectMapper.toJson(UserJoined(sessionId, username))
roomOf(sessionId).sendEvent(USER_JOIN.event, serialized)
client.joinRoom(sessionId)
sessionService.joinSession(JoinSession(SessionId(sessionId), Username(username)))
println("User $username joined session $sessionId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ package piperkt.services.multimedia.interfaces.websockets

import com.corundumstudio.socketio.SocketIOServer
import com.corundumstudio.socketio.listener.DataListener
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import io.micronaut.serde.ObjectMapper
import jakarta.inject.Singleton

inline fun <reified E> E.toJson(): String = Json.encodeToString(serializer(), this)
@Singleton
class JsonMapper(private val mapper: ObjectMapper) {
fun <E> fromJson(json: String, clazz: Class<E>): E {
return mapper.readValue(json, clazz)
}

inline fun <reified E> fromJson(json: String): E = Json.decodeFromString(serializer(), json)
fun toJson(obj: Any): String {
return mapper.writeValueAsString(obj)
}
}

inline fun <reified E> SocketIOServer.on(event: String, listener: DataListener<E>) {
inline fun <reified E> SocketIOServer.on(
mapper: JsonMapper,
event: String,
listener: DataListener<E>
) {
this.addEventListener(event, String::class.java) { client, data, ackRequest ->
val message = fromJson<E>(data)
val message = mapper.fromJson(data, E::class.java)
listener.onData(client, message, ackRequest)
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions multimedia-service/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
micronaut:
http:
client:
read-timeout: 60s
5 changes: 0 additions & 5 deletions multimedia-service/src/main/resources/application.properties

This file was deleted.

6 changes: 6 additions & 0 deletions multimedia-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
application:
name: "Piperkt Users Service"
version: "1.0.0"
mongodb:
package-names:
- piperkt.services.multimedia.infrastructure.persistence
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package piperkt.services.multimedia.interfaces.websockets

import base.UnitTest
import base.IntegrationTest
import data.UsersData.jane
import data.UsersData.john
import io.kotest.assertions.fail
import io.socket.client.IO
import io.socket.client.Socket
import jakarta.inject.Inject
import mocks.publishers.MockedSessionEventPublisher
import mocks.repositories.InMemorySessionRepository
import piperkt.services.multimedia.application.session.SessionService
import piperkt.services.multimedia.domain.session.SessionFactory
import piperkt.services.multimedia.interfaces.websockets.MultimediaProtocolEvent.*
import piperkt.services.multimedia.interfaces.websockets.MultimediaProtocolMessage.*

class MultimediaSocketIOServerTest :
UnitTest.FunSpec({
class MultimediaSocketIOServerTest(@Inject private val objectMapper: JsonMapper) :
IntegrationTest.FunSpec({
// Setup
val sessionRepository = InMemorySessionRepository()
val sessionService = SessionService(sessionRepository, MockedSessionEventPublisher())
val server = MultimediaSocketIOServer(sessionService)
val server = MultimediaSocketIOServer(sessionService, objectMapper)

// Setup data
val johnId = john().id
Expand All @@ -40,6 +41,8 @@ class MultimediaSocketIOServerTest :
val johnClient = authorizedClientOf(johnId.value)
val janeClient = authorizedClientOf(janeId.value)

fun Any.toJson(): String = objectMapper.toJson(this)

test("should allow client to join") {
val message = JoinMessage(session.id.value)
johnClient.emit(JOIN.event, message.toJson())
Expand Down
4 changes: 0 additions & 4 deletions servers-service/application.yml

This file was deleted.

Loading

0 comments on commit 9c58d74

Please sign in to comment.