140 lines
18 KiB
JavaScript
140 lines
18 KiB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
import express from 'express';
|
|
import { BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError } from "./HttpError";
|
|
import { lib } from "nunjucks";
|
|
import Logger from "./Logger";
|
|
import WebSocketListener from "./WebSocketListener";
|
|
var TemplateError = lib.TemplateError;
|
|
import Controller from "./Controller";
|
|
export default class Application {
|
|
constructor(version) {
|
|
this.controllers = [];
|
|
this.webSocketListeners = {};
|
|
this.components = [];
|
|
this.ready = false;
|
|
this.version = version;
|
|
}
|
|
use(thing) {
|
|
if (thing instanceof Controller) {
|
|
this.controllers.push(thing);
|
|
}
|
|
else if (thing instanceof WebSocketListener) {
|
|
const path = thing.path();
|
|
this.webSocketListeners[path] = thing;
|
|
Logger.info(`Added websocket listener on ${path}`);
|
|
}
|
|
else {
|
|
this.components.push(thing);
|
|
}
|
|
}
|
|
start() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
Logger.info(`${this.constructor.name} v${this.version} - hi`);
|
|
process.once('SIGINT', () => {
|
|
this.stop().catch(console.error);
|
|
});
|
|
// Register all components and alike
|
|
yield this.init();
|
|
// Init express
|
|
const app = express();
|
|
const router = express.Router({});
|
|
app.use(router);
|
|
// Error handler
|
|
app.use((err, req, res, next) => {
|
|
if (res.headersSent) {
|
|
return next(err);
|
|
}
|
|
let errorID;
|
|
let logStr = `${req.method} ${req.originalUrl} - `;
|
|
if (err instanceof BadRequestError || err instanceof ServiceUnavailableHttpError) {
|
|
logStr += `${err.errorCode} ${err.name}`;
|
|
errorID = Logger.silentError(err, logStr);
|
|
}
|
|
else {
|
|
errorID = Logger.error(err, logStr + `500 Internal Error`, err);
|
|
}
|
|
let httpError;
|
|
if (err instanceof HttpError) {
|
|
httpError = err;
|
|
}
|
|
else if (err instanceof TemplateError && err.cause instanceof HttpError) {
|
|
httpError = err.cause;
|
|
}
|
|
else {
|
|
httpError = new ServerError('Internal server error.', err);
|
|
}
|
|
res.status(httpError.errorCode);
|
|
res.format({
|
|
html: () => {
|
|
res.render('errors/' + httpError.errorCode + '.njk', {
|
|
error_code: httpError.errorCode,
|
|
error_message: httpError.message,
|
|
error_instructions: httpError.instructions,
|
|
error_id: errorID,
|
|
});
|
|
},
|
|
json: () => {
|
|
res.json({
|
|
status: 'error',
|
|
code: httpError.errorCode,
|
|
message: httpError.message,
|
|
instructions: httpError.instructions,
|
|
error_id: errorID,
|
|
});
|
|
},
|
|
default: () => {
|
|
res.type('txt').send(`${httpError.errorCode} - ${httpError.message}\n\n${httpError.instructions}\n\nError ID: ${errorID}`);
|
|
}
|
|
});
|
|
});
|
|
// Start all components
|
|
for (const component of this.components) {
|
|
yield component.start(app, router);
|
|
}
|
|
// Routes
|
|
this.routes(router);
|
|
this.ready = true;
|
|
});
|
|
}
|
|
stop() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
Logger.info('Stopping application...');
|
|
for (const component of this.components) {
|
|
yield component.stop();
|
|
}
|
|
Logger.info(`${this.constructor.name} v${this.version} - bye`);
|
|
});
|
|
}
|
|
routes(rootRouter) {
|
|
for (const controller of this.controllers) {
|
|
if (controller.hasGlobalHandlers()) {
|
|
controller.setupGlobalHandlers(rootRouter);
|
|
Logger.info(`Registered global middlewares for controller ${controller.constructor.name}`);
|
|
}
|
|
}
|
|
for (const controller of this.controllers) {
|
|
const router = express.Router();
|
|
controller.setupRoutes(router);
|
|
rootRouter.use(controller.getRoutesPrefix(), router);
|
|
Logger.info(`> Registered routes for controller ${controller.constructor.name}`);
|
|
}
|
|
rootRouter.use((req) => {
|
|
throw new NotFoundHttpError('page', req.originalUrl);
|
|
});
|
|
}
|
|
getWebSocketListeners() {
|
|
return this.webSocketListeners;
|
|
}
|
|
isReady() {
|
|
return this.ready;
|
|
}
|
|
}
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiLi8iLCJzb3VyY2VzIjpbIkFwcGxpY2F0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sT0FBa0QsTUFBTSxTQUFTLENBQUM7QUFDekUsT0FBTyxFQUNILGVBQWUsRUFDZixTQUFTLEVBQ1QsaUJBQWlCLEVBQ2pCLFdBQVcsRUFDWCwyQkFBMkIsRUFDOUIsTUFBTSxhQUFhLENBQUM7QUFDckIsT0FBTyxFQUFDLEdBQUcsRUFBQyxNQUFNLFVBQVUsQ0FBQztBQUM3QixPQUFPLE1BQU0sTUFBTSxVQUFVLENBQUM7QUFDOUIsT0FBTyxpQkFBaUIsTUFBTSxxQkFBcUIsQ0FBQztBQUVwRCxJQUFPLGFBQWEsR0FBRyxHQUFHLENBQUMsYUFBYSxDQUFDO0FBQ3pDLE9BQU8sVUFBVSxNQUFNLGNBQWMsQ0FBQztBQUV0QyxNQUFNLENBQUMsT0FBTyxPQUFnQixXQUFXO0lBUXJDLFlBQXNCLE9BQWU7UUFOcEIsZ0JBQVcsR0FBaUIsRUFBRSxDQUFDO1FBQy9CLHVCQUFrQixHQUF1QyxFQUFFLENBQUM7UUFDNUQsZUFBVSxHQUFnQyxFQUFFLENBQUM7UUFFdEQsVUFBSyxHQUFZLEtBQUssQ0FBQztRQUczQixJQUFJLENBQUMsT0FBTyxHQUFHLE9BQU8sQ0FBQztJQUMzQixDQUFDO0lBSVMsR0FBRyxDQUFDL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ChlcnIpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBsZXQgZXJyb3JJRDogc3RyaW5nO1xuXG4gICAgICAgICAgICBsZXQgbG9nU3RyID0gYCR7cmVxLm1ldGhvZH0gJHtyZXEub3JpZ2luYWxVcmx9IC0gYDtcbiAgICAgICAgICAgIGlmIChlcnIgaW5zdGFuY2VvZiBCYWRSZXF1ZXN0RXJyb3IgfHwgZXJyIGluc3RhbmNlb2YgU2VydmljZVVuYXZhaWxhYmxlSHR0cEVycm9yKSB7XG4gICAgICAgICAgICAgICAgbG9nU3RyICs9IGAke2Vyci5lcnJvckNvZGV9ICR7ZXJyLm5hbWV9YDtcbiAgICAgICAgICAgICAgICBlcnJvcklEID0gTG9nZ2VyLnNpbGVudEVycm9yKGVyciwgbG9nU3RyKTtcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgZXJyb3JJRCA9IExvZ2dlci5lcnJvcihlcnIsIGxvZ1N0ciArIGA1MDAgSW50ZXJuYWwgRXJyb3JgLCBlcnIpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBsZXQgaHR0cEVycm9yOiBIdHRwRXJyb3I7XG5cbiAgICAgICAgICAgIGlmIChlcnIgaW5zdGFuY2VvZiBIdHRwRXJyb3IpIHtcbiAgICAgICAgICAgICAgICBodHRwRXJyb3IgPSBlcnI7XG4gICAgICAgICAgICB9IGVsc2UgaWYgKGVyciBpbnN0YW5jZW9mIFRlbXBsYXRlRXJyb3IgJiYgZXJyLmNhdXNlIGluc3RhbmNlb2YgSHR0cEVycm9yKSB7XG4gICAgICAgICAgICAgICAgaHR0cEVycm9yID0gZXJyLmNhdXNlO1xuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICBodHRwRXJyb3IgPSBuZXcgU2VydmVyRXJyb3IoJ0ludGVybmFsIHNlcnZlciBlcnJvci4nLCBlcnIpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICByZXMuc3RhdHVzKGh0dHBFcnJvci5lcnJvckNvZGUpO1xuICAgICAgICAgICAgcmVzLmZvcm1hdCh7XG4gICAgICAgICAgICAgICAgaHRtbDogKCkgPT4ge1xuICAgICAgICAgICAgICAgICAgICByZXMucmVuZGVyKCdlcnJvcnMvJyArIGh0dHBFcnJvci5lcnJvckNvZGUgKyAnLm5qaycsIHtcbiAgICAgICAgICAgICAgICAgICAgICAgIGVycm9yX2NvZGU6IGh0dHBFcnJvci5lcnJvckNvZGUsXG4gICAgICAgICAgICAgICAgICAgICAgICBlcnJvcl9tZXNzYWdlOiBodHRwRXJyb3IubWVzc2FnZSxcbiAgICAgICAgICAgICAgICAgICAgICAgIGVycm9yX2luc3RydWN0aW9uczogaHR0cEVycm9yLmluc3RydWN0aW9ucyxcbiAgICAgICAgICAgICAgICAgICAgICAgIGVycm9yX2lkOiBlcnJvcklELFxuICAgICAgICAgICAgICAgICAgICB9KTtcbiAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICAgIGpzb246ICgpID0+IHtcbiAgICAgICAgICAgICAgICAgICAgcmVzLmpzb24oe1xuICAgICAgICAgICAgICAgICAgICAgICAgc3RhdHVzOiAnZXJyb3InLFxuICAgICAgICAgICAgICAgICAgICAgICAgY29kZTogaHR0cEVycm9yLmVycm9yQ29kZSxcbiAgICAgICAgICAgICAgICAgICAgICAgIG1lc3NhZ2U6IGh0dHBFcnJvci5tZXNzYWdlLFxuICAgICAgICAgICAgICAgICAgICAgICAgaW5zdHJ1Y3Rpb25zOiBodHRwRXJyb3IuaW5zdHJ1Y3Rpb25zLFxuICAgICAgICAgICAgICAgICAgICAgICAgZXJyb3JfaWQ6IGVycm9ySUQsXG4gICAgICAgICAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgICAgIH0sXG4gICAgICAgICAgICAgICAgZGVmYXVsdDogKCkgPT4ge1xuICAgICAgICAgICAgICAgICAgICByZXMudHlwZSgndHh0Jykuc2VuZChgJHtodHRwRXJyb3IuZXJyb3JDb2RlfSAtICR7aHR0cEVycm9yLm1lc3NhZ2V9XFxuXFxuJHtodHRwRXJyb3IuaW5zdHJ1Y3Rpb25zfVxcblxcbkVycm9yIElEOiAke2Vycm9ySUR9YCk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSk7XG4gICAgICAgIH0pO1xuXG4gICAgICAgIC8vIFN0YXJ0IGFsbCBjb21wb25lbnRzXG4gICAgICAgIGZvciAoY29uc3QgY29tcG9uZW50IG9mIHRoaXMuY29tcG9uZW50cykge1xuICAgICAgICAgICAgYXdhaXQgY29tcG9uZW50LnN0YXJ0KGFwcCwgcm91dGVyKTtcbiAgICAgICAgfVxuXG4gICAgICAgIC8vIFJvdXRlc1xuICAgICAgICB0aGlzLnJvdXRlcyhyb3V0ZXIpO1xuXG4gICAgICAgIHRoaXMucmVhZHkgPSB0cnVlO1xuICAgIH1cblxuICAgIGFzeW5jIHN0b3AoKTogUHJvbWlzZTx2b2lkPiB7XG4gICAgICAgIExvZ2dlci5pbmZvKCdTdG9wcGluZyBhcHBsaWNhdGlvbi4uLicpO1xuXG4gICAgICAgIGZvciAoY29uc3QgY29tcG9uZW50IG9mIHRoaXMuY29tcG9uZW50cykge1xuICAgICAgICAgICAgYXdhaXQgY29tcG9uZW50LnN0b3AoKTtcbiAgICAgICAgfVxuXG4gICAgICAgIExvZ2dlci5pbmZvKGAke3RoaXMuY29uc3RydWN0b3IubmFtZX0gdiR7dGhpcy52ZXJzaW9ufSAtIGJ5ZWApO1xuICAgIH1cblxuICAgIHByaXZhdGUgcm91dGVzKHJvb3RSb3V0ZXI6IFJvdXRlcikge1xuICAgICAgICBmb3IgKGNvbnN0IGNvbnRyb2xsZXIgb2YgdGhpcy5jb250cm9sbGVycykge1xuICAgICAgICAgICAgaWYgKGNvbnRyb2xsZXIuaGFzR2xvYmFsSGFuZGxlcnMoKSkge1xuICAgICAgICAgICAgICAgIGNvbnRyb2xsZXIuc2V0dXBHbG9iYWxIYW5kbGVycyhyb290Um91d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