ily.li/src/App.ts

161 lines
6.5 KiB
TypeScript
Raw Normal View History

2020-11-22 13:49:08 +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-06-14 13:01:52 +02:00
import AboutController from "./controllers/AboutController";
2020-11-22 13:49:08 +01:00
import AutoUpdateComponent from "swaf/components/AutoUpdateComponent";
2020-06-14 13:01:52 +02:00
import AuthController from "./controllers/AuthController";
2020-11-22 13:49:08 +01:00
import MagicLinkWebSocketListener from "swaf/auth/magic_link/MagicLinkWebSocketListener";
2020-06-14 13:01:52 +02:00
import MagicLinkController from "./controllers/MagicLinkController";
2020-11-22 13:49:08 +01:00
import MailController from "swaf/auth/MailController";
2020-06-14 13:01:52 +02:00
import FileController from "./controllers/FileController";
2020-11-22 13:49:08 +01:00
import CreateUsersAndUserEmailsTable from "swaf/auth/migrations/CreateUsersAndUserEmailsTable";
import CreateMagicLinksTable from "swaf/auth/migrations/CreateMagicLinksTable";
2020-06-14 13:01:52 +02:00
import CreateAuthTokensTable from "./migrations/CreateAuthTokensTable";
2020-11-22 13:49:08 +01:00
import AuthComponent from "swaf/auth/AuthComponent";
import AuthGuard from "swaf/auth/AuthGuard";
import MagicLink from "swaf/auth/models/MagicLink";
2020-06-14 13:01:52 +02:00
import AuthToken from "./models/AuthToken";
import {MagicLinkActionType} from "./controllers/MagicLinkActionType";
import {Request} from "express";
import CreateFilesTable from "./migrations/CreateFilesTable";
import IncreaseFilesSizeField from "./migrations/IncreaseFilesSizeField";
2020-11-22 13:49:08 +01:00
import AddApprovedFieldToUsersTable from "swaf/auth/migrations/AddApprovedFieldToUsersTable";
2020-07-06 10:49:18 +02:00
import CreateUrlRedirectsTable from "./migrations/CreateUrlRedirectsTable";
import AuthTokenController from "./controllers/AuthTokenController";
import URLRedirectController from "./controllers/URLRedirectController";
import LinkController from "./controllers/LinkController";
2020-11-22 13:49:08 +01:00
import BackendController from "swaf/helpers/BackendController";
import RedirectBackComponent from "swaf/components/RedirectBackComponent";
import DummyMigration from "swaf/migrations/DummyMigration";
import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable";
import packageJson = require('./package.json');
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(
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-22 13:49:08 +01:00
DummyMigration,
2020-06-14 13:01:52 +02:00
CreateUsersAndUserEmailsTable,
CreateMagicLinksTable,
CreateAuthTokensTable,
CreateFilesTable,
IncreaseFilesSizeField,
2020-06-27 16:31:36 +02:00
AddApprovedFieldToUsersTable,
2020-07-06 10:49:18 +02:00
CreateUrlRedirectsTable,
2020-11-22 13:49:08 +01:00
DropLegacyLogsTable,
2020-11-27 15:12:00 +01:00
FixUserMainEmailRelation,
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
this.use(new NunjucksComponent());
this.use(new PreviousUrlComponent());
2020-08-11 17:54:15 +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());
2020-06-14 21:23:57 +02:00
// Session
this.use(new RedisComponent());
this.use(new SessionComponent(this.as(RedisComponent)));
2020-06-14 13:01:52 +02:00
this.use(new AuthComponent(new class extends AuthGuard<MagicLink | AuthToken> {
2020-11-22 14:13:57 +01:00
public async getProofForSession(session: Session): Promise<MagicLink | AuthToken | null> {
2020-11-22 13:49:08 +01:00
return await MagicLink.bySessionId(
session.id,
[MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER],
);
2020-06-14 13:01:52 +02:00
}
public async getProofForRequest(req: Request): Promise<MagicLink | AuthToken | null> {
const authorization = req.header('Authorization');
if (authorization) {
2020-06-27 16:31:36 +02:00
const token = await AuthToken.select().where('secret', authorization).first();
2020-06-14 21:23:57 +02:00
if (token) {
token.use();
await token.save();
}
return token;
2020-06-14 13:01:52 +02:00
}
2020-11-22 14:13:57 +01:00
return await super.getProofForRequest(req);
2020-06-14 13:01:52 +02:00
}
2020-11-22 13:49:08 +01:00
}(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)));
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
this.use(new AuthController());
2020-11-22 13:49:08 +01:00
this.use(new MagicLinkController(this.as<MagicLinkWebSocketListener<this>>(MagicLinkWebSocketListener)));
2020-06-27 16:31:36 +02:00
this.use(new BackendController());
2020-06-14 13:01:52 +02:00
// Core functionality
this.use(new MailController());
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
}