From 38409988c0ed3e171e77ee691154775b36ed1e00 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 16 Jan 2025 23:25:34 +0100 Subject: feat: Add basic server implementation --- .../moe/nea/ledger/server/core/Application.kt | 34 ++++++++++++++++++++++ .../moe/nea/ledger/server/core/api/BaseApi.kt | 25 ++++++++++++++++ .../kotlin/moe/nea/ledger/server/core/model.kt | 30 +++++++++++++++++++ server/core/src/main/resources/application.conf | 10 +++++++ 4 files changed, 99 insertions(+) create mode 100644 server/core/src/main/kotlin/moe/nea/ledger/server/core/Application.kt create mode 100644 server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt create mode 100644 server/core/src/main/kotlin/moe/nea/ledger/server/core/model.kt create mode 100644 server/core/src/main/resources/application.conf (limited to 'server/core/src/main') diff --git a/server/core/src/main/kotlin/moe/nea/ledger/server/core/Application.kt b/server/core/src/main/kotlin/moe/nea/ledger/server/core/Application.kt new file mode 100644 index 0000000..0ea6ed3 --- /dev/null +++ b/server/core/src/main/kotlin/moe/nea/ledger/server/core/Application.kt @@ -0,0 +1,34 @@ +package moe.nea.ledger.server.core + +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.netty.EngineMain +import io.ktor.server.plugins.compression.Compression +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.routing.route +import io.ktor.server.routing.routing +import moe.nea.ledger.database.Database +import moe.nea.ledger.server.core.api.apiRouting +import java.io.File + +fun main(args: Array) { + EngineMain.main(args) +} + + +fun Application.module() { + install(Compression) + install(ContentNegotiation) { + json() +// cbor() + } + val database = Database(File(System.getProperty("ledger.databasefolder"))) + database.loadAndUpgrade() + routing { + route("/api") { + this.apiRouting(database) + } + } +} + diff --git a/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt b/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt new file mode 100644 index 0000000..264f74b --- /dev/null +++ b/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt @@ -0,0 +1,25 @@ +package moe.nea.ledger.server.core.api + +import io.ktor.server.response.respond +import io.ktor.server.response.respondText +import io.ktor.server.routing.Route +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import moe.nea.ledger.database.DBLogEntry +import moe.nea.ledger.database.Database +import moe.nea.ledger.server.core.Profile + +fun Route.apiRouting(database: Database) { + get("/") { + call.respondText("K") + } + get("/profiles") { + val profiles = DBLogEntry.from(database.connection) + .select(DBLogEntry.playerId, DBLogEntry.profileId) + .distinct() + .map { + Profile(it[DBLogEntry.playerId], it[DBLogEntry.profileId]) + } + call.respond(profiles) + } +} diff --git a/server/core/src/main/kotlin/moe/nea/ledger/server/core/model.kt b/server/core/src/main/kotlin/moe/nea/ledger/server/core/model.kt new file mode 100644 index 0000000..a27a729 --- /dev/null +++ b/server/core/src/main/kotlin/moe/nea/ledger/server/core/model.kt @@ -0,0 +1,30 @@ +@file:UseSerializers(UUIDSerializer::class) + +package moe.nea.ledger.server.core + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.UseSerializers +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import java.util.UUID + +object UUIDSerializer : KSerializer { + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("LedgerUUID", PrimitiveKind.STRING) + + override fun deserialize(decoder: kotlinx.serialization.encoding.Decoder): UUID { + return UUID.fromString(decoder.decodeString()) + } + + override fun serialize(encoder: kotlinx.serialization.encoding.Encoder, value: UUID) { + encoder.encodeString(value.toString()) + } +} + +@Serializable +data class Profile( + val playerId: UUID, + val profileId: UUID, +) \ No newline at end of file diff --git a/server/core/src/main/resources/application.conf b/server/core/src/main/resources/application.conf new file mode 100644 index 0000000..386ffd3 --- /dev/null +++ b/server/core/src/main/resources/application.conf @@ -0,0 +1,10 @@ +ktor { + application { + modules = [ + moe.nea.ledger.server.core.ApplicationKt.module + ] + } + deployment { + port = 8080 + } +} \ No newline at end of file -- cgit