swaf-boilerplate/src/App.ts

106 lines
4.4 KiB
TypeScript

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 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 HomeController from "./controllers/HomeController";
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
import DummyMigration from "swaf/migrations/DummyMigration";
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
import PreviousUrlComponent from "swaf/components/PreviousUrlComponent";
import MailViewEngine from "swaf/frontend/MailViewEngine";
import AssetCompiler from "swaf/frontend/AssetCompiler";
import FrontendToolsComponent from "swaf/components/FrontendToolsComponent";
import CopyAssetPreCompiler from "swaf/frontend/CopyAssetPreCompiler";
import ScssAssetPreCompiler from "swaf/frontend/ScssAssetPreCompiler";
import TypeScriptPreCompiler from "swaf/frontend/TypeScriptPreCompiler";
import SvelteViewEngine from "swaf/frontend/SvelteViewEngine";
import NunjucksViewEngine from "swaf/frontend/NunjucksViewEngine";
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,
DropLegacyLogsTable,
];
}
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)));
// Utils
this.use(new FormHelperComponent());
// Middlewares
this.use(new CsrfProtectionComponent());
// WebSocket server
this.use(new WebSocketServerComponent());
}
private registerWebSocketListeners() {
// WebSocket listeners
}
private registerControllers() {
this.use(new HomeController());
}
}