swaf/src/components/MaintenanceComponent.ts

32 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-06-27 18:06:39 +02:00
import config from "config";
import {NextFunction, Request, Response, Router} from "express";
import ApplicationComponent from "../ApplicationComponent.js";
import {ServiceUnavailableHttpError} from "../HttpError.js";
2020-04-22 15:52:17 +02:00
export default class MaintenanceComponent extends ApplicationComponent {
public async initRoutes(router: Router): Promise<void> {
2020-04-22 15:52:17 +02:00
router.use((req: Request, res: Response, next: NextFunction) => {
if (res.headersSent) {
return next();
}
if (!this.getApp().isReady()) {
2020-04-22 15:52:17 +02:00
res.header({'Retry-After': 60});
res.locals.refresh_after = 5;
2020-06-27 18:06:39 +02:00
throw new ServiceUnavailableHttpError(`${config.get('app.name')} is readying up. Please wait a few seconds...`);
2020-04-22 15:52:17 +02:00
}
for (const component of this.getApp().getComponents()) {
if (!component.isReady()) {
res.header({'Retry-After': 60});
res.locals.refresh_after = 30;
throw new ServiceUnavailableHttpError(`${config.get('app.name')} is unavailable due to failure of dependent services.`);
}
2020-04-22 15:52:17 +02:00
}
next();
});
}
}