ily.li/src/App.ts

146 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-11-22 13:49:08 +01:00
import Application from "swaf/Application";
2021-11-21 17:52:56 +01:00
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
import CsrfProtectionComponent from "swaf/components/CsrfProtectionComponent";
2020-11-22 13:49:08 +01:00
import ExpressAppComponent from "swaf/components/ExpressAppComponent";
2021-11-21 17:52:56 +01:00
import FormHelperComponent from "swaf/components/FormHelperComponent";
import FrontendToolsComponent from "swaf/components/FrontendToolsComponent";
2020-11-22 13:49:08 +01:00
import LogRequestsComponent from "swaf/components/LogRequestsComponent";
2021-11-21 17:52:56 +01:00
import MailComponent from "swaf/components/MailComponent";
import MaintenanceComponent from "swaf/components/MaintenanceComponent";
import MysqlComponent from "swaf/components/MysqlComponent";
import PreviousUrlComponent from "swaf/components/PreviousUrlComponent";
2020-11-22 13:49:08 +01:00
import RedisComponent from "swaf/components/RedisComponent";
import ServeStaticDirectoryComponent from "swaf/components/ServeStaticDirectoryComponent";
import SessionComponent from "swaf/components/SessionComponent";
import WebSocketServerComponent from "swaf/components/WebSocketServerComponent";
2021-11-21 17:52:56 +01:00
import Migration, {MigrationType} from "swaf/db/Migration";
2021-11-20 19:54:59 +01:00
import AssetCompiler from "swaf/frontend/AssetCompiler";
import CopyAssetPreCompiler from "swaf/frontend/CopyAssetPreCompiler";
2021-11-21 17:52:56 +01:00
import MailViewEngine from "swaf/frontend/MailViewEngine";
import NunjucksViewEngine from "swaf/frontend/NunjucksViewEngine";
2021-11-20 19:54:59 +01:00
import ScssAssetPreCompiler from "swaf/frontend/ScssAssetPreCompiler";
import SvelteViewEngine from "swaf/frontend/SvelteViewEngine";
2021-11-21 17:52:56 +01:00
import TypeScriptPreCompiler from "swaf/frontend/TypeScriptPreCompiler";
import CreateMigrationsTable from "swaf/migrations/CreateMigrationsTable";
2020-11-22 13:49:08 +01:00
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
2021-11-21 17:52:56 +01:00
import DummyMigration from "swaf/migrations/DummyMigration";
import HomeController from "./controllers/HomeController.js";
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(
2021-11-20 19:54:59 +01:00
version: string,
2020-10-05 13:38:03 +02:00
private readonly addr: string,
private readonly port: number,
) {
2021-11-20 19:54:59 +01:00
super(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,
2020-11-22 13:49:08 +01:00
DummyMigration,
2021-01-25 17:43:26 +01:00
CreateUsersAndUserEmailsTableMigration,
CreateMagicLinksTableMigration,
2020-06-14 13:01:52 +02:00
CreateAuthTokensTable,
CreateFilesTable,
IncreaseFilesSizeField,
2021-01-25 17:43:26 +01:00
AddApprovedFieldToUsersTableMigration,
2020-07-06 10:49:18 +02:00
CreateUrlRedirectsTable,
2020-11-22 13:49:08 +01:00
DropLegacyLogsTable,
2021-01-25 17:43:26 +01:00
DummyMigration,
AddUsedToMagicLinksMigration,
MakeMagicLinksSessionNotUniqueMigration,
AddPasswordToUsersMigration,
2021-03-30 16:19:37 +02:00
DummyMigration,
ReplaceTtlWithExpiresAtFilesTable,
2020-04-23 18:07:55 +02:00
];
}
protected async init(): Promise<void> {
this.registerComponents();
this.registerWebSocketListeners();
this.registerControllers();
}
private registerComponents() {
2020-08-11 17:54:15 +02:00
// Base
this.use(new ExpressAppComponent(this.addr, this.port));
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
2021-11-20 19:54:59 +01:00
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());
2020-08-11 17:54:15 +02:00
2020-04-23 18:07:55 +02:00
// Maintenance
2021-11-20 19:54:59 +01:00
this.use(new MaintenanceComponent());
2020-05-05 15:26:28 +02:00
this.use(new AutoUpdateComponent());
2020-04-23 18:07:55 +02:00
// Services
this.use(new MysqlComponent());
2021-11-20 19:54:59 +01:00
this.use(new MailComponent(new MailViewEngine(intermediateDirectory, ...additionalViewPaths)));
2020-04-23 18:07:55 +02:00
2020-06-14 21:23:57 +02:00
// Session
this.use(new RedisComponent());
this.use(new SessionComponent(this.as(RedisComponent)));
2021-01-25 17:43:26 +01:00
this.use(new AuthComponent(this, new MagicLinkAuthMethod(this, MAGIC_LINK_MAIL), new PasswordAuthMethod(this)));
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, this.as(ExpressAppComponent), this.as(RedisComponent)));
2021-11-20 19:54:59 +01:00
this.use(new WebSocketServerComponent());
2021-03-30 16:31:23 +02:00
// Jobs
this.use(new DeleteOldFilesJobComponent());
2020-04-23 18:07:55 +02:00
}
private registerWebSocketListeners() {
2020-11-22 13:49:08 +01:00
this.use(new MagicLinkWebSocketListener());
2020-04-23 18:07:55 +02:00
}
private registerControllers() {
// Multi-domain + vhost
this.use(new LinkController());
2020-06-14 13:01:52 +02:00
// Priority
2021-01-25 17:43:26 +01:00
this.use(new MailController());
2020-06-14 13:01:52 +02:00
this.use(new AuthController());
2020-11-22 13:49:08 +01:00
this.use(new MagicLinkController(this.as<MagicLinkWebSocketListener<this>>(MagicLinkWebSocketListener)));
2020-06-14 13:01:52 +02:00
2021-01-25 17:43:26 +01:00
// Core
this.use(new AccountController());
this.use(new BackendController());
2020-06-14 13:01:52 +02:00
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());
// 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
}