Skip to content

Commit

Permalink
Merge pull request #861 from digitalfabrik/856-add-health-check-endpoint
Browse files Browse the repository at this point in the history
856: Add health check endpoint
  • Loading branch information
sarahsporck authored Mar 15, 2023
2 parents 1002ca2 + 2fd763d commit 03fec1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand Down

0 comments on commit 03fec1b

Please sign in to comment.