ily.li/src/App.ts

146 lines
5.8 KiB
TypeScript

import Application from "swaf/Application";
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
import CsrfProtectionComponent from "swaf/components/CsrfProtectionComponent";
import ExpressAppComponent from "swaf/components/ExpressAppComponent";
import FormHelperComponent from "swaf/components/FormHelperComponent";
import FrontendToolsComponent from "swaf/components/FrontendToolsComponent";
import LogRequestsComponent from "swaf/components/LogRequestsComponent";
import MailComponent from "swaf/components/MailComponent";
import MaintenanceComponent from "swaf/components/MaintenanceComponent";
import MysqlComponent from "swaf/components/MysqlComponent";
import PreviousUrlComponent from "swaf/components/PreviousUrlComponent";
import RedisComponent from "swaf/components/RedisComponent";
import ServeStaticDirectoryComponent from "swaf/components/ServeStaticDirectoryComponent";
import SessionComponent from "swaf/components/SessionComponent";
import WebSocketServerComponent from "swaf/components/WebSocketServerComponent";
import Migration, {MigrationType} from "swaf/db/Migration";
import AssetCompiler from "swaf/frontend/AssetCompiler";
import CopyAssetPreCompiler from "swaf/frontend/CopyAssetPreCompiler";
import MailViewEngine from "swaf/frontend/MailViewEngine";
import NunjucksViewEngine from "swaf/frontend/NunjucksViewEngine";
import ScssAssetPreCompiler from "swaf/frontend/ScssAssetPreCompiler";
import SvelteViewEngine from "swaf/frontend/SvelteViewEngine";
import TypeScriptPreCompiler from "swaf/frontend/TypeScriptPreCompiler";
import CreateMigrationsTable from "swaf/migrations/CreateMigrationsTable";
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
import DummyMigration from "swaf/migrations/DummyMigration";
import HomeController from "./controllers/HomeController.js";
export default class App extends Application {
public constructor(
version: string,
private readonly addr: string,
private readonly port: number,
) {
super(version);
}
protected getMigrations(): MigrationType<Migration>[] {
return [
CreateMigrationsTable,
DummyMigration,
CreateUsersAndUserEmailsTableMigration,
CreateMagicLinksTableMigration,
CreateAuthTokensTable,
CreateFilesTable,
IncreaseFilesSizeField,
AddApprovedFieldToUsersTableMigration,
CreateUrlRedirectsTable,
DropLegacyLogsTable,
DummyMigration,
AddUsedToMagicLinksMigration,
MakeMagicLinksSessionNotUniqueMigration,
AddPasswordToUsersMigration,
DummyMigration,
ReplaceTtlWithExpiresAtFilesTable,
];
}
protected async init(): Promise<void> {
this.registerComponents();
this.registerWebSocketListeners();
this.registerControllers();
}
private registerComponents() {
// Base
this.use(new ExpressAppComponent(this.addr, this.port));
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
const intermediateDirectory = 'intermediates/assets';
const assetCompiler = new AssetCompiler(intermediateDirectory, 'public');
const additionalViewPaths = ['test/assets'];
this.use(new FrontendToolsComponent(
assetCompiler,
new CopyAssetPreCompiler(intermediateDirectory, '', 'json', additionalViewPaths, false),
new ScssAssetPreCompiler(intermediateDirectory, assetCompiler.targetDir, 'scss', additionalViewPaths),
new CopyAssetPreCompiler(intermediateDirectory, 'img', 'svg', additionalViewPaths, true),
new TypeScriptPreCompiler(intermediateDirectory, additionalViewPaths),
new SvelteViewEngine(intermediateDirectory, ...additionalViewPaths),
new NunjucksViewEngine(intermediateDirectory, ...additionalViewPaths),
));
this.use(new PreviousUrlComponent());
// Maintenance
this.use(new MaintenanceComponent());
this.use(new AutoUpdateComponent());
// Services
this.use(new MysqlComponent());
this.use(new MailComponent(new MailViewEngine(intermediateDirectory, ...additionalViewPaths)));
// Session
this.use(new RedisComponent());
this.use(new SessionComponent(this.as(RedisComponent)));
this.use(new AuthComponent(this, new MagicLinkAuthMethod(this, MAGIC_LINK_MAIL), new PasswordAuthMethod(this)));
// Utils
this.use(new FormHelperComponent());
// Middlewares
this.use(new CsrfProtectionComponent());
// WebSocket server
this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent)));
this.use(new WebSocketServerComponent());
// Jobs
this.use(new DeleteOldFilesJobComponent());
}
private registerWebSocketListeners() {
this.use(new MagicLinkWebSocketListener());
}
private registerControllers() {
// Multi-domain + vhost
this.use(new LinkController());
// Priority
this.use(new MailController());
this.use(new AuthController());
this.use(new MagicLinkController(this.as<MagicLinkWebSocketListener<this>>(MagicLinkWebSocketListener)));
// Core
this.use(new AccountController());
this.use(new BackendController());
// Other functionality
this.use(new AuthTokenController());
// Semi-static
this.use(new AboutController());
// Global slug
this.use(new FileController());
this.use(new URLRedirectController());
}
}