feat: NCS-53 changed IO to MicroService for easier scaling (#37)

Reviewed-on: #37
Reviewed-by: Shahd Lala <shosho996@blackhole.local>
This commit is contained in:
2026-04-21 15:38:58 +02:00
parent 74a4fce0ca
commit b5a2966ada
53 changed files with 772 additions and 31 deletions
@@ -0,0 +1,13 @@
quarkus:
http:
port: 8081
application:
name: nowchess-io
smallrye-openapi:
info-title: NowChess IO Service
info-version: 1.0.0
info-description: Chess notation import and export — FEN and PGN
path: /openapi
swagger-ui:
always-include: true
path: /swagger-ui
@@ -0,0 +1,8 @@
package de.nowchess.io.json
import com.fasterxml.jackson.databind.{DeserializationContext, KeyDeserializer}
import de.nowchess.api.board.Square
class SquareKeyDeserializer extends KeyDeserializer:
override def deserializeKey(key: String, ctx: DeserializationContext): AnyRef =
Square.fromAlgebraic(key).orNull
@@ -0,0 +1,9 @@
package de.nowchess.io.json
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.{JsonSerializer, SerializerProvider}
import de.nowchess.api.board.Square
class SquareKeySerializer extends JsonSerializer[Square]:
override def serialize(value: Square, gen: JsonGenerator, provider: SerializerProvider): Unit =
gen.writeFieldName(value.toString)
@@ -0,0 +1,24 @@
package de.nowchess.io.service.config
import com.fasterxml.jackson.core.Version
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.nowchess.api.board.Square
import de.nowchess.io.json.{SquareKeyDeserializer, SquareKeySerializer}
import io.quarkus.jackson.ObjectMapperCustomizer
import jakarta.inject.Singleton
@Singleton
class JacksonConfig extends ObjectMapperCustomizer:
def customize(mapper: ObjectMapper): Unit =
mapper.registerModule(new DefaultScalaModule() {
override def version(): Version =
// scalafix:off DisableSyntax.null
new Version(2, 21, 1, null, "com.fasterxml.jackson.module", "jackson-module-scala")
// scalafix:on DisableSyntax.null
})
val squareModule = new SimpleModule()
squareModule.addKeyDeserializer(classOf[Square], new SquareKeyDeserializer())
squareModule.addKeySerializer(classOf[Square], new SquareKeySerializer())
mapper.registerModule(squareModule)
@@ -0,0 +1,29 @@
package de.nowchess.io.service.config
import de.nowchess.api.board.{CastlingRights, Color, File, Piece, PieceType, Rank, Square}
import de.nowchess.api.game.{DrawReason, GameContext, GameResult}
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
import de.nowchess.io.service.dto.{ImportFenRequest, ImportPgnRequest, IoErrorDto}
import io.quarkus.runtime.annotations.RegisterForReflection
@RegisterForReflection(
targets = Array(
classOf[ImportFenRequest],
classOf[ImportPgnRequest],
classOf[IoErrorDto],
classOf[GameContext],
classOf[GameResult],
classOf[DrawReason],
classOf[Color],
classOf[Piece],
classOf[PieceType],
classOf[CastlingRights],
classOf[Square],
classOf[File],
classOf[Rank],
classOf[Move],
classOf[MoveType],
classOf[PromotionPiece],
),
)
class NativeReflectionConfig
@@ -0,0 +1,3 @@
package de.nowchess.io.service.dto
case class ImportFenRequest(fen: String)
@@ -0,0 +1,3 @@
package de.nowchess.io.service.dto
case class ImportPgnRequest(pgn: String)
@@ -0,0 +1,3 @@
package de.nowchess.io.service.dto
case class IoErrorDto(code: String, message: String)
@@ -0,0 +1,77 @@
package de.nowchess.io.service.resource
import de.nowchess.api.game.GameContext
import de.nowchess.io.fen.{FenExporter, FenParser}
import de.nowchess.io.pgn.{PgnExporter, PgnParser}
import de.nowchess.io.service.dto.{ImportFenRequest, ImportPgnRequest, IoErrorDto}
import io.smallrye.mutiny.Uni
import jakarta.enterprise.context.ApplicationScoped
import jakarta.ws.rs.*
import jakarta.ws.rs.core.{MediaType, Response}
import org.eclipse.microprofile.openapi.annotations.Operation
import org.eclipse.microprofile.openapi.annotations.media.{Content, Schema}
import org.eclipse.microprofile.openapi.annotations.responses.{APIResponse, APIResponses}
import org.eclipse.microprofile.openapi.annotations.tags.Tag
@Path("/io")
@ApplicationScoped
@Tag(name = "IO", description = "Chess notation import and export")
class IoResource:
@POST
@Path("/import/fen")
@Consumes(Array(MediaType.APPLICATION_JSON))
@Produces(Array(MediaType.APPLICATION_JSON))
@Operation(summary = "Import FEN", description = "Parse a FEN string into a GameContext")
@APIResponses(
Array(
new APIResponse(responseCode = "200", description = "Parsed GameContext"),
new APIResponse(responseCode = "400", description = "Invalid FEN"),
),
)
def importFen(body: ImportFenRequest): Uni[Response] =
Uni.createFrom().item {
FenParser.parseFen(body.fen) match
case Left(err) =>
Response.status(400).entity(IoErrorDto("INVALID_FEN", err)).build()
case Right(ctx) =>
Response.ok(ctx).build()
}
@POST
@Path("/import/pgn")
@Consumes(Array(MediaType.APPLICATION_JSON))
@Produces(Array(MediaType.APPLICATION_JSON))
@Operation(summary = "Import PGN", description = "Parse a PGN string into a GameContext")
@APIResponses(
Array(
new APIResponse(responseCode = "200", description = "Parsed GameContext"),
new APIResponse(responseCode = "400", description = "Invalid PGN"),
),
)
def importPgn(body: ImportPgnRequest): Uni[Response] =
Uni.createFrom().item {
PgnParser.importGameContext(body.pgn) match
case Left(err) =>
Response.status(400).entity(IoErrorDto("INVALID_PGN", err)).build()
case Right(ctx) =>
Response.ok(ctx).build()
}
@POST
@Path("/export/fen")
@Consumes(Array(MediaType.APPLICATION_JSON))
@Produces(Array(MediaType.TEXT_PLAIN))
@Operation(summary = "Export FEN", description = "Serialize a GameContext to FEN notation")
@APIResponse(responseCode = "200", description = "FEN string")
def exportFen(ctx: GameContext): Uni[Response] =
Uni.createFrom().item(Response.ok(FenExporter.exportGameContext(ctx)).build())
@POST
@Path("/export/pgn")
@Consumes(Array(MediaType.APPLICATION_JSON))
@Produces(Array("application/x-chess-pgn"))
@Operation(summary = "Export PGN", description = "Serialize a GameContext to PGN notation")
@APIResponse(responseCode = "200", description = "PGN text")
def exportPgn(ctx: GameContext): Uni[Response] =
Uni.createFrom().item(Response.ok(PgnExporter.exportGameContext(ctx)).build())