2025-02-03 18:03:23 +00:00
|
|
|
import { logger } from "@common/infrastructure/logger";
|
|
|
|
|
import { globalErrorHandler } from "@common/presentation";
|
2025-02-15 21:30:12 +00:00
|
|
|
import { authProvider } 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.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-04-01 15:32:53 +00:00
|
|
|
// secure apps by setting various HTTP headers
|
|
|
|
|
app.use(helmet());
|
|
|
|
|
|
|
|
|
|
// Middleware global para desactivar la caché en todas las rutas
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
|
|
|
|
|
res.setHeader("Pragma", "no-cache");
|
|
|
|
|
res.setHeader("Expires", "0");
|
|
|
|
|
res.setHeader("etag", "false");
|
|
|
|
|
next(); // Continúa con la siguiente función middleware o la ruta
|
|
|
|
|
});
|
|
|
|
|
|
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-15 21:30:12 +00:00
|
|
|
authProvider.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-04-01 14:26:15 +00:00
|
|
|
logger.info(`▶️ Incoming request ${req.method} to ${req.path}`);
|
2025-02-03 13:12:36 +00:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-01 15:32:53 +00:00
|
|
|
// Registrar rutas de la API
|
|
|
|
|
app.use("/api/v1", v1Routes());
|
|
|
|
|
|
2025-04-22 08:11:50 +00:00
|
|
|
// Gestión global de errores.
|
|
|
|
|
// Siempre al final de la cadena de middlewares
|
|
|
|
|
// y después de las rutas.
|
|
|
|
|
app.use(globalErrorHandler);
|
|
|
|
|
|
2025-01-29 16:01:17 +00:00
|
|
|
return app;
|
|
|
|
|
}
|