2020-11-11 18:59:38 +01:00
|
|
|
import Application from "../src/Application";
|
|
|
|
import Migration, {MigrationType} from "../src/db/Migration";
|
|
|
|
import ExpressAppComponent from "../src/components/ExpressAppComponent";
|
|
|
|
import RedisComponent from "../src/components/RedisComponent";
|
|
|
|
import MysqlComponent from "../src/components/MysqlComponent";
|
|
|
|
import NunjucksComponent from "../src/components/NunjucksComponent";
|
|
|
|
import LogRequestsComponent from "../src/components/LogRequestsComponent";
|
|
|
|
import MailComponent from "../src/components/MailComponent";
|
|
|
|
import SessionComponent from "../src/components/SessionComponent";
|
|
|
|
import AuthComponent from "../src/auth/AuthComponent";
|
|
|
|
import FormHelperComponent from "../src/components/FormHelperComponent";
|
|
|
|
import RedirectBackComponent from "../src/components/RedirectBackComponent";
|
|
|
|
import ServeStaticDirectoryComponent from "../src/components/ServeStaticDirectoryComponent";
|
|
|
|
import {Express} from "express";
|
|
|
|
import MagicLinkAuthMethod from "../src/auth/magic_link/MagicLinkAuthMethod";
|
|
|
|
import PasswordAuthMethod from "../src/auth/password/PasswordAuthMethod";
|
|
|
|
import {MAGIC_LINK_MAIL} from "./Mails";
|
|
|
|
import CreateMigrationsTable from "./migrations/CreateMigrationsTable";
|
2020-11-11 19:08:33 +01:00
|
|
|
import CreateUsersAndUserEmailsTableMigration from "./auth/migrations/CreateUsersAndUserEmailsTableMigration";
|
2020-11-11 18:59:38 +01:00
|
|
|
import CreateMagicLinksTableMigration from "./auth/magic_link/CreateMagicLinksTableMigration";
|
2020-11-11 19:08:33 +01:00
|
|
|
import AuthController from "./auth/AuthController";
|
|
|
|
import MagicLinkWebSocketListener from "./auth/magic_link/MagicLinkWebSocketListener";
|
|
|
|
import MagicLinkController from "./auth/magic_link/MagicLinkController";
|
|
|
|
import AddPasswordToUsersMigration from "./auth/password/AddPasswordToUsersMigration";
|
|
|
|
import AddNameToUsersMigration from "./auth/migrations/AddNameToUsersMigration";
|
|
|
|
import CsrfProtectionComponent from "./components/CsrfProtectionComponent";
|
2020-11-14 17:24:42 +01:00
|
|
|
import MailController from "./mail/MailController";
|
|
|
|
import WebSocketServerComponent from "./components/WebSocketServerComponent";
|
|
|
|
import Controller from "./Controller";
|
2021-01-21 15:58:03 +01:00
|
|
|
import AccountController from "./auth/AccountController";
|
|
|
|
import MakeMagicLinksSessionNotUniqueMigration from "./auth/magic_link/MakeMagicLinksSessionNotUniqueMigration";
|
|
|
|
import AddUsedToMagicLinksMigration from "./auth/magic_link/AddUsedToMagicLinksMigration";
|
2021-01-22 15:54:26 +01:00
|
|
|
import packageJson = require('../package.json');
|
2020-11-11 18:59:38 +01:00
|
|
|
|
|
|
|
export const MIGRATIONS = [
|
|
|
|
CreateMigrationsTable,
|
2020-11-11 19:08:33 +01:00
|
|
|
CreateUsersAndUserEmailsTableMigration,
|
|
|
|
AddPasswordToUsersMigration,
|
2020-11-11 18:59:38 +01:00
|
|
|
CreateMagicLinksTableMigration,
|
2020-11-14 17:24:42 +01:00
|
|
|
AddNameToUsersMigration,
|
2021-01-21 15:58:03 +01:00
|
|
|
MakeMagicLinksSessionNotUniqueMigration,
|
|
|
|
AddUsedToMagicLinksMigration,
|
2020-11-11 18:59:38 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
export default class TestApp extends Application {
|
|
|
|
private readonly addr: string;
|
|
|
|
private readonly port: number;
|
|
|
|
|
|
|
|
public constructor(addr: string, port: number) {
|
|
|
|
super(packageJson.version, true);
|
|
|
|
this.addr = addr;
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMigrations(): MigrationType<Migration>[] {
|
|
|
|
return MIGRATIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
2020-11-11 19:08:33 +01:00
|
|
|
this.registerWebSocketListeners();
|
|
|
|
this.registerControllers();
|
2020-11-11 18:59:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected registerComponents(): void {
|
|
|
|
// Base
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new ExpressAppComponent(this.addr, this.port));
|
2020-11-11 18:59:38 +01:00
|
|
|
this.use(new LogRequestsComponent());
|
|
|
|
|
|
|
|
// Static files
|
|
|
|
this.use(new ServeStaticDirectoryComponent('public'));
|
|
|
|
|
|
|
|
// Dynamic views and routes
|
|
|
|
this.use(new NunjucksComponent(['test/views', 'views']));
|
|
|
|
this.use(new RedirectBackComponent());
|
|
|
|
|
|
|
|
// Services
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new MysqlComponent());
|
2020-11-11 18:59:38 +01:00
|
|
|
this.use(new MailComponent());
|
|
|
|
|
|
|
|
// Session
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new RedisComponent());
|
|
|
|
this.use(new SessionComponent(this.as(RedisComponent)));
|
2020-11-11 18:59:38 +01:00
|
|
|
|
2020-11-11 19:08:33 +01:00
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
this.use(new CsrfProtectionComponent());
|
|
|
|
|
2020-11-11 18:59:38 +01:00
|
|
|
// Auth
|
|
|
|
this.use(new AuthComponent(this, new MagicLinkAuthMethod(this, MAGIC_LINK_MAIL), new PasswordAuthMethod(this)));
|
2020-11-14 17:24:42 +01:00
|
|
|
|
|
|
|
// WebSocket server
|
|
|
|
this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent)));
|
2020-11-11 19:08:33 +01:00
|
|
|
}
|
2020-11-11 18:59:38 +01:00
|
|
|
|
2020-11-11 19:08:33 +01:00
|
|
|
protected registerWebSocketListeners(): void {
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new MagicLinkWebSocketListener());
|
2020-11-11 18:59:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:08:33 +01:00
|
|
|
protected registerControllers(): void {
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new MailController());
|
2020-11-11 19:08:33 +01:00
|
|
|
this.use(new AuthController());
|
2021-01-21 15:58:03 +01:00
|
|
|
this.use(new AccountController());
|
2020-11-11 18:59:38 +01:00
|
|
|
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new MagicLinkController(this.as<MagicLinkWebSocketListener<this>>(MagicLinkWebSocketListener)));
|
|
|
|
|
|
|
|
// Special home controller
|
|
|
|
this.use(new class extends Controller {
|
|
|
|
public routes(): void {
|
|
|
|
this.get('/', (req, res) => {
|
|
|
|
res.render('home');
|
|
|
|
}, 'home');
|
|
|
|
}
|
|
|
|
}());
|
2020-11-11 19:08:33 +01:00
|
|
|
}
|
2020-11-11 18:59:38 +01:00
|
|
|
|
|
|
|
public getExpressApp(): Express {
|
|
|
|
return this.as(ExpressAppComponent).getExpressApp();
|
|
|
|
}
|
|
|
|
}
|