Skip to content

TaskView is a self-hosted project and task management platform focused on clarity, ownership, and control. TaskView is built for teams that want a transparent, self-hosted alternative to SaaS task managers.

4.2 KB
Raw Blame
import express, { type Request, type Response } from 'express';
import helmet from 'helmet';
import { appUserMiddleware } from './middlewares/app-user-middleware';
import { corsMiddleware } from './middlewares/cors';
import errorHandler from './middlewares/error-handler';
import routes from './routes';
import passport, { initPassportLogin } from './tv-modules/auth/strategies/passport-login';
import { LoginMethods } from './tv-modules/auth/LoginMethods';
import SessionStorage from './tv-modules/auth/SessionStorage';
import { AccountDeletion } from './tv-modules/auth/AccountDeletion';
import { InviteEmailDispatcher } from './tv-modules/collaboration/InviteEmailDispatcher';
import { PublicApiUrl } from './modules/public-url';
import { validateWebhooksEnvOnStartup } from './tv-modules/webhooks/webhooks.utils';
import { validateSsoEnvOnStartup } from './tv-modules/sso/sso.utils';
import { FileStorageFactory } from './tv-modules/files/storage/FileStorageFactory';
import cookieParser from 'cookie-parser';
import { registerAllEventHandlers, startAllWorkers } from './core/all-events';
import { ScimBodyErrorHandler } from './tv-modules/scim/middlewares/ScimBodyErrorHandler';

export default class App {
    public app: express.Application;
    public port: number;

    constructor(port: number) {
        LoginMethods.validateOnStartup();
        SessionStorage.validateOnStartup();
        AccountDeletion.validateOnStartup();
        PublicApiUrl.validateOnStartup();
        InviteEmailDispatcher.validateOnStartup();
        validateWebhooksEnvOnStartup();
        validateSsoEnvOnStartup();
        FileStorageFactory.validateOnStartup();

        this.app = express();
        this.port = port;

        this.extendApp();

        this.initializeMiddlewares();
        registerAllEventHandlers();
        this.initializeRoutes();
        this.app.use(errorHandler);
        this.app.use(passport.initialize());
        initPassportLogin();

        this.extendMiddlewares();
    }

    protected extendApp(): void { }
    protected extendMiddlewares(): void { }

    private resolveTrustProxy(): boolean | number | string {
        const raw = process.env.TRUST_PROXY?.trim();
        if (!raw || raw.toLowerCase() === 'false') return false;
        if (raw.toLowerCase() === 'true') return true;
        if (/^\d+$/.test(raw)) return Number(raw);
        return raw;
    }

    private initializeMiddlewares() {
        this.app.set('trust proxy', this.resolveTrustProxy());

        //add tvJson method, clien need response format like {response: data}
        this.app.use((_req: Request, res: Response, next) => {
            res.tvJson = function (data: any) {
                this.json({ response: data });
            };
            next();
        });

        this.app.use(helmet());
        this.app.use(corsMiddleware);
        this.app.use(cookieParser());
        this.app.use(express.json({
            // RFC 7644 §3.1: SCIM clients (Entra ID, Okta, OneLogin) send application/scim+json
            type: ['application/json', 'application/scim+json'],
            verify: (req: any, _res, buf) => {
                // Store raw body for webhook signature verification github and gitlab integrations
                if (req.url?.includes('/webhook/')) {
                    req.rawBody = buf;
                }
            },
        }));
        this.app.use(express.urlencoded({
            extended: true,
            verify: (req: any, _res, buf) => {
                // Slack sends slash commands / interactivity as urlencoded; keep the raw body
                // for HMAC signature verification (VerifySlackRequest).
                if (req.url?.includes('/messaging/slack/')) {
                    req.rawBody = buf;
                }
            },
        }));
        this.app.use(appUserMiddleware);
    }

    private initializeRoutes() {
        for (const i in routes) {
            this.app.use(i, new routes[i]().getRouter());
        }
        this.app.use('/scim/v2', ScimBodyErrorHandler);
    }

    public listen() {
        return this.app.listen(this.port, '0.0.0.0', async () => {
            console.log(`Server is running on port ${this.port}`);
            await startAllWorkers();
        });
    }
}