Add health websocket

This is used by some of our services.
This commit is contained in:
Asher 2021-01-28 12:47:50 -06:00
parent f136a60093
commit b13db3124b
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
3 changed files with 21 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { Router } from "express" import { Router } from "express"
import { wss, Router as WsRouter } from "../wsRouter"
export const router = Router() export const router = Router()
@ -8,3 +9,19 @@ router.get("/", (req, res) => {
lastHeartbeat: req.heart.lastHeartbeat, lastHeartbeat: req.heart.lastHeartbeat,
}) })
}) })
export const wsRouter = WsRouter()
wsRouter.ws("/", async (req) => {
wss.handleUpgrade(req, req.socket, req.head, (ws) => {
ws.on("message", () => {
ws.send(
JSON.stringify({
event: "health",
status: req.heart.alive() ? "alive" : "expired",
lastHeartbeat: req.heart.lastHeartbeat,
}),
)
})
})
})

View File

@ -133,6 +133,7 @@ export const register = async (
wsApp.use("/vscode", vscode.wsRouter.router) wsApp.use("/vscode", vscode.wsRouter.router)
app.use("/healthz", health.router) app.use("/healthz", health.router)
wsApp.use("/healthz", health.wsRouter.router)
if (args.auth === AuthType.Password) { if (args.auth === AuthType.Password) {
app.use("/login", login.router) app.use("/login", login.router)

View File

@ -1,6 +1,7 @@
import * as express from "express" import * as express from "express"
import * as expressCore from "express-serve-static-core" import * as expressCore from "express-serve-static-core"
import * as http from "http" import * as http from "http"
import Websocket from "ws"
import * as pluginapi from "../../typings/pluginapi" import * as pluginapi from "../../typings/pluginapi"
export const handleUpgrade = (app: express.Express, server: http.Server): void => { export const handleUpgrade = (app: express.Express, server: http.Server): void => {
@ -48,3 +49,5 @@ export class WebsocketRouter {
export function Router(): WebsocketRouter { export function Router(): WebsocketRouter {
return new WebsocketRouter() return new WebsocketRouter()
} }
export const wss = new Websocket.Server({ noServer: true })