Files
taskview-community/api/src/App.ts
T
Nikolai Giman af63530390 feat: mcp & sso
2026-04-12 23:14:10 +02:00

92 lines
2.9 KiB
TypeScript

import cors from 'cors';
import express, { type Request, type Response } from 'express';
import helmet from 'helmet';
import { appUserMiddleware } from './middlewares/app-user-middleware';
import errorHandler from './middlewares/error-handler';
import routes from './routes';
import passport, { initPassportLogin } from './tv-modules/auth/strategies/passport-login';
import cookieParser from 'cookie-parser';
import { registerAllEventHandlers, startAllWorkers } from './core/all-events';
const allow = new Set([
...(process.env.CORS_REMOVE_DEFAULT_ALLOWED_ORIGINS === 'true' ? [] : [
// default allowed origins for official TaskView apps
"https://app.taskview.tech",
"https://taskview.handscream.com",
"capacitor://taskview.handscream.com",
"capacitor://app.taskview.tech",
"https://appleid.apple.com"
]),
...(process.env.CORS_ALLOWED_ORIGINS?.split(',') || []),
]);
export default class App {
public app: express.Application;
public port: number;
constructor(port: number) {
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 initializeMiddlewares() {
//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(cookieParser());
this.app.use(appUserMiddleware);
this.app.use(cors({
credentials: true,
origin(origin, cb) {
if (!origin || origin === 'null') return cb(null, true);
if (allow.has(origin)) return cb(null, true);
return cb(new Error(`CORS blocked origin: ${origin}`), false);
},
}));
this.app.use(helmet());
this.app.use(express.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 }));
}
private initializeRoutes() {
for (const i in routes) {
this.app.use(i, new routes[i]().getRouter());
}
}
public listen() {
return this.app.listen(this.port, '0.0.0.0', async () => {
console.log(`Server is running on port ${this.port}`);
await startAllWorkers();
});
}
}