HttpServer working with GET routes
This commit is contained in:
13
Web/WebSocket/websocket/server/src/health/HealthCheck.ts
Normal file
13
Web/WebSocket/websocket/server/src/health/HealthCheck.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type {HealthStatus} from './IHealthCheck';
|
||||
|
||||
export default class HealthCheck {
|
||||
public checkHealth(): HealthStatus {
|
||||
return {
|
||||
status: 'ok',
|
||||
environment: 'development',
|
||||
app: 'websocket-server',
|
||||
version: '0.0.1',
|
||||
zone: 'us-central1'
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import type {Request, Response} from 'express';
|
||||
import type IRoute from '../net/http/IRoutes';
|
||||
import IHealthCheck from './IHealthCheck';
|
||||
|
||||
export default class HealthCheckRoute implements IRoute {
|
||||
private readonly _healthCheck: IHealthCheck;
|
||||
|
||||
constructor(healthCheck: IHealthCheck) {
|
||||
this._healthCheck = healthCheck;
|
||||
}
|
||||
|
||||
public getGETRoutes() {
|
||||
console.log('[HealthCheckRoute] getGETRoutes called');
|
||||
const routes = {
|
||||
'/ok.html': (req, res) => this.externalReadiness.call(this, req, res),
|
||||
'/ping': (req, res) => this.ping.call(this, req, res)
|
||||
};
|
||||
console.log('[HealthCheckRoute] returning routes:', Object.keys(routes));
|
||||
return routes;
|
||||
}
|
||||
|
||||
public getPOSTRoutes() {
|
||||
return {};
|
||||
}
|
||||
|
||||
public getPUTRoutes() {
|
||||
return {};
|
||||
}
|
||||
|
||||
public getPATCHRoutes() {
|
||||
return {};
|
||||
}
|
||||
|
||||
public getDELETERoutes() {
|
||||
return {};
|
||||
}
|
||||
|
||||
private externalReadiness(_req: Request, res: Response) {
|
||||
console.log('[HealthCheckRoute] External readiness');
|
||||
res.setHeader('Cache-Control', 'public, max-age=0, no-cache, no-store');
|
||||
|
||||
try {
|
||||
const result = this._healthCheck.checkHealth();
|
||||
|
||||
if (!result || !result.status) {
|
||||
return res.status(500).end();
|
||||
}
|
||||
|
||||
switch (result.status) {
|
||||
case 'ok':
|
||||
case 'draining':
|
||||
case 'draining2':
|
||||
case 'disabled':
|
||||
return res.status(200).json(result);
|
||||
case 'starting':
|
||||
case 'drained':
|
||||
case 'stopped':
|
||||
return res.status(503).json(result);
|
||||
default:
|
||||
return res.status(500).json(result);
|
||||
}
|
||||
} catch {
|
||||
return res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
private ping(_req: Request, res: Response) {
|
||||
console.log('[HealthCheckRoute] Ping handler called from [%s]', _req.ip);
|
||||
|
||||
res.status(200).send({status: 'pong'}).end();
|
||||
}
|
||||
}
|
||||
11
Web/WebSocket/websocket/server/src/health/IHealthCheck.ts
Normal file
11
Web/WebSocket/websocket/server/src/health/IHealthCheck.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export type HealthStatus = {
|
||||
status: 'ok' | 'draining' | 'draining2' | 'disabled' | 'starting' | 'drained' | 'stopped';
|
||||
environment: string;
|
||||
app: string;
|
||||
version: string;
|
||||
zone: string;
|
||||
};
|
||||
|
||||
export default interface IHealthCheck {
|
||||
checkHealth(): HealthStatus;
|
||||
}
|
||||
Reference in New Issue
Block a user