2020-04-23 18:07:55 +02:00
|
|
|
import Application from "wms-core/Application";
|
|
|
|
import {Type} from "wms-core/Utils";
|
|
|
|
import Migration from "wms-core/db/Migration";
|
|
|
|
import ExpressAppComponent from "wms-core/components/ExpressAppComponent";
|
|
|
|
import NunjucksComponent from "wms-core/components/NunjucksComponent";
|
|
|
|
import MysqlComponent from "wms-core/components/MysqlComponent";
|
|
|
|
import LogRequestsComponent from "wms-core/components/LogRequestsComponent";
|
|
|
|
import RedisComponent from "wms-core/components/RedisComponent";
|
|
|
|
import ServeStaticDirectoryComponent from "wms-core/components/ServeStaticDirectoryComponent";
|
|
|
|
import MaintenanceComponent from "wms-core/components/MaintenanceComponent";
|
|
|
|
import MailComponent from "wms-core/components/MailComponent";
|
|
|
|
import SessionComponent from "wms-core/components/SessionComponent";
|
|
|
|
import FormHelperComponent from "wms-core/components/FormHelperComponent";
|
|
|
|
import CsrfProtectionComponent from "wms-core/components/CsrfProtectionComponent";
|
|
|
|
import WebSocketServerComponent from "wms-core/components/WebSocketServerComponent";
|
|
|
|
import HomeController from "./controllers/HomeController";
|
2020-07-20 16:34:35 +02:00
|
|
|
import AuthController from "./controllers/AuthController";
|
|
|
|
import AuthComponent from "wms-core/auth/AuthComponent";
|
|
|
|
import AuthGuard from "wms-core/auth/AuthGuard";
|
2020-07-25 10:53:54 +02:00
|
|
|
import {PasswordAuthProof} from "./models/UserPasswordComponent";
|
2020-07-20 16:34:35 +02:00
|
|
|
import LDAPServerComponent from "./LDAPServerComponent";
|
2020-05-05 15:26:28 +02:00
|
|
|
import AutoUpdateComponent from "wms-core/components/AutoUpdateComponent";
|
2020-07-20 16:34:35 +02:00
|
|
|
import AccountController from "./controllers/AccountController";
|
|
|
|
import CreateMigrationsTable from "wms-core/migrations/CreateMigrationsTable";
|
|
|
|
import CreateLogsTable from "wms-core/migrations/CreateLogsTable";
|
|
|
|
import CreateUsersAndUserEmailsTable from "wms-core/auth/migrations/CreateUsersAndUserEmailsTable";
|
2020-07-25 10:53:54 +02:00
|
|
|
import AddPasswordToUsers from "./migrations/AddPasswordToUsers";
|
2020-07-20 16:34:35 +02:00
|
|
|
import CreateMagicLinksTable from "wms-core/auth/migrations/CreateMagicLinksTable";
|
|
|
|
import MailController from "wms-core/auth/MailController";
|
|
|
|
import MagicLinkController from "./controllers/MagicLinkController";
|
|
|
|
import MagicLinkWebSocketListener from "wms-core/auth/magic_link/MagicLinkWebSocketListener";
|
2020-07-25 10:53:54 +02:00
|
|
|
import BackendController from "wms-core/helpers/BackendController";
|
|
|
|
import AddApprovedFieldToUsersTable from "wms-core/auth/migrations/AddApprovedFieldToUsersTable";
|
|
|
|
import FixUserMainEmailRelation from "wms-core/auth/migrations/FixUserMainEmailRelation";
|
|
|
|
import DropNameFromUsers from "wms-core/auth/migrations/DropNameFromUsers";
|
|
|
|
import MagicLink from "wms-core/auth/models/MagicLink";
|
|
|
|
import AddNameToUsers from "./migrations/AddNameToUsers";
|
2020-07-27 16:02:09 +02:00
|
|
|
import CreateMailTables from "./migrations/CreateMailTables";
|
|
|
|
import MailboxBackendController from "./controllers/MailboxBackendController";
|
2020-04-23 18:07:55 +02:00
|
|
|
|
2020-06-27 17:20:06 +02:00
|
|
|
export default class App extends Application {
|
2020-04-23 18:07:55 +02:00
|
|
|
private readonly port: number;
|
2020-07-20 16:34:35 +02:00
|
|
|
private magicLinkWebSocketListener?: MagicLinkWebSocketListener;
|
2020-04-23 18:07:55 +02:00
|
|
|
|
|
|
|
constructor(port: number) {
|
|
|
|
super(require('../package.json').version);
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMigrations(): Type<Migration>[] {
|
|
|
|
return [
|
|
|
|
CreateMigrationsTable,
|
|
|
|
CreateLogsTable,
|
2020-07-20 16:34:35 +02:00
|
|
|
CreateUsersAndUserEmailsTable,
|
2020-07-25 10:53:54 +02:00
|
|
|
AddPasswordToUsers,
|
2020-07-20 16:34:35 +02:00
|
|
|
CreateMagicLinksTable,
|
2020-07-25 10:53:54 +02:00
|
|
|
AddApprovedFieldToUsersTable,
|
|
|
|
FixUserMainEmailRelation,
|
|
|
|
DropNameFromUsers,
|
|
|
|
AddNameToUsers,
|
2020-07-27 16:02:09 +02:00
|
|
|
CreateMailTables,
|
2020-04-23 18:07:55 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
|
|
|
this.registerWebSocketListeners();
|
|
|
|
this.registerControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private registerComponents() {
|
|
|
|
const redisComponent = new RedisComponent();
|
|
|
|
const mysqlComponent = new MysqlComponent();
|
|
|
|
|
|
|
|
const expressAppComponent = new ExpressAppComponent(this.port);
|
|
|
|
this.use(expressAppComponent);
|
|
|
|
this.use(new NunjucksComponent());
|
|
|
|
this.use(new LogRequestsComponent());
|
|
|
|
|
|
|
|
// Static files
|
|
|
|
this.use(new ServeStaticDirectoryComponent('public'));
|
|
|
|
this.use(new ServeStaticDirectoryComponent('node_modules/feather-icons/dist', '/icons'));
|
|
|
|
|
|
|
|
// Maintenance
|
|
|
|
this.use(new MaintenanceComponent(this, () => {
|
|
|
|
return redisComponent.canServe() && mysqlComponent.canServe();
|
|
|
|
}));
|
2020-05-05 15:26:28 +02:00
|
|
|
this.use(new AutoUpdateComponent());
|
2020-04-23 18:07:55 +02:00
|
|
|
|
|
|
|
// Services
|
|
|
|
this.use(mysqlComponent);
|
|
|
|
this.use(new MailComponent());
|
|
|
|
|
|
|
|
// Session
|
|
|
|
this.use(redisComponent);
|
|
|
|
this.use(new SessionComponent(redisComponent));
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
this.use(new CsrfProtectionComponent());
|
|
|
|
|
2020-07-20 16:34:35 +02:00
|
|
|
// Auth
|
2020-07-25 10:53:54 +02:00
|
|
|
this.use(new AuthComponent(new class extends AuthGuard<PasswordAuthProof | MagicLink> {
|
|
|
|
public async getProofForSession(session: Express.Session): Promise<PasswordAuthProof | MagicLink | null> {
|
|
|
|
return PasswordAuthProof.getProofForSession(session) ||
|
|
|
|
MagicLink.bySessionID(session.id);
|
2020-07-20 16:34:35 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2020-04-23 18:07:55 +02:00
|
|
|
// WebSocket server
|
|
|
|
this.use(new WebSocketServerComponent(this, expressAppComponent, redisComponent));
|
2020-07-20 16:34:35 +02:00
|
|
|
|
|
|
|
// LDAP server
|
|
|
|
this.use(new LDAPServerComponent());
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private registerWebSocketListeners() {
|
2020-07-20 16:34:35 +02:00
|
|
|
this.magicLinkWebSocketListener = new MagicLinkWebSocketListener();
|
|
|
|
this.use(this.magicLinkWebSocketListener);
|
2020-04-23 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private registerControllers() {
|
2020-07-20 16:34:35 +02:00
|
|
|
// Priority routes / interrupting middlewares
|
|
|
|
this.use(new AccountController());
|
|
|
|
this.use(new MagicLinkController(this.magicLinkWebSocketListener!))
|
2020-07-25 10:53:54 +02:00
|
|
|
this.use(new BackendController());
|
2020-07-27 16:02:09 +02:00
|
|
|
this.use(new MailboxBackendController());
|
2020-07-25 10:53:54 +02:00
|
|
|
this.use(new AuthController());
|
2020-07-20 16:34:35 +02:00
|
|
|
|
|
|
|
// Core functionality
|
|
|
|
this.use(new MailController());
|
|
|
|
|
|
|
|
// Other functionnality
|
|
|
|
|
|
|
|
// Semi-static routes
|
2020-04-23 18:07:55 +02:00
|
|
|
this.use(new HomeController());
|
|
|
|
}
|
|
|
|
}
|