From 2fd763d511941fae137959be638484e347fbb173 Mon Sep 17 00:00:00 2001 From: Sarah Sporck Date: Wed, 15 Mar 2023 09:17:28 +0100 Subject: [PATCH] 856: Add health endpoint --- .../common/webservice/HealthHandler.kt | 25 +++++++++++++++++++ .../backend/common/webservice/WebService.kt | 4 +++ 2 files changed, 29 insertions(+) create mode 100644 backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/HealthHandler.kt diff --git a/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/HealthHandler.kt b/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/HealthHandler.kt new file mode 100644 index 000000000..67172e9a7 --- /dev/null +++ b/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/HealthHandler.kt @@ -0,0 +1,25 @@ +package app.ehrenamtskarte.backend.application.webservice + +import app.ehrenamtskarte.backend.config.BackendConfiguration +import app.ehrenamtskarte.backend.projects.database.ProjectEntity +import app.ehrenamtskarte.backend.projects.database.Projects +import io.javalin.http.Context +import org.jetbrains.exposed.exceptions.ExposedSQLException +import org.jetbrains.exposed.sql.transactions.transaction + +class HealthHandler(private val config: BackendConfiguration) { + fun handle(ctx: Context) { + try { + ctx.status(200) + transaction { + val projectIds = config.projects.map { it.id } + val projects = ProjectEntity.find { Projects.project inList projectIds } + if (projects.empty()) { + ctx.status(502) + } + } + } catch (exception: ExposedSQLException) { + ctx.status(502) + } + } +} diff --git a/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/WebService.kt b/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/WebService.kt index 9e8db6e44..3ee1c74b9 100644 --- a/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/WebService.kt +++ b/backend/src/main/kotlin/app/ehrenamtskarte/backend/common/webservice/WebService.kt @@ -1,6 +1,7 @@ package app.ehrenamtskarte.backend.common.webservice import app.ehrenamtskarte.backend.application.webservice.ApplicationAttachmentHandler +import app.ehrenamtskarte.backend.application.webservice.HealthHandler import app.ehrenamtskarte.backend.config.BackendConfiguration import app.ehrenamtskarte.backend.map.webservice.MapStyleHandler import io.javalin.Javalin @@ -50,6 +51,7 @@ class WebService { val graphQLHandler = GraphQLHandler(config) val mapStyleHandler = MapStyleHandler(config) val applicationHandler = ApplicationAttachmentHandler(applicationData) + val healthHandler = HealthHandler(config) app.post("/") { ctx -> if (!production) { @@ -71,6 +73,8 @@ class WebService { applicationHandler.handle(ctx) } + app.get("/health") { ctx -> healthHandler.handle(ctx) } + app.start(host, port) println("Server is running at http://$host:$port") println("Goto http://$host:$port/graphiql/ for a graphical editor")