Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions src/node/app/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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)
}

private alive(): Boolean {
const now = Date.now()
return (now - this.heart.lastHeartbeat < this.heart.heartbeatInterval)
}

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.alive()) ? 'alive' : 'expired',
lastHeartbeat: this.heart.lastHeartbeat

}
}

return result

}
}
2 changes: 2 additions & 0 deletions src/node/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ProxyHttpProvider } from "./app/proxy"
import { StaticHttpProvider } from "./app/static"
import { UpdateHttpProvider } from "./app/update"
import { VscodeHttpProvider } from "./app/vscode"
import { HealthHttpProvider } from "./app/health"
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli"
import { AuthType, HttpServer, HttpServerOptions } from "./http"
import { loadPlugins } from "./plugin"
Expand Down Expand Up @@ -78,6 +79,7 @@ const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void>
httpServer.registerHttpProvider("/proxy", ProxyHttpProvider)
httpServer.registerHttpProvider("/login", LoginHttpProvider, args.config!, envPassword)
httpServer.registerHttpProvider("/static", StaticHttpProvider)
httpServer.registerHttpProvider("/healthz", HealthHttpProvider, httpServer.heart)

await loadPlugins(httpServer, args)

Expand Down
9 changes: 5 additions & 4 deletions src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ export abstract class HttpProvider {
*/
export class Heart {
private heartbeatTimer?: NodeJS.Timeout
private heartbeatInterval = 60000
private lastHeartbeat = 0
public heartbeatInterval = 60000
public lastHeartbeat = 0

public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise<boolean>) {}

Expand Down Expand Up @@ -457,7 +457,7 @@ export class HttpServer {
private listenPromise: Promise<string | null> | undefined
public readonly protocol: "http" | "https"
private readonly providers = new Map<string, HttpProvider>()
private readonly heart: Heart
public readonly heart: Heart
private readonly socketProvider = new SocketProxyProvider()

/**
Expand Down Expand Up @@ -602,8 +602,9 @@ export class HttpServer {
}

private onRequest = async (request: http.IncomingMessage, response: http.ServerResponse): Promise<void> => {
this.heart.beat()
const route = this.parseUrl(request)
if (route.providerBase !== '/healthz')
this.heart.beat()
const write = (payload: HttpResponse): void => {
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
"Content-Type": payload.mime || getMediaMime(payload.filePath),
Expand Down