Added /healthz JSON response for heartbeat data. #1940 (#1984)

This commit is contained in:
Jacob Goldman
2020-08-31 11:29:12 -04:00
committed by GitHub
parent de41646fc4
commit 75c8fdeed2
3 changed files with 46 additions and 7 deletions

32
src/node/app/health.ts Normal file
View File

@@ -0,0 +1,32 @@
import * as http from "http"
import { HttpCode, HttpError } from "../../common/http"
import { HttpProvider, HttpResponse, Route, Heart, HttpProviderOptions } from "../http"
/**
* Check the heartbeat.
*/
export class HealthHttpProvider extends HttpProvider {
public constructor(options: HttpProviderOptions, private readonly heart: Heart) {
super(options)
}
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
if (!this.authenticated(request)) {
if (this.isRoot(route)) {
return { redirect: "/login", query: { to: route.fullPath } }
}
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
}
const result = {
cache: false,
mime: "application/json",
content: {
status: this.heart.alive() ? "alive" : "expired",
lastHeartbeat: this.heart.lastHeartbeat,
},
}
return result
}
}