Skip to content

Commit b13db31

Browse files
committed
Add health websocket
This is used by some of our services.
1 parent f136a60 commit b13db31

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/node/routes/health.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Router } from "express"
2+
import { wss, Router as WsRouter } from "../wsRouter"
23

34
export const router = Router()
45

@@ -8,3 +9,19 @@ router.get("/", (req, res) => {
89
lastHeartbeat: req.heart.lastHeartbeat,
910
})
1011
})
12+
13+
export const wsRouter = WsRouter()
14+
15+
wsRouter.ws("/", async (req) => {
16+
wss.handleUpgrade(req, req.socket, req.head, (ws) => {
17+
ws.on("message", () => {
18+
ws.send(
19+
JSON.stringify({
20+
event: "health",
21+
status: req.heart.alive() ? "alive" : "expired",
22+
lastHeartbeat: req.heart.lastHeartbeat,
23+
}),
24+
)
25+
})
26+
})
27+
})

src/node/routes/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export const register = async (
133133
wsApp.use("/vscode", vscode.wsRouter.router)
134134

135135
app.use("/healthz", health.router)
136+
wsApp.use("/healthz", health.wsRouter.router)
136137

137138
if (args.auth === AuthType.Password) {
138139
app.use("/login", login.router)

src/node/wsRouter.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as express from "express"
22
import * as expressCore from "express-serve-static-core"
33
import * as http from "http"
4+
import Websocket from "ws"
45
import * as pluginapi from "../../typings/pluginapi"
56

67
export const handleUpgrade = (app: express.Express, server: http.Server): void => {
@@ -48,3 +49,5 @@ export class WebsocketRouter {
4849
export function Router(): WebsocketRouter {
4950
return new WebsocketRouter()
5051
}
52+
53+
export const wss = new Websocket.Server({ noServer: true })

0 commit comments

Comments
 (0)