import config from "config"; import {NextFunction, Request, Response, Router} from "express"; import ApplicationComponent from "../ApplicationComponent.js"; import {ServiceUnavailableHttpError} from "../HttpError.js"; export default class MaintenanceComponent extends ApplicationComponent { public async initRoutes(router: Router): Promise { router.use((req: Request, res: Response, next: NextFunction) => { if (res.headersSent) { return next(); } if (!this.getApp().isReady()) { res.header({'Retry-After': 60}); res.locals.refresh_after = 5; throw new ServiceUnavailableHttpError(`${config.get('app.name')} is readying up. Please wait a few seconds...`); } 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.`); } } next(); }); } }