forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.ts
43 lines (35 loc) · 1.17 KB
/
health.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
}
}