| 1 | import express, { type Application } from 'express'; |
| 2 | import cors from 'cors'; |
| 3 | import helmet from 'helmet'; |
| 4 | import { errorHandler } from '@/middleware/error-handler'; |
| 5 | import { registerRoutes } from '@/routes'; |
| 6 | import { createInternalAuthMiddleware } from '@chatapp/common'; |
| 7 | import { env } from './config/env'; |
| 8 | |
| 9 | export const createApp = (): Application => { |
| 10 | const app = express(); |
| 11 | |
| 12 | app.use(helmet()); |
| 13 | app.use( |
| 14 | cors({ |
| 15 | origin: '*', |
| 16 | credentials: true, |
| 17 | }), |
| 18 | ); |
| 19 | |
| 20 | app.use(express.json()); |
| 21 | app.use(express.urlencoded({ extended: true })); |
| 22 | app.use( |
| 23 | createInternalAuthMiddleware(env.INTERNAL_API_TOKEN, { |
| 24 | exemptPaths: ['/health'], |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | registerRoutes(app); |
| 29 | |
| 30 | app.use((_req, res) => { |
| 31 | res.status(404).json({ message: 'Not Found' }); |
| 32 | }); |
| 33 | |
| 34 | app.use(errorHandler); |
| 35 | |
| 36 | return app; |
| 37 | }; |