import Application from "swaf/Application"; import Migration, {MigrationType} from "swaf/db/Migration"; import CreateMigrationsTable from "swaf/migrations/CreateMigrationsTable"; import ExpressAppComponent from "swaf/components/ExpressAppComponent"; import NunjucksComponent from "swaf/components/NunjucksComponent"; import MysqlComponent from "swaf/components/MysqlComponent"; import LogRequestsComponent from "swaf/components/LogRequestsComponent"; import RedisComponent from "swaf/components/RedisComponent"; import ServeStaticDirectoryComponent from "swaf/components/ServeStaticDirectoryComponent"; import MaintenanceComponent from "swaf/components/MaintenanceComponent"; import MailComponent from "swaf/components/MailComponent"; import SessionComponent from "swaf/components/SessionComponent"; import FormHelperComponent from "swaf/components/FormHelperComponent"; import CsrfProtectionComponent from "swaf/components/CsrfProtectionComponent"; import WebSocketServerComponent from "swaf/components/WebSocketServerComponent"; import AboutController from "./controllers/AboutController"; import AutoUpdateComponent from "swaf/components/AutoUpdateComponent"; import AuthController from "./controllers/AuthController"; import MagicLinkWebSocketListener from "swaf/auth/magic_link/MagicLinkWebSocketListener"; import MagicLinkController from "./controllers/MagicLinkController"; import MailController from "swaf/auth/MailController"; import FileController from "./controllers/FileController"; import CreateUsersAndUserEmailsTable from "swaf/auth/migrations/CreateUsersAndUserEmailsTable"; import CreateMagicLinksTable from "swaf/auth/migrations/CreateMagicLinksTable"; import CreateAuthTokensTable from "./migrations/CreateAuthTokensTable"; import AuthComponent from "swaf/auth/AuthComponent"; import AuthGuard from "swaf/auth/AuthGuard"; import MagicLink from "swaf/auth/models/MagicLink"; import AuthToken from "./models/AuthToken"; import {MagicLinkActionType} from "./controllers/MagicLinkActionType"; import {Request} from "express"; import CreateFilesTable from "./migrations/CreateFilesTable"; import IncreaseFilesSizeField from "./migrations/IncreaseFilesSizeField"; import AddApprovedFieldToUsersTable from "swaf/auth/migrations/AddApprovedFieldToUsersTable"; import CreateUrlRedirectsTable from "./migrations/CreateUrlRedirectsTable"; import AuthTokenController from "./controllers/AuthTokenController"; import URLRedirectController from "./controllers/URLRedirectController"; import LinkController from "./controllers/LinkController"; import BackendController from "swaf/helpers/BackendController"; import RedirectBackComponent from "swaf/components/RedirectBackComponent"; import DummyMigration from "swaf/migrations/DummyMigration"; import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable"; import {Session} from "express-session"; import packageJson = require('../package.json'); export default class App extends Application { public constructor( private readonly addr: string, private readonly port: number, ) { super(packageJson.version); } protected getMigrations(): MigrationType[] { return [ CreateMigrationsTable, DummyMigration, CreateUsersAndUserEmailsTable, CreateMagicLinksTable, CreateAuthTokensTable, CreateFilesTable, IncreaseFilesSizeField, AddApprovedFieldToUsersTable, CreateUrlRedirectsTable, DropLegacyLogsTable, ]; } protected async init(): Promise { this.registerComponents(); this.registerWebSocketListeners(); this.registerControllers(); } private registerComponents() { const redisComponent = new RedisComponent(); const mysqlComponent = new MysqlComponent(); const expressAppComponent = new ExpressAppComponent(this.addr, this.port); this.use(expressAppComponent); // Base this.use(new LogRequestsComponent()); // Static files this.use(new ServeStaticDirectoryComponent('public')); this.use(new ServeStaticDirectoryComponent('node_modules/feather-icons/dist', '/icons')); // Dynamic views and routes this.use(new NunjucksComponent()); this.use(new RedirectBackComponent()); // Maintenance this.use(new MaintenanceComponent(this, () => { return redisComponent.canServe() && mysqlComponent.canServe(); })); this.use(new AutoUpdateComponent()); // Services this.use(mysqlComponent); this.use(new MailComponent()); // Session this.use(redisComponent); this.use(new SessionComponent(redisComponent)); this.use(new AuthComponent(new class extends AuthGuard { public async getProofForSession(session: Session): Promise { return await MagicLink.bySessionId( session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER], ); } public async getProofForRequest(req: Request): Promise { const authorization = req.header('Authorization'); if (authorization) { const token = await AuthToken.select().where('secret', authorization).first(); if (token) { token.use(); await token.save(); } return token; } return super.getProofForRequest(req); } }(this))); // Utils this.use(new FormHelperComponent()); // Middlewares this.use(new CsrfProtectionComponent()); // WebSocket server this.use(new WebSocketServerComponent(this, expressAppComponent, redisComponent)); } private registerWebSocketListeners() { this.use(new MagicLinkWebSocketListener()); } private registerControllers() { // Multi-domain + vhost this.use(new LinkController()); // Priority this.use(new AuthController()); this.use(new MagicLinkController(this.as>(MagicLinkWebSocketListener))); this.use(new BackendController()); // Core functionality this.use(new MailController()); // Other functionality this.use(new AuthTokenController()); // Semi-static this.use(new AboutController()); // Global slug this.use(new FileController()); this.use(new URLRedirectController()); } }