rainbox.email/src/App.ts

146 lines
6.3 KiB
TypeScript
Raw Normal View History

2021-01-25 18:04:11 +01:00
import Application from "swaf/Application";
import Migration, {MigrationType} from "swaf/db/Migration";
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";
2021-01-25 18:04:11 +01:00
import AuthComponent from "swaf/auth/AuthComponent";
import LDAPServerComponent from "./LDAPServerComponent";
2021-01-25 18:04:11 +01:00
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
import DummyMigration from "swaf/migrations/DummyMigration";
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
import AccountMailboxController from "./controllers/AccountMailboxController";
import CreateMigrationsTable from "swaf/migrations/CreateMigrationsTable";
import MagicLinkWebSocketListener from "swaf/auth/magic_link/MagicLinkWebSocketListener";
import BackendController from "swaf/helpers/BackendController";
import CreateMailTablesMigration from "./migrations/CreateMailTablesMigration";
import MailboxBackendController from "./controllers/backend/MailboxBackendController";
import MailAutoConfigController from "./controllers/MailAutoConfigController";
import AccountBackendController from "./controllers/backend/AccountBackendController";
2021-01-25 18:04:11 +01:00
import CreateUsersAndUserEmailsTableMigration from "swaf/auth/migrations/CreateUsersAndUserEmailsTableMigration";
import AddPasswordToUsersMigration from "swaf/auth/password/AddPasswordToUsersMigration";
import CreateMagicLinksTableMigration from "swaf/auth/magic_link/CreateMagicLinksTableMigration";
import AddApprovedFieldToUsersTableMigration from "swaf/auth/migrations/AddApprovedFieldToUsersTableMigration";
import AddNameToUsersMigration from "swaf/auth/migrations/AddNameToUsersMigration";
import PreviousUrlComponent from "swaf/components/PreviousUrlComponent";
import MagicLinkAuthMethod from "swaf/auth/magic_link/MagicLinkAuthMethod";
import {MAGIC_LINK_MAIL} from "swaf/Mails";
import PasswordAuthMethod from "swaf/auth/password/PasswordAuthMethod";
import MagicLinkController from "swaf/auth/magic_link/MagicLinkController";
import AuthController from "swaf/auth/AuthController";
import MailController from "swaf/mail/MailController";
import AccountController from "swaf/auth/AccountController";
import packageJson = require('./package.json');
2021-01-25 18:04:11 +01:00
import AddUsedToMagicLinksMigration from "swaf/auth/magic_link/AddUsedToMagicLinksMigration";
import MakeMagicLinksSessionNotUniqueMigration from "swaf/auth/magic_link/MakeMagicLinksSessionNotUniqueMigration";
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,
2021-01-25 18:04:11 +01:00
CreateUsersAndUserEmailsTableMigration,
AddPasswordToUsersMigration,
CreateMagicLinksTableMigration,
AddApprovedFieldToUsersTableMigration,
DummyMigration,
DummyMigration,
AddNameToUsersMigration,
CreateMailTablesMigration,
2020-11-02 18:17:55 +01:00
DropLegacyLogsTable,
2021-01-25 18:04:11 +01:00
AddUsedToMagicLinksMigration,
MakeMagicLinksSessionNotUniqueMigration,
2020-04-23 18:07:55 +02:00
];
}
protected async init(): Promise<void> {
this.registerComponents();
this.registerWebSocketListeners();
this.registerControllers();
}
private registerComponents() {
2020-07-29 15:03:54 +02: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'));
2020-07-29 15:03:54 +02:00
// Dynamic views and routes
this.use(new NunjucksComponent());
this.use(new PreviousUrlComponent());
2020-07-29 15:03:54 +02:00
2020-04-23 18:07:55 +02:00
// Maintenance
this.use(new MaintenanceComponent(this, () => {
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
this.use(new MysqlComponent());
2020-04-23 18:07:55 +02:00
this.use(new MailComponent());
// Session
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());
// Auth
2021-01-25 18:04:11 +01:00
this.use(new AuthComponent(this, new MagicLinkAuthMethod(this, MAGIC_LINK_MAIL), new PasswordAuthMethod(this)));
2020-04-23 18:07:55 +02:00
// WebSocket server
this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent)));
// LDAP server
this.use(new LDAPServerComponent());
2020-04-23 18:07:55 +02:00
}
private registerWebSocketListeners() {
2021-01-25 18:04:11 +01:00
this.use(new MagicLinkWebSocketListener());
2020-04-23 18:07:55 +02:00
}
private registerControllers() {
// Priority routes / interrupting middlewares
2021-01-25 18:04:11 +01:00
this.use(new MailAutoConfigController()); // Needs to override MailController
this.use(new MailController());
this.use(new AuthController());
this.use(new AccountController());
2021-01-25 18:04:11 +01:00
this.use(new AccountMailboxController());
this.use(new MagicLinkController(this.as<MagicLinkWebSocketListener<this>>(MagicLinkWebSocketListener)));
// Core functionality
this.use(new BackendController());
2020-07-27 16:02:09 +02:00
this.use(new MailboxBackendController());
this.use(new AccountBackendController());
// Other functionnality
// Semi-static routes
2020-04-23 18:07:55 +02:00
this.use(new HomeController());
}
}