code-server/src/node/routes/health.ts

29 lines
668 B
TypeScript
Raw Normal View History

2020-10-21 07:05:58 +08:00
import { Router } from "express"
import { wss, Router as WsRouter } from "../wsRouter"
2020-10-21 07:05:58 +08:00
export const router = Router()
2020-10-21 07:05:58 +08:00
router.get("/", (req, res) => {
res.json({
status: req.heart.alive() ? "alive" : "expired",
lastHeartbeat: req.heart.lastHeartbeat,
})
})
export const wsRouter = WsRouter()
wsRouter.ws("/", async (req) => {
wss.handleUpgrade(req, req.ws, req.head, (ws) => {
ws.addEventListener("message", () => {
ws.send(
JSON.stringify({
event: "health",
status: req.heart.alive() ? "alive" : "expired",
lastHeartbeat: req.heart.lastHeartbeat,
}),
)
})
req.ws.resume()
})
})