Uecko_ERP/apps/server/src/app.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-02-03 18:03:23 +00:00
import { logger } from "@common/infrastructure/logger";
import { globalErrorHandler } from "@common/presentation";
2025-02-07 11:14:36 +00:00
import { createAuthProvider } from "@contexts/auth/infraestructure";
2025-01-30 10:45:31 +00:00
import dotenv from "dotenv";
2025-01-29 16:01:17 +00:00
import express, { Application } from "express";
import helmet from "helmet";
2025-01-30 10:45:31 +00:00
import responseTime from "response-time";
2025-02-01 21:48:13 +00:00
import { v1Routes } from "./routes";
2025-01-29 16:01:17 +00:00
dotenv.config();
export function createApp(): Application {
const app = express();
2025-02-01 21:48:13 +00:00
app.set("port", process.env.PORT ?? 3002);
2025-01-29 16:01:17 +00:00
// secure apps by setting various HTTP headers
app.use(helmet());
app.disable("x-powered-by");
// Middlewares
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));
2025-02-01 21:48:13 +00:00
app.use(responseTime()); // set up the response-time middleware
2025-01-29 16:01:17 +00:00
2025-02-07 11:14:36 +00:00
// Inicializar Auth Provider
2025-02-05 20:40:59 +00:00
app.use((req, res, next) => {
2025-02-07 11:14:36 +00:00
createAuthProvider().initialize();
2025-02-05 20:40:59 +00:00
next();
});
2025-02-01 21:48:13 +00:00
2025-02-03 13:12:36 +00:00
app.use((req, _, next) => {
2025-02-03 19:50:16 +00:00
logger.info(`▶️ Incoming request ${req.method} to ${req.path}`);
2025-02-03 13:12:36 +00:00
next();
});
2025-02-01 21:48:13 +00:00
// Registrar rutas de la API
app.use("/api/v1", v1Routes());
2025-01-29 16:01:17 +00:00
2025-02-01 21:48:13 +00:00
// Gestión global de errores
2025-02-03 18:03:23 +00:00
app.use(globalErrorHandler);
2025-01-29 16:01:17 +00:00
return app;
}