Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

856: Add health check endpoint #861

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sarahsporck marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (exception: ExposedSQLException) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also make sense to change this to exception: Exception, to increase the resilience of this endpoint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you want. Probably any other exception will also return some status != 200.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. Otherwise it will return 500 Internal Server Error

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