Skip to content

Commit

Permalink
Merge pull request #59 from MarshallWace/health-checks
Browse files Browse the repository at this point in the history
adding health-check endpoints
  • Loading branch information
mw-lb authored Oct 8, 2024
2 parents de7b50a + fb183f1 commit 1d72481
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ fun Application.configureMonitoring() {
}
}

fun Application.configureHealthCheck(name: String, isHealth: () -> Boolean) {
routing {
get("/$name", {
hidden = true
}) {
if (isHealth()) {
call.respond(HttpStatusCode.OK)
} else {
call.respond(HttpStatusCode.ServiceUnavailable)
}
}
}
}

fun Application.configureHealthyHealthCheck(name: String) = configureHealthCheck(name) { true }

fun Application.configureHTTP() {
install(Compression)
install(CORS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ fun Application.module(config: Config) {
configureCallLogging()
configureFrameworks(config)

log

environment.monitor.subscribe(ApplicationStopped) { application ->
application.environment.log.info("Ktor server has stopped")
// TODO can this be done in a better way?
Expand All @@ -47,4 +45,7 @@ fun Application.module(config: Config) {
}

configureRouting()

configureHealthyHealthCheck("liveness")
configureHealthyHealthCheck("readiness")
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ fun Application.module(config: Config) {
}

configureRouting()

configureHealthyHealthCheck("liveness")
configureHealthyHealthCheck("readiness")
}

0 comments on commit 1d72481

Please sign in to comment.