diff options
author | Linnea Gräf <nea@nea.moe> | 2025-01-17 15:30:04 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-01-17 15:30:04 +0100 |
commit | aa7e28e799ce19f04c6c33782ea8d25ef4c3bb98 (patch) | |
tree | a84f850c1f88ce3c8a16b2fdb5de52fba3ad0edb /server/core | |
parent | f49de9677285ba6287d049a6187ff00bf90ee77f (diff) | |
download | LocalTransactionLedger-aa7e28e799ce19f04c6c33782ea8d25ef4c3bb98.tar.gz LocalTransactionLedger-aa7e28e799ce19f04c6c33782ea8d25ef4c3bb98.tar.bz2 LocalTransactionLedger-aa7e28e799ce19f04c6c33782ea8d25ef4c3bb98.zip |
feat(Server): Add frontend starter
Diffstat (limited to 'server/core')
3 files changed, 33 insertions, 4 deletions
diff --git a/server/core/build.gradle.kts b/server/core/build.gradle.kts index f254df1..ad6c80e 100644 --- a/server/core/build.gradle.kts +++ b/server/core/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { implementation("io.ktor:ktor-server-content-negotiation") implementation("io.ktor:ktor-serialization-kotlinx-json") implementation("io.ktor:ktor-server-compression") + implementation("io.ktor:ktor-server-cors") implementation("sh.ondr:kotlin-json-schema:0.1.1") implementation(project(":database:impl")) implementation(project(":server:swagger")) 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 index eef43d6..a177b05 100644 --- 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 @@ -6,6 +6,7 @@ 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.plugins.cors.routing.CORS import io.ktor.server.response.respondRedirect import io.ktor.server.routing.get import io.ktor.server.routing.route @@ -46,6 +47,9 @@ fun Application.module() { }) // cbor() } + install(CORS) { + anyHost() + } val database = Database(File(System.getProperty("ledger.databasefolder"))) database.loadAndUpgrade() routing { 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 index d407f0b..c58763f 100644 --- 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 @@ -1,17 +1,21 @@ package moe.nea.ledger.server.core.api +import io.ktor.http.Url +import io.ktor.http.toURI import io.ktor.server.response.respond -import io.ktor.server.response.respondText import io.ktor.server.routing.Route import io.ktor.server.routing.get +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.decodeFromStream import moe.nea.ledger.database.DBLogEntry import moe.nea.ledger.database.Database import moe.nea.ledger.server.core.Profile +import sh.ondr.jsonschema.jsonSchema fun Route.apiRouting(database: Database) { - get("/") { - call.respondText("K") - } get("/profiles") { val profiles = DBLogEntry.from(database.connection) .select(DBLogEntry.playerId, DBLogEntry.profileId) @@ -28,10 +32,30 @@ fun Route.apiRouting(database: Database) { schema<List<Profile>>() } } + @OptIn(DelicateCoroutinesApi::class) + val itemNames = GlobalScope.async { + val itemNamesUrl = + Url("https://github.com/nea89o/ledger-auxiliary-data/raw/refs/heads/master/data/item_names.json") + Json.decodeFromStream<Map<String, String>>(itemNamesUrl.toURI().toURL().openStream()) + } + get("/item") { + val itemIds = call.queryParameters.getAll("itemId")?.toSet() ?: emptySet() + val itemNameMap = itemNames.await() + call.respond(itemIds.associateWith { itemNameMap[it] }) + }.docs { + summary = "Get item names for item ids" + operationId = "getItemNames" + tag(Tags.HYPIXEL) + queryParameter<List<String>>("itemId") + respondsOk { + schema<Map<String, String?>>() + } + } } enum class Tags : IntoTag { PROFILE, + HYPIXEL, MANAGEMENT, ; |