2020-06-14 21:48:50 +02:00
|
|
|
import express, {NextFunction, Request, Response, Router} from 'express';
|
2020-04-23 15:43:15 +02:00
|
|
|
import {BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError} from "./HttpError";
|
2020-04-22 15:52:17 +02:00
|
|
|
import {lib} from "nunjucks";
|
|
|
|
import WebSocketListener from "./WebSocketListener";
|
|
|
|
import ApplicationComponent from "./ApplicationComponent";
|
|
|
|
import Controller from "./Controller";
|
2020-04-23 15:43:15 +02:00
|
|
|
import MysqlConnectionManager from "./db/MysqlConnectionManager";
|
2020-09-25 23:42:15 +02:00
|
|
|
import Migration, {MigrationType} from "./db/Migration";
|
2020-04-23 15:43:15 +02:00
|
|
|
import {Type} from "./Utils";
|
2020-06-14 11:43:00 +02:00
|
|
|
import LogRequestsComponent from "./components/LogRequestsComponent";
|
2020-11-14 16:24:00 +01:00
|
|
|
import {ValidationBag, ValidationError} from "./db/Validator";
|
2020-06-27 17:11:31 +02:00
|
|
|
import config from "config";
|
2020-07-15 15:13:40 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import SecurityError from "./SecurityError";
|
2020-07-15 15:22:04 +02:00
|
|
|
import * as path from "path";
|
2020-07-19 11:57:47 +02:00
|
|
|
import CacheProvider from "./CacheProvider";
|
|
|
|
import RedisComponent from "./components/RedisComponent";
|
2020-09-25 22:03:22 +02:00
|
|
|
import Extendable from "./Extendable";
|
2020-11-02 17:48:52 +01:00
|
|
|
import {log} from "./Logger";
|
2020-07-08 09:52:08 +02:00
|
|
|
import TemplateError = lib.TemplateError;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export default abstract class Application implements Extendable<ApplicationComponent | WebSocketListener<Application>> {
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly version: string;
|
2020-07-08 09:52:08 +02:00
|
|
|
private readonly ignoreCommandLine: boolean;
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly controllers: Controller[] = [];
|
2020-09-25 22:03:22 +02:00
|
|
|
private readonly webSocketListeners: { [p: string]: WebSocketListener<Application> } = {};
|
|
|
|
private readonly components: ApplicationComponent[] = [];
|
2020-07-19 11:57:47 +02:00
|
|
|
private cacheProvider?: CacheProvider;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
private ready: boolean = false;
|
|
|
|
|
2020-07-08 09:52:08 +02:00
|
|
|
protected constructor(version: string, ignoreCommandLine: boolean = false) {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.version = version;
|
2020-07-08 09:52:08 +02:00
|
|
|
this.ignoreCommandLine = ignoreCommandLine;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected abstract getMigrations(): MigrationType<Migration>[];
|
2020-04-23 15:43:15 +02:00
|
|
|
|
2020-12-04 14:42:09 +01:00
|
|
|
protected abstract init(): Promise<void>;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected use(thing: Controller | WebSocketListener<this> | ApplicationComponent): void {
|
2020-04-22 15:52:17 +02:00
|
|
|
if (thing instanceof Controller) {
|
2020-09-25 22:03:22 +02:00
|
|
|
thing.setApp(this);
|
2020-04-22 15:52:17 +02:00
|
|
|
this.controllers.push(thing);
|
|
|
|
} else if (thing instanceof WebSocketListener) {
|
|
|
|
const path = thing.path();
|
|
|
|
this.webSocketListeners[path] = thing;
|
2020-07-19 11:57:47 +02:00
|
|
|
thing.init(this);
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info(`Added websocket listener on ${path}`);
|
2020-04-22 15:52:17 +02:00
|
|
|
} else {
|
2020-04-25 09:34:02 +02:00
|
|
|
thing.setApp(this);
|
2020-04-22 15:52:17 +02:00
|
|
|
this.components.push(thing);
|
2020-07-19 11:57:47 +02:00
|
|
|
|
|
|
|
if (thing instanceof RedisComponent) {
|
|
|
|
this.cacheProvider = thing;
|
|
|
|
}
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start(): Promise<void> {
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info(`${config.get('app.name')} v${this.version} - hi`);
|
2020-04-22 15:52:17 +02:00
|
|
|
process.once('SIGINT', () => {
|
|
|
|
this.stop().catch(console.error);
|
|
|
|
});
|
|
|
|
|
2020-04-23 15:43:15 +02:00
|
|
|
// Register migrations
|
|
|
|
MysqlConnectionManager.registerMigrations(this.getMigrations());
|
|
|
|
|
2020-06-05 14:32:39 +02:00
|
|
|
// Process command line
|
2020-07-08 09:52:08 +02:00
|
|
|
if (!this.ignoreCommandLine && await this.processCommandLine()) {
|
2020-06-05 14:32:39 +02:00
|
|
|
await this.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-22 15:52:17 +02:00
|
|
|
// Register all components and alike
|
|
|
|
await this.init();
|
|
|
|
|
2020-07-15 15:06:13 +02:00
|
|
|
// Security
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
await this.checkSecuritySettings();
|
|
|
|
}
|
|
|
|
|
2020-04-22 15:52:17 +02:00
|
|
|
// Init express
|
|
|
|
const app = express();
|
2020-07-11 11:46:16 +02:00
|
|
|
const initRouter = express.Router();
|
|
|
|
const handleRouter = express.Router();
|
|
|
|
app.use(initRouter);
|
|
|
|
app.use(handleRouter);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
// Error handlers
|
2020-09-25 23:42:15 +02:00
|
|
|
app.use((err: unknown, req: Request, res: Response, next: NextFunction) => {
|
2020-07-11 11:46:16 +02:00
|
|
|
if (res.headersSent) return next(err);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-11-14 16:24:00 +01:00
|
|
|
// Transform single validation errors into a validation bag for convenience
|
|
|
|
if (err instanceof ValidationError) {
|
|
|
|
const bag = new ValidationBag();
|
|
|
|
bag.addMessage(err);
|
|
|
|
err = bag;
|
|
|
|
}
|
|
|
|
|
2020-06-14 21:48:50 +02:00
|
|
|
if (err instanceof ValidationBag) {
|
2020-11-14 16:24:00 +01:00
|
|
|
const bag = err;
|
2020-06-15 12:58:15 +02:00
|
|
|
res.format({
|
|
|
|
json: () => {
|
2020-11-15 14:12:45 +01:00
|
|
|
res.status(400);
|
2020-06-15 12:58:15 +02:00
|
|
|
res.json({
|
|
|
|
status: 'error',
|
2020-11-15 14:12:45 +01:00
|
|
|
code: 400,
|
2020-06-15 12:58:15 +02:00
|
|
|
message: 'Invalid form data',
|
2020-11-14 16:24:00 +01:00
|
|
|
messages: bag.getMessages(),
|
2020-06-15 12:58:15 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
text: () => {
|
2020-11-15 14:12:45 +01:00
|
|
|
res.status(400);
|
2020-11-14 16:24:00 +01:00
|
|
|
res.send('Error: ' + bag.getMessages());
|
2020-06-15 12:58:15 +02:00
|
|
|
},
|
|
|
|
html: () => {
|
2020-11-14 16:24:00 +01:00
|
|
|
req.flash('validation', bag.getMessages());
|
2020-06-15 12:58:15 +02:00
|
|
|
res.redirectBack();
|
|
|
|
},
|
|
|
|
});
|
2020-06-14 21:48:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:48:52 +01:00
|
|
|
const errorId = LogRequestsComponent.logRequest(req, res, err, '500 Internal Error',
|
2020-09-25 23:42:15 +02:00
|
|
|
err instanceof BadRequestError || err instanceof ServiceUnavailableHttpError);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
let httpError: HttpError;
|
|
|
|
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
httpError = err;
|
|
|
|
} else if (err instanceof TemplateError && err.cause instanceof HttpError) {
|
|
|
|
httpError = err.cause;
|
|
|
|
} else {
|
2020-09-25 23:42:15 +02:00
|
|
|
httpError = new ServerError('Internal server error.', err instanceof Error ? err : undefined);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-09-25 23:42:15 +02:00
|
|
|
error_id: errorId,
|
2020-04-22 15:52:17 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
json: () => {
|
|
|
|
res.json({
|
|
|
|
status: 'error',
|
|
|
|
code: httpError.errorCode,
|
|
|
|
message: httpError.message,
|
|
|
|
instructions: httpError.instructions,
|
2020-09-25 23:42:15 +02:00
|
|
|
error_id: errorId,
|
2020-04-22 15:52:17 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
default: () => {
|
2020-09-25 23:42:15 +02:00
|
|
|
res.type('txt').send(`${httpError.errorCode} - ${httpError.message}\n\n${httpError.instructions}\n\nError ID: ${errorId}`);
|
|
|
|
},
|
2020-04-22 15:52:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
// Start components
|
|
|
|
for (const component of this.components) {
|
2020-09-25 22:03:22 +02:00
|
|
|
await component.start?.(app);
|
2020-07-11 11:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Components routes
|
2020-04-22 15:52:17 +02:00
|
|
|
for (const component of this.components) {
|
2020-09-25 22:03:22 +02:00
|
|
|
if (component.init) {
|
|
|
|
component.setCurrentRouter(initRouter);
|
|
|
|
await component.init(initRouter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component.handle) {
|
|
|
|
component.setCurrentRouter(handleRouter);
|
|
|
|
await component.handle(handleRouter);
|
|
|
|
}
|
|
|
|
|
|
|
|
component.setCurrentRouter(null);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Routes
|
2020-07-11 11:46:16 +02:00
|
|
|
this.routes(initRouter, handleRouter);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
this.ready = true;
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:32:39 +02:00
|
|
|
protected async processCommandLine(): Promise<boolean> {
|
|
|
|
const args = process.argv;
|
|
|
|
for (let i = 2; i < args.length; i++) {
|
|
|
|
switch (args[i]) {
|
|
|
|
case '--verbose':
|
2020-11-02 17:48:52 +01:00
|
|
|
log.setSettings({minLevel: "trace"});
|
2020-06-05 14:32:39 +02:00
|
|
|
break;
|
2020-06-14 11:43:00 +02:00
|
|
|
case '--full-http-requests':
|
|
|
|
LogRequestsComponent.logFullHttpRequests();
|
|
|
|
break;
|
2020-06-05 14:32:39 +02:00
|
|
|
case 'migration':
|
|
|
|
await MysqlConnectionManager.migrationCommand(args.slice(i + 1));
|
|
|
|
return true;
|
|
|
|
default:
|
2020-11-02 17:48:52 +01:00
|
|
|
log.warn('Unrecognized argument', args[i]);
|
2020-06-05 14:32:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:06:13 +02:00
|
|
|
private async checkSecuritySettings(): Promise<void> {
|
2020-07-15 15:13:40 +02:00
|
|
|
// Check config file permissions
|
2020-07-15 15:22:04 +02:00
|
|
|
const configDir = 'config';
|
|
|
|
for (const file of fs.readdirSync(configDir)) {
|
|
|
|
const fullPath = path.resolve(configDir, file);
|
|
|
|
const stats = fs.lstatSync(fullPath);
|
2020-07-15 15:13:40 +02:00
|
|
|
if (stats.uid !== process.getuid())
|
2020-07-15 15:39:08 +02:00
|
|
|
throw new SecurityError(`${fullPath} is not owned by this process (${process.getuid()}).`);
|
|
|
|
|
|
|
|
const mode = (stats.mode & parseInt('777', 8)).toString(8);
|
|
|
|
if (mode !== '400')
|
|
|
|
throw new SecurityError(`${fullPath} is ${mode}; should be 400.`);
|
2020-07-15 15:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check security fields
|
2020-07-15 15:06:13 +02:00
|
|
|
for (const component of this.components) {
|
2020-09-25 22:03:22 +02:00
|
|
|
await component.checkSecuritySettings?.();
|
2020-07-15 15:06:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:46:21 +02:00
|
|
|
public async stop(): Promise<void> {
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info('Stopping application...');
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
for (const component of this.components) {
|
2020-09-25 22:03:22 +02:00
|
|
|
await component.stop?.();
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info(`${this.constructor.name} v${this.version} - bye`);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
private routes(initRouter: Router, handleRouter: Router) {
|
2020-04-22 15:52:17 +02:00
|
|
|
for (const controller of this.controllers) {
|
2020-09-25 22:03:22 +02:00
|
|
|
if (controller.hasGlobalMiddlewares()) {
|
2020-07-11 11:46:16 +02:00
|
|
|
controller.setupGlobalHandlers(handleRouter);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info(`Registered global middlewares for controller ${controller.constructor.name}`);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const controller of this.controllers) {
|
2020-07-11 11:08:57 +02:00
|
|
|
const {mainRouter, fileUploadFormRouter} = controller.setupRoutes();
|
2020-07-11 11:46:16 +02:00
|
|
|
initRouter.use(controller.getRoutesPrefix(), fileUploadFormRouter);
|
|
|
|
handleRouter.use(controller.getRoutesPrefix(), mainRouter);
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2021-01-21 15:44:30 +01:00
|
|
|
log.info(`> Registered routes for controller ${controller.constructor.name} at ${controller.getRoutesPrefix()}`);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
handleRouter.use((req: Request) => {
|
2020-04-22 15:52:17 +02:00
|
|
|
throw new NotFoundHttpError('page', req.originalUrl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public isReady(): boolean {
|
|
|
|
return this.ready;
|
|
|
|
}
|
2020-04-25 09:34:02 +02:00
|
|
|
|
|
|
|
public getVersion(): string {
|
|
|
|
return this.version;
|
|
|
|
}
|
2020-07-19 11:57:47 +02:00
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
public getWebSocketListeners(): { [p: string]: WebSocketListener<Application> } {
|
2020-07-19 11:57:47 +02:00
|
|
|
return this.webSocketListeners;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public getCache(): CacheProvider | null {
|
|
|
|
return this.cacheProvider || null;
|
2020-07-19 11:57:47 +02:00
|
|
|
}
|
2020-09-23 08:46:21 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public as<C extends ApplicationComponent | WebSocketListener<Application>>(type: Type<C>): C {
|
|
|
|
const module = this.components.find(component => component.constructor === type) ||
|
|
|
|
Object.values(this.webSocketListeners).find(listener => listener.constructor === type);
|
|
|
|
if (!module) throw new Error(`This app doesn't have a ${type.name} component.`);
|
|
|
|
return module as C;
|
2020-09-25 22:03:22 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public asOptional<C extends ApplicationComponent | WebSocketListener<Application>>(type: Type<C>): C | null {
|
|
|
|
const module = this.components.find(component => component.constructor === type) ||
|
|
|
|
Object.values(this.webSocketListeners).find(listener => listener.constructor === type);
|
|
|
|
return module ? module as C : null;
|
2020-09-23 08:46:21 +02:00
|
|
|
}
|
2020-09-25 22:03:22 +02:00
|
|
|
}
|