Skip to content

Commit d3c3405

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

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/node/routes/health.ts

Lines changed: 17 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const register = async (
113113
wsApp.use("/vscode", vscode.wsRouter.router)
114114

115115
app.use("/healthz", health.router)
116+
wsApp.use("/healthz", health.wsRouter.router)
116117

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

src/node/wsRouter.ts

Lines changed: 3 additions & 0 deletions
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)