diff options
author | Linnea Gräf <nea@nea.moe> | 2025-01-16 23:25:34 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-01-16 23:25:34 +0100 |
commit | 38409988c0ed3e171e77ee691154775b36ed1e00 (patch) | |
tree | 64c15e6af17c27fc1b995b2790e7afc654eefb45 /server/core/src/main | |
parent | 5daefca0b50e563152462f0ee1a963b8c98c33f0 (diff) | |
download | LocalTransactionLedger-38409988c0ed3e171e77ee691154775b36ed1e00.tar.gz LocalTransactionLedger-38409988c0ed3e171e77ee691154775b36ed1e00.tar.bz2 LocalTransactionLedger-38409988c0ed3e171e77ee691154775b36ed1e00.zip |
feat: Add basic server implementation
Diffstat (limited to 'server/core/src/main')
4 files changed, 99 insertions, 0 deletions
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<String>) { + 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<UUID> { + 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 |