2022-01-06 01:26:15 +00:00
|
|
|
import "./bootstrap";
|
|
|
|
import "reflect-metadata";
|
|
|
|
import "express-async-errors";
|
|
|
|
import express, { Request, Response, NextFunction } from "express";
|
|
|
|
import cors from "cors";
|
|
|
|
import cookieParser from "cookie-parser";
|
|
|
|
import * as Sentry from "@sentry/node";
|
|
|
|
|
|
|
|
import "./database";
|
|
|
|
import uploadConfig from "./config/upload";
|
|
|
|
import AppError from "./errors/AppError";
|
|
|
|
import routes from "./routes";
|
2023-07-21 15:55:34 +00:00
|
|
|
import { logger } from "./utils/logger";
|
|
|
|
|
|
|
|
// Swagger SLM
|
|
|
|
const swaggerUI = require("swagger-ui-express");
|
|
|
|
const YAML = require("yamljs");
|
|
|
|
const swaggerDocument = YAML.load("./swagger.yaml");
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
credentials: true,
|
|
|
|
origin: process.env.FRONTEND_URL
|
|
|
|
})
|
|
|
|
);
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(Sentry.Handlers.requestHandler());
|
|
|
|
app.use("/public", express.static(uploadConfig.directory));
|
2023-07-21 15:55:34 +00:00
|
|
|
|
|
|
|
app.get("/", (req: Request, res: Response) => {
|
|
|
|
res.send('<h1>SLM/OMNIHIT api</h1><a href="/api-docs">Documentation</a>');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
app.use(routes);
|
|
|
|
|
|
|
|
app.use(Sentry.Handlers.errorHandler());
|
|
|
|
|
|
|
|
app.use(async (err: Error, req: Request, res: Response, _: NextFunction) => {
|
|
|
|
if (err instanceof AppError) {
|
|
|
|
logger.warn(err);
|
|
|
|
return res.status(err.statusCode).json({ error: err.message });
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error(err);
|
|
|
|
return res.status(500).json({ error: "Internal server error" });
|
|
|
|
});
|
|
|
|
|
|
|
|
export default app;
|