aboutsummaryrefslogtreecommitdiff
path: root/server/swagger/src/main/kotlin
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-17 14:21:04 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-17 14:21:04 +0100
commitf49de9677285ba6287d049a6187ff00bf90ee77f (patch)
tree90fb77cac434106f6fa183731a4dd681191190ed /server/swagger/src/main/kotlin
parent8a9f076d826cb93dcce292180de6fc2be66a7872 (diff)
downloadLocalTransactionLedger-f49de9677285ba6287d049a6187ff00bf90ee77f.tar.gz
LocalTransactionLedger-f49de9677285ba6287d049a6187ff00bf90ee77f.tar.bz2
LocalTransactionLedger-f49de9677285ba6287d049a6187ff00bf90ee77f.zip
feat: Add profile info
Diffstat (limited to 'server/swagger/src/main/kotlin')
-rw-r--r--server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt27
1 files changed, 25 insertions, 2 deletions
diff --git a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt
index fd63f81..5d222ee 100644
--- a/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt
+++ b/server/swagger/src/main/kotlin/moe/nea/ledger/server/core/api/docs.kt
@@ -7,6 +7,8 @@ import io.ktor.http.content.OutgoingContent
import io.ktor.http.defaultForFilePath
import io.ktor.server.application.ApplicationCallPipeline
import io.ktor.server.application.BaseApplicationPlugin
+import io.ktor.server.application.host
+import io.ktor.server.application.port
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.HttpMethodRouteSelector
@@ -239,6 +241,12 @@ class Documentation(config: Configuration) {
override fun install(pipeline: ApplicationCallPipeline, configure: Configuration.() -> Unit): Documentation {
val config = Configuration().also(configure)
+ if (config.servers.isEmpty()) {
+ config.servers.add(Server(
+ "http://${pipeline.environment.config.host}:${pipeline.environment.config.port}",
+ "Server",
+ ))
+ }
val plugin = Documentation(config)
return plugin
}
@@ -246,6 +254,9 @@ class Documentation(config: Configuration) {
val info = config.info
var root: RoutingNode? = null
+ private set
+ val servers: List<Server> = config.servers
+
private val documentationNodes = mutableMapOf<DocumentationPath, DocumentationContext>()
fun createDocumentationNode(endpoint: DocumentationEndpoint) =
documentationNodes.getOrPut(endpoint.path) { DocumentationContext(endpoint.path) }
@@ -254,8 +265,7 @@ class Documentation(config: Configuration) {
private val openApiJson by lazy {
OpenApiModel(
info = info,
- // TODO: generate server list better
- servers = listOf(Server("http://localhost:8080", "Local Server")),
+ servers = servers,
paths = documentationNodes.map {
OpenApiPath(it.key.path) to it.value.intoJson()
}.toMap()
@@ -266,12 +276,18 @@ class Documentation(config: Configuration) {
return openApiJson
}
+ fun setRootNode(routingNode: RoutingNode) {
+ require(documentationNodes.isEmpty()) { "Cannot set API root node after routes have been documented: ${documentationNodes.keys}" }
+ this.root = routingNode
+ }
+
class Configuration {
var info: Info = Info(
title = "Example API Docs",
description = "Missing description",
version = "0.0.0"
)
+ val servers: MutableList<Server> = mutableListOf()
}
}
@@ -282,3 +298,10 @@ fun Route.docs(block: DocumentationOperationContext.() -> Unit) {
block(node)
}
+/**
+ * Mark this current routing node as API route. Note that this will not apply retroactively and all api requests must be declared relative to this one.
+ */
+fun Route.setApiRoot() {
+ plugin(Documentation).setRootNode(this as RoutingNode)
+}
+