2020-04-23 18:07:55 +02:00
|
|
|
import Application from "wms-core/Application";
|
|
|
|
import {Type} from "wms-core/Utils";
|
|
|
|
import Migration from "wms-core/db/Migration";
|
|
|
|
import CreateMigrationsTable from "wms-core/migrations/CreateMigrationsTable";
|
|
|
|
import CreateLogsTable from "wms-core/migrations/CreateLogsTable";
|
|
|
|
import ExpressAppComponent from "wms-core/components/ExpressAppComponent";
|
|
|
|
import NunjucksComponent from "wms-core/components/NunjucksComponent";
|
|
|
|
import MysqlComponent from "wms-core/components/MysqlComponent";
|
|
|
|
import LogRequestsComponent from "wms-core/components/LogRequestsComponent";
|
|
|
|
import RedisComponent from "wms-core/components/RedisComponent";
|
|
|
|
import ServeStaticDirectoryComponent from "wms-core/components/ServeStaticDirectoryComponent";
|
|
|
|
import MaintenanceComponent from "wms-core/components/MaintenanceComponent";
|
|
|
|
import MailComponent from "wms-core/components/MailComponent";
|
|
|
|
import SessionComponent from "wms-core/components/SessionComponent";
|
|
|
|
import FormHelperComponent from "wms-core/components/FormHelperComponent";
|
|
|
|
import CsrfProtectionComponent from "wms-core/components/CsrfProtectionComponent";
|
|
|
|
import WebSocketServerComponent from "wms-core/components/WebSocketServerComponent";
|
2020-06-14 13:01:52 +02:00
|
|
|
import AboutController from "./controllers/AboutController";
|
2020-05-05 15:26:28 +02:00
|
|
|
import AutoUpdateComponent from "wms-core/components/AutoUpdateComponent";
|
2020-06-14 13:01:52 +02:00
|
|
|
import AuthController from "./controllers/AuthController";
|
|
|
|
import MagicLinkWebSocketListener from "wms-core/auth/magic_link/MagicLinkWebSocketListener";
|
|
|
|
import MagicLinkController from "./controllers/MagicLinkController";
|
|
|
|
import MailController from "wms-core/auth/MailController";
|
|
|
|
import FileController from "./controllers/FileController";
|
|
|
|
import CreateUsersAndUserEmailsTable from "wms-core/auth/migrations/CreateUsersAndUserEmailsTable";
|
|
|
|
import CreateMagicLinksTable from "wms-core/auth/migrations/CreateMagicLinksTable";
|
|
|
|
import CreateAuthTokensTable from "./migrations/CreateAuthTokensTable";
|
|
|
|
import AuthComponent from "wms-core/auth/AuthComponent";
|
|
|
|
import AuthGuard from "wms-core/auth/AuthGuard";
|
|
|
|
import MagicLink from "wms-core/auth/models/MagicLink";
|
|
|
|
import AuthToken from "./models/AuthToken";
|
|
|
|
import {MagicLinkActionType} from "./controllers/MagicLinkActionType";
|
|
|
|
import {Request} from "express";
|
|
|
|
import CreateFilesTable from "./migrations/CreateFilesTable";
|
2020-06-15 10:16:28 +02:00
|
|
|
import IncreaseFilesSizeField from "./migrations/IncreaseFilesSizeField";
|
2020-06-27 16:31:36 +02:00
|
|
|
import AddApprovedFieldToUsersTable from "wms-core/auth/migrations/AddApprovedFieldToUsersTable";
|
2020-07-06 10:49:18 +02:00
|
|
|
import CreateUrlRedirectsTable from "./migrations/CreateUrlRedirectsTable";
|
|
|
|
import AuthTokenController from "./controllers/AuthTokenController";
|
|
|
|
import URLRedirectController from "./controllers/URLRedirectController";
|
2020-07-06 15:39:47 +02:00
|
|
|
import LinkController from "./controllers/LinkController";
|
2020-08-11 17:54:15 +02:00
|
|
|
import BackendController from "wms-core/helpers/BackendController";
|
|
|
|
import RedirectBackComponent from "wms-core/components/RedirectBackComponent";
|
2020-10-05 13:38:03 +02:00
|
|
|
import packageJson = require('../package.json');
|
2020-04-23 18:07:55 +02:00
|
|
|
|
2020-06-14 13:01:52 +02:00
|
|
|
export default class App extends Application {
|
2020-10-05 13:38:03 +02:00
|
|
|
public constructor(
|
|
|
|
private readonly addr: string,
|
|
|
|
private readonly port: number,
|
|
|
|
) {
|
|
|
|
super(packageJson.version);
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:38:03 +02:00
|
|
|
protected getMigrations(): MigrationType<Migration>[] {
|
2020-04-23 18:07:55 +02:00
|
|
|
return [
|
|
|
|
CreateMigrationsTable,
|
|
|
|
CreateLogsTable,
|
2020-06-14 13:01:52 +02:00
|
|
|
CreateUsersAndUserEmailsTable,
|
|
|
|
CreateMagicLinksTable,
|
|
|
|
CreateAuthTokensTable,
|
|
|
|
CreateFilesTable,
|
2020-06-15 10:16:28 +02:00
|
|
|
IncreaseFilesSizeField,
|
2020-06-27 16:31:36 +02:00
|
|
|
AddApprovedFieldToUsersTable,
|
2020-07-06 10:49:18 +02:00
|
|
|
CreateUrlRedirectsTable,
|
2020-04-23 18:07:55 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
|
|
|
this.registerWebSocketListeners();
|
|
|
|
this.registerControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private registerComponents() {
|
|
|
|
const redisComponent = new RedisComponent();
|
|
|
|
const mysqlComponent = new MysqlComponent();
|
|
|
|
|
2020-10-05 13:38:03 +02:00
|
|
|
const expressAppComponent = new ExpressAppComponent(this.addr, this.port);
|
2020-06-14 17:37:47 +02:00
|
|
|
this.use(expressAppComponent);
|
2020-08-11 17:54:15 +02:00
|
|
|
|
|
|
|
// Base
|
2020-06-14 17:37:47 +02:00
|
|
|
this.use(new LogRequestsComponent());
|
|
|
|
|
2020-06-14 21:23:57 +02:00
|
|
|
// Static files
|
|
|
|
this.use(new ServeStaticDirectoryComponent('public'));
|
|
|
|
this.use(new ServeStaticDirectoryComponent('node_modules/feather-icons/dist', '/icons'));
|
|
|
|
|
2020-08-11 17:54:15 +02:00
|
|
|
// Dynamic views and routes
|
|
|
|
this.use(new NunjucksComponent());
|
|
|
|
this.use(new RedirectBackComponent());
|
|
|
|
|
2020-04-23 18:07:55 +02:00
|
|
|
// Maintenance
|
|
|
|
this.use(new MaintenanceComponent(this, () => {
|
|
|
|
return redisComponent.canServe() && mysqlComponent.canServe();
|
|
|
|
}));
|
2020-05-05 15:26:28 +02:00
|
|
|
this.use(new AutoUpdateComponent());
|
2020-04-23 18:07:55 +02:00
|
|
|
|
|
|
|
// Services
|
|
|
|
this.use(mysqlComponent);
|
|
|
|
this.use(new MailComponent());
|
|
|
|
|
2020-06-14 21:23:57 +02:00
|
|
|
// Session
|
|
|
|
this.use(redisComponent);
|
|
|
|
this.use(new SessionComponent(redisComponent));
|
2020-06-14 13:01:52 +02:00
|
|
|
this.use(new AuthComponent(new class extends AuthGuard<MagicLink | AuthToken> {
|
|
|
|
public async getProofForSession(session: Express.Session): Promise<any | null> {
|
|
|
|
return await MagicLink.bySessionID(session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getProofForRequest(req: Request): Promise<MagicLink | AuthToken | null> {
|
|
|
|
const authorization = req.header('Authorization');
|
|
|
|
if (authorization) {
|
2020-06-27 16:31:36 +02:00
|
|
|
const token = await AuthToken.select().where('secret', authorization).first();
|
2020-06-14 21:23:57 +02:00
|
|
|
if (token) {
|
|
|
|
token.use();
|
|
|
|
await token.save();
|
|
|
|
}
|
|
|
|
return token;
|
2020-06-14 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.getProofForRequest(req);
|
|
|
|
}
|
|
|
|
}));
|
2020-04-23 18:07:55 +02:00
|
|
|
|
2020-06-14 21:23:57 +02:00
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
|
2020-04-23 18:07:55 +02:00
|
|
|
// Middlewares
|
|
|
|
this.use(new CsrfProtectionComponent());
|
|
|
|
|
|
|
|
// WebSocket server
|
|
|
|
this.use(new WebSocketServerComponent(this, expressAppComponent, redisComponent));
|
|
|
|
}
|
|
|
|
|
|
|
|
private registerWebSocketListeners() {
|
2020-06-14 13:01:52 +02:00
|
|
|
this.magicLinkWebSocketListener = new MagicLinkWebSocketListener();
|
|
|
|
this.use(this.magicLinkWebSocketListener);
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private registerControllers() {
|
2020-07-06 15:39:47 +02:00
|
|
|
// Multi-domain + vhost
|
|
|
|
this.use(new LinkController());
|
|
|
|
|
2020-06-14 13:01:52 +02:00
|
|
|
// Priority
|
|
|
|
this.use(new AuthController());
|
|
|
|
this.use(new MagicLinkController(this.magicLinkWebSocketListener!));
|
2020-06-27 16:31:36 +02:00
|
|
|
this.use(new BackendController());
|
2020-06-14 13:01:52 +02:00
|
|
|
|
|
|
|
// Core functionality
|
|
|
|
this.use(new MailController());
|
|
|
|
|
2020-07-06 10:49:18 +02:00
|
|
|
// Other functionality
|
|
|
|
this.use(new AuthTokenController());
|
|
|
|
|
2020-06-14 13:01:52 +02:00
|
|
|
// Semi-static
|
|
|
|
this.use(new AboutController());
|
2020-06-14 14:53:05 +02:00
|
|
|
|
|
|
|
// Global slug
|
|
|
|
this.use(new FileController());
|
2020-07-06 10:49:18 +02:00
|
|
|
this.use(new URLRedirectController());
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
2020-10-05 13:38:03 +02:00
|
|
|
}
|