diff --git a/src/app.ts b/src/app.ts index e69de29..1a6a53d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -0,0 +1,66 @@ +import * as process from "node:process"; +import { MongodbService } from "@services/databases/mongodb.service"; +import { EnvUtils } from "@utils/env.util"; +import { LogsUtils } from "@utils/logs.util"; +import compression from "compression"; +import cors from "cors"; +import express, { type Express } from "express"; +import helmet from "helmet"; + +console.log("\n\n> Starting...\n\n\n\n"); + +const logger = new LogsUtils("App"); +const envs = new EnvUtils("App"); + +const app: Express = express(); + +// enable cors +app.use(cors()); +app.options("*", cors()); + +// enable xss sanitizer +app.use( + helmet({ + xXssProtection: true, + }), +); +app.use(helmet.xXssProtection()); + +// parse json request body +app.use(express.json()); + +// parse urlencoded request body +app.use( + express.urlencoded({ + extended: true, + }), +); + +// gzip compression +app.use(compression()); + +try { + //app.use("/auth", AuthRouter); + logger.info("Routers loaded !"); +} catch (err) { + logger.error(err); + throw null; +} + +try { + const port = envs.get("APP_PORT") || "3000"; + app.listen(Number.parseInt(port)); + logger.info( + `Server is running ! (port:${port})`, + `Memory total: ${Math.round( + process.memoryUsage().rss / 1_000_000, + )} Mio\n - Memory used by V8: ${Math.round( + process.memoryUsage().heapUsed / 1_000_000, + )} Mio`, + ); +} catch (error) { + logger.error(`Server failed to start: ${error}`); + process.exit(1); +} + +const mongo = new MongodbService("App"); \ No newline at end of file