Extract status API routes
This commit is contained in:
@@ -1,36 +1,15 @@
|
|||||||
package de.ruvnox.tactical
|
package de.ruvnox.tactical
|
||||||
|
|
||||||
import io.ktor.http.ContentType
|
import de.ruvnox.tactical.api.statusRoutes
|
||||||
import io.ktor.server.application.Application
|
import io.ktor.server.application.Application
|
||||||
import io.ktor.server.application.install
|
import io.ktor.server.application.install
|
||||||
import io.ktor.server.engine.embeddedServer
|
import io.ktor.server.engine.embeddedServer
|
||||||
import io.ktor.server.netty.Netty
|
import io.ktor.server.netty.Netty
|
||||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||||
import io.ktor.server.response.respond
|
|
||||||
import io.ktor.server.response.respondText
|
|
||||||
import io.ktor.server.routing.get
|
|
||||||
import io.ktor.server.routing.routing
|
import io.ktor.server.routing.routing
|
||||||
import io.ktor.serialization.kotlinx.json.json
|
import io.ktor.serialization.kotlinx.json.json
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class ServiceStatusResponse(
|
|
||||||
val service: String,
|
|
||||||
val status: String,
|
|
||||||
val version: String,
|
|
||||||
val database: DatabaseStatusResponse
|
|
||||||
)
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class DatabaseStatusResponse(
|
|
||||||
val status: String,
|
|
||||||
val host: String,
|
|
||||||
val port: Int,
|
|
||||||
val name: String,
|
|
||||||
val latencyMs: Long?
|
|
||||||
)
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val serverHost = System.getenv("SERVER_HOST") ?: "0.0.0.0"
|
val serverHost = System.getenv("SERVER_HOST") ?: "0.0.0.0"
|
||||||
val serverPort = System.getenv("SERVER_PORT")?.toIntOrNull() ?: 8080
|
val serverPort = System.getenv("SERVER_PORT")?.toIntOrNull() ?: 8080
|
||||||
@@ -55,41 +34,6 @@ fun Application.module() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/") {
|
statusRoutes()
|
||||||
call.respondText(
|
|
||||||
text = "RUVNOX Tactical API",
|
|
||||||
contentType = ContentType.Text.Plain
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
get("/health") {
|
|
||||||
call.respondText(
|
|
||||||
text = "RUVNOX Tactical server online",
|
|
||||||
contentType = ContentType.Text.Plain
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
get("/api/v1/status") {
|
|
||||||
call.respond(
|
|
||||||
ServiceStatusResponse(
|
|
||||||
service = "ruvnox-tactical-server",
|
|
||||||
status = "online",
|
|
||||||
version = "0.1.0-SNAPSHOT",
|
|
||||||
database = checkDatabaseStatus()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkDatabaseStatus(): DatabaseStatusResponse {
|
|
||||||
val result = Database.check()
|
|
||||||
|
|
||||||
return DatabaseStatusResponse(
|
|
||||||
status = result.status,
|
|
||||||
host = result.host,
|
|
||||||
port = result.port,
|
|
||||||
name = result.name,
|
|
||||||
latencyMs = result.latencyMs
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package de.ruvnox.tactical.api
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ServiceStatusResponse(
|
||||||
|
val service: String,
|
||||||
|
val status: String,
|
||||||
|
val version: String,
|
||||||
|
val database: DatabaseStatusResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class DatabaseStatusResponse(
|
||||||
|
val status: String,
|
||||||
|
val host: String,
|
||||||
|
val port: Int,
|
||||||
|
val name: String,
|
||||||
|
val latencyMs: Long?
|
||||||
|
)
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package de.ruvnox.tactical.api
|
||||||
|
|
||||||
|
import de.ruvnox.tactical.Database
|
||||||
|
import io.ktor.http.ContentType
|
||||||
|
import io.ktor.server.response.respond
|
||||||
|
import io.ktor.server.response.respondText
|
||||||
|
import io.ktor.server.routing.Route
|
||||||
|
import io.ktor.server.routing.get
|
||||||
|
|
||||||
|
fun Route.statusRoutes() {
|
||||||
|
get("/") {
|
||||||
|
call.respondText(
|
||||||
|
text = "RUVNOX Tactical API",
|
||||||
|
contentType = ContentType.Text.Plain
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/health") {
|
||||||
|
call.respondText(
|
||||||
|
text = "RUVNOX Tactical server online",
|
||||||
|
contentType = ContentType.Text.Plain
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/api/v1/status") {
|
||||||
|
val database = Database.check()
|
||||||
|
|
||||||
|
call.respond(
|
||||||
|
ServiceStatusResponse(
|
||||||
|
service = "ruvnox-tactical-server",
|
||||||
|
status = "online",
|
||||||
|
version = "0.1.0-SNAPSHOT",
|
||||||
|
database = DatabaseStatusResponse(
|
||||||
|
status = database.status,
|
||||||
|
host = database.host,
|
||||||
|
port = database.port,
|
||||||
|
name = database.name,
|
||||||
|
latencyMs = database.latencyMs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user