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 | |
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')
6 files changed, 135 insertions, 0 deletions
diff --git a/server/core/build.gradle.kts b/server/core/build.gradle.kts new file mode 100644 index 0000000..87f613a --- /dev/null +++ b/server/core/build.gradle.kts @@ -0,0 +1,31 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + application +} + +val ktor_version = "3.0.3" + +dependencies { + implementation(platform("io.ktor:ktor-bom:$ktor_version")) + implementation("io.ktor:ktor-server-netty") + implementation("io.ktor:ktor-server-status-pages") + implementation("io.ktor:ktor-server-content-negotiation") + implementation("io.ktor:ktor-server-openapi") + implementation("io.ktor:ktor-serialization-kotlinx-json") + implementation("io.ktor:ktor-server-compression") + implementation(project(":database:impl")) + + runtimeOnly("ch.qos.logback:logback-classic:1.5.16") + runtimeOnly("org.xerial:sqlite-jdbc:3.45.3.0") +} + +java { + toolchain.languageVersion.set(JavaLanguageVersion.of(21)) +} +application { + val isDevelopment: Boolean = project.ext.has("development") + applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment", + "-Dledger.databasefolder=${project(":mod").file("run/money-ledger").absoluteFile}") + mainClass.set("moe.nea.ledger.server.core.ApplicationKt") +} 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 diff --git a/server/core/test-requests/test-hello.http b/server/core/test-requests/test-hello.http new file mode 100644 index 0000000..3ddf352 --- /dev/null +++ b/server/core/test-requests/test-hello.http @@ -0,0 +1,5 @@ +### GET request to example server +GET localhost:8080/ + +### GET profiles +GET localhost:8080/api/profiles
\ No newline at end of file |