2020-11-13 10:54:35 +01:00
|
|
|
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 NunjucksComponent from "swaf/components/NunjucksComponent";
|
|
|
|
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";
|
2020-04-23 18:07:55 +02:00
|
|
|
import HomeController from "./controllers/HomeController";
|
2020-11-13 10:54:35 +01:00
|
|
|
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
|
|
|
|
import DummyMigration from "swaf/migrations/DummyMigration";
|
|
|
|
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
|
2021-01-25 13:17:41 +01:00
|
|
|
import PreviousUrlComponent from "swaf/components/PreviousUrlComponent";
|
|
|
|
import packageJson = require('./package.json');
|
2020-04-23 18:07:55 +02:00
|
|
|
|
2020-06-27 17:20:06 +02:00
|
|
|
export default class App extends Application {
|
2020-10-05 13:38:03 +02:00
|
|
|
public constructor(
|
|
|
|
private readonly addr: string,
|
|
|
|
private readonly port: number,
|
|
|
|
) {
|
|
|
|
super(packageJson.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-02 18:17:55 +01:00
|
|
|
DummyMigration,
|
|
|
|
DropLegacyLogsTable,
|
2020-04-23 18:07:55 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
|
|
|
this.registerWebSocketListeners();
|
|
|
|
this.registerControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private registerComponents() {
|
2021-01-25 13:17:41 +01:00
|
|
|
// Base
|
|
|
|
this.use(new ExpressAppComponent(this.addr, this.port));
|
2020-04-23 18:07:55 +02:00
|
|
|
this.use(new LogRequestsComponent());
|
|
|
|
|
|
|
|
// Static files
|
|
|
|
this.use(new ServeStaticDirectoryComponent('public'));
|
|
|
|
this.use(new ServeStaticDirectoryComponent('node_modules/feather-icons/dist', '/icons'));
|
|
|
|
|
2021-01-25 13:17:41 +01:00
|
|
|
// Dynamic views and routes
|
|
|
|
this.use(new NunjucksComponent());
|
|
|
|
this.use(new PreviousUrlComponent());
|
|
|
|
|
2020-04-23 18:07:55 +02:00
|
|
|
// Maintenance
|
|
|
|
this.use(new MaintenanceComponent(this, () => {
|
2021-01-25 13:17:41 +01:00
|
|
|
return this.as(RedisComponent).canServe() && this.as(MysqlComponent).canServe();
|
2020-04-23 18:07:55 +02:00
|
|
|
}));
|
2020-05-05 15:26:28 +02:00
|
|
|
this.use(new AutoUpdateComponent());
|
2020-04-23 18:07:55 +02:00
|
|
|
|
|
|
|
// Services
|
2021-01-25 13:17:41 +01:00
|
|
|
this.use(new MysqlComponent());
|
2020-04-23 18:07:55 +02:00
|
|
|
this.use(new MailComponent());
|
|
|
|
|
|
|
|
// Session
|
2021-01-25 13:17:41 +01:00
|
|
|
this.use(new RedisComponent());
|
|
|
|
this.use(new SessionComponent(this.as(RedisComponent)));
|
2020-04-23 18:07:55 +02:00
|
|
|
|
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
this.use(new CsrfProtectionComponent());
|
|
|
|
|
|
|
|
// WebSocket server
|
2021-01-25 13:17:41 +01:00
|
|
|
this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent)));
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private registerWebSocketListeners() {
|
2020-10-05 13:38:03 +02:00
|
|
|
// WebSocket listeners
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private registerControllers() {
|
|
|
|
this.use(new HomeController());
|
|
|
|
}
|
2020-10-05 13:38:03 +02:00
|
|
|
}
|