2020-11-11 18:59:38 +01:00
|
|
|
import {Express} from "express";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
import Application from "./Application.js";
|
|
|
|
import AccountController from "./auth/AccountController.js";
|
|
|
|
import AuthComponent from "./auth/AuthComponent.js";
|
|
|
|
import AuthController from "./auth/AuthController.js";
|
|
|
|
import AddUsedToMagicLinksMigration from "./auth/magic_link/AddUsedToMagicLinksMigration.js";
|
|
|
|
import CreateMagicLinksTableMigration from "./auth/magic_link/CreateMagicLinksTableMigration.js";
|
|
|
|
import MagicLinkAuthMethod from "./auth/magic_link/MagicLinkAuthMethod.js";
|
|
|
|
import MagicLinkController from "./auth/magic_link/MagicLinkController.js";
|
|
|
|
import MagicLinkWebSocketListener from "./auth/magic_link/MagicLinkWebSocketListener.js";
|
|
|
|
import MakeMagicLinksSessionNotUniqueMigration from "./auth/magic_link/MakeMagicLinksSessionNotUniqueMigration.js";
|
2021-11-08 00:24:53 +01:00
|
|
|
import AddApprovedFieldToUsersTableMigration from "./auth/migrations/AddApprovedFieldToUsersTableMigration.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import AddNameChangedAtToUsersMigration from "./auth/migrations/AddNameChangedAtToUsersMigration.js";
|
|
|
|
import AddNameToUsersMigration from "./auth/migrations/AddNameToUsersMigration.js";
|
|
|
|
import CreateUsersAndUserEmailsTableMigration from "./auth/migrations/CreateUsersAndUserEmailsTableMigration.js";
|
|
|
|
import AddPasswordToUsersMigration from "./auth/password/AddPasswordToUsersMigration.js";
|
|
|
|
import PasswordAuthMethod from "./auth/password/PasswordAuthMethod.js";
|
|
|
|
import CsrfProtectionComponent from "./components/CsrfProtectionComponent.js";
|
|
|
|
import ExpressAppComponent from "./components/ExpressAppComponent.js";
|
|
|
|
import FormHelperComponent from "./components/FormHelperComponent.js";
|
|
|
|
import FrontendToolsComponent from "./components/FrontendToolsComponent.js";
|
|
|
|
import LogRequestsComponent from "./components/LogRequestsComponent.js";
|
|
|
|
import MailComponent from "./components/MailComponent.js";
|
2021-05-13 16:26:27 +02:00
|
|
|
import MaintenanceComponent from "./components/MaintenanceComponent.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import MysqlComponent from "./components/MysqlComponent.js";
|
|
|
|
import PreviousUrlComponent from "./components/PreviousUrlComponent.js";
|
|
|
|
import RedisComponent from "./components/RedisComponent.js";
|
|
|
|
import ServeStaticDirectoryComponent from "./components/ServeStaticDirectoryComponent.js";
|
|
|
|
import SessionComponent from "./components/SessionComponent.js";
|
|
|
|
import WebSocketServerComponent from "./components/WebSocketServerComponent.js";
|
|
|
|
import Controller from "./Controller.js";
|
|
|
|
import Migration, {MigrationType} from "./db/Migration.js";
|
2021-05-04 17:04:14 +02:00
|
|
|
import AssetCompiler from "./frontend/AssetCompiler.js";
|
|
|
|
import CopyAssetPreCompiler from "./frontend/CopyAssetPreCompiler.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import MailViewEngine from "./frontend/MailViewEngine.js";
|
|
|
|
import NunjucksViewEngine from "./frontend/NunjucksViewEngine.js";
|
2021-06-02 18:29:37 +02:00
|
|
|
import ScssAssetPreCompiler from "./frontend/ScssAssetPreCompiler.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import SvelteViewEngine from "./frontend/SvelteViewEngine.js";
|
2021-05-04 17:04:14 +02:00
|
|
|
import TypeScriptPreCompiler from "./frontend/TypeScriptPreCompiler.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import BackendController from "./helpers/BackendController.js";
|
|
|
|
import MailController from "./mail/MailController.js";
|
|
|
|
import {MAGIC_LINK_MAIL} from "./Mails.js";
|
|
|
|
import CreateMigrationsTable from "./migrations/CreateMigrationsTable.js";
|
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,
|
2021-02-23 17:42:25 +01:00
|
|
|
AddNameChangedAtToUsersMigration,
|
2020-11-11 18:59:38 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
export default class TestApp extends Application {
|
|
|
|
private readonly addr: string;
|
|
|
|
private readonly port: number;
|
|
|
|
|
2021-11-08 00:52:33 +01:00
|
|
|
public constructor(
|
|
|
|
version: string,
|
|
|
|
addr: string,
|
|
|
|
port: number,
|
|
|
|
ignoreCommandLine: boolean = false,
|
|
|
|
private readonly approvalMode: boolean,
|
|
|
|
) {
|
2021-05-03 19:29:22 +02:00
|
|
|
super(version, ignoreCommandLine);
|
2020-11-11 18:59:38 +01:00
|
|
|
this.addr = addr;
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMigrations(): MigrationType<Migration>[] {
|
2021-11-08 00:52:33 +01:00
|
|
|
const migrations = [...MIGRATIONS];
|
|
|
|
|
|
|
|
if (this.approvalMode) {
|
|
|
|
migrations.push(AddApprovedFieldToUsersTableMigration);
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrations;
|
2020-11-11 18:59:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
2021-05-13 16:26:27 +02:00
|
|
|
// Maintenance
|
|
|
|
this.use(new MaintenanceComponent());
|
|
|
|
|
2020-11-11 18:59:38 +01:00
|
|
|
// Dynamic views and routes
|
2021-05-27 15:26:19 +02:00
|
|
|
const intermediateDirectory = 'intermediates/assets';
|
2021-06-02 18:29:37 +02:00
|
|
|
const assetCompiler = new AssetCompiler(intermediateDirectory, 'public');
|
2021-04-28 14:53:46 +02:00
|
|
|
this.use(new FrontendToolsComponent(
|
2021-06-02 18:29:37 +02:00
|
|
|
assetCompiler,
|
2021-05-04 17:04:14 +02:00
|
|
|
new CopyAssetPreCompiler(intermediateDirectory, '', 'json', ['test/assets'], false),
|
2021-06-02 18:29:37 +02:00
|
|
|
new ScssAssetPreCompiler(intermediateDirectory, assetCompiler.targetDir, 'scss', ['test/assets']),
|
2021-06-02 17:13:46 +02:00
|
|
|
new CopyAssetPreCompiler(intermediateDirectory, 'img', 'svg', ['test/assets'], true),
|
2021-05-04 17:04:14 +02:00
|
|
|
new TypeScriptPreCompiler(intermediateDirectory, ['test/assets']),
|
|
|
|
new SvelteViewEngine(intermediateDirectory, 'test/assets'),
|
|
|
|
new NunjucksViewEngine(intermediateDirectory, 'test/assets'),
|
2021-04-28 14:53:46 +02:00
|
|
|
));
|
2021-01-24 22:24:04 +01:00
|
|
|
this.use(new PreviousUrlComponent());
|
2020-11-11 18:59:38 +01:00
|
|
|
|
|
|
|
// Services
|
2020-11-14 17:24:42 +01:00
|
|
|
this.use(new MysqlComponent());
|
2021-05-27 15:26:19 +02:00
|
|
|
this.use(new MailComponent(new MailViewEngine('intermediates/assets', 'test/assets')));
|
2020-11-11 18:59:38 +01:00
|
|
|
|
|
|
|
// 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
|
2021-05-13 16:03:59 +02:00
|
|
|
this.use(new WebSocketServerComponent());
|
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());
|
2021-04-22 15:38:24 +02:00
|
|
|
this.use(new BackendController());
|
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');
|
2021-06-02 18:30:20 +02:00
|
|
|
this.get('/tests', (req, res) => {
|
|
|
|
res.render('tests');
|
|
|
|
}, 'tests');
|
2021-11-08 00:24:53 +01:00
|
|
|
this.get('/design', (req, res) => {
|
|
|
|
req.flash('success', 'Success.');
|
|
|
|
req.flash('info', 'Info.');
|
|
|
|
req.flash('warning', 'Warning.');
|
|
|
|
req.flash('error', 'Error.');
|
|
|
|
req.flash('error-alert', 'Error alert.');
|
|
|
|
res.render('design');
|
|
|
|
}, 'design');
|
2020-11-14 17:24:42 +01:00
|
|
|
}
|
|
|
|
}());
|
2020-11-11 19:08:33 +01:00
|
|
|
}
|
2020-11-11 18:59:38 +01:00
|
|
|
|
|
|
|
public getExpressApp(): Express {
|
|
|
|
return this.as(ExpressAppComponent).getExpressApp();
|
|
|
|
}
|
|
|
|
}
|