first commit
This commit is contained in:
96
src/app.ts
Normal file
96
src/app.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import path from "node:path";
|
||||
import cookieParser from "cookie-parser";
|
||||
import createHttpError from "http-errors";
|
||||
// import { ServerErrorsObject, ServerResponse } from "./core/types";
|
||||
// import { secureApp } from "./config/secure-app";
|
||||
// import mainRouter from "./core/router/main.router";
|
||||
import dotenv from "dotenv";
|
||||
import { secureApp } from "./core/config/secure-app";
|
||||
import initDB from "./core/config/db-connection";
|
||||
import { ServerErrorsObject, ServerResponse } from "./core/types";
|
||||
import { seedRBAC } from "../seeders/rbac.seed";
|
||||
import mainRouter from "./core/router/main.router";
|
||||
|
||||
// import { seedDepartments } from "./seeders/department.seed";
|
||||
// import { seedUsers } from "./seeders/user.seed";
|
||||
// import { seedRoles } from "./seeders/role.seed";
|
||||
const express = require("express") as typeof import("express");
|
||||
dotenv.config();
|
||||
|
||||
export default class ServerApplication {
|
||||
#PORT = 4000;
|
||||
#APP = express();
|
||||
constructor() {
|
||||
this.serverConfiguration();
|
||||
this.StartApplication();
|
||||
this.InitClientSession();
|
||||
this.RoutesConfiguration();
|
||||
this.ErrorHandlingConfiguration();
|
||||
}
|
||||
|
||||
async serverConfiguration() {
|
||||
// this.#APP.use(secureApp);
|
||||
this.#APP.use(express.json());
|
||||
this.#APP.use(express.urlencoded({ extended: true }));
|
||||
this.#APP.set("json spaces", 2);
|
||||
this.#APP.use(
|
||||
"/media/images",
|
||||
express.static(path.join(__dirname, "..", "media", "images")),
|
||||
);
|
||||
this.#APP.use(
|
||||
"/media/videos",
|
||||
express.static(path.join(__dirname, "..", "media", "videos")),
|
||||
);
|
||||
}
|
||||
async StartApplication() {
|
||||
await initDB();
|
||||
// await seedRBAC();
|
||||
|
||||
|
||||
|
||||
this.#APP.listen(this.#PORT, () => {
|
||||
console.log(
|
||||
`Server Running on PORT ${this.#PORT} url : ${"http://localhost:"}${
|
||||
this.#PORT
|
||||
}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
InitClientSession() {
|
||||
this.#APP.use(cookieParser(process.env.COOKIE_PARSER_SECRET_KEY));
|
||||
}
|
||||
RoutesConfiguration() {
|
||||
// this.#APP.get("/", (req, res) => res.send(""));
|
||||
|
||||
this.#APP.use("/api/v1", mainRouter);
|
||||
|
||||
}
|
||||
ErrorHandlingConfiguration() {
|
||||
this.#APP.use((req: any, res: Response, next: NextFunction) => {
|
||||
next(createHttpError.NotFound("این آدرس یافت نشد"));
|
||||
});
|
||||
this.#APP.use(
|
||||
async (error: any, req: any, res: ServerResponse, next: NextFunction) => {
|
||||
// await ErrorLog.create({
|
||||
// message: error.message,
|
||||
// stack: error.stack,
|
||||
// severity: "HIGH",
|
||||
// });
|
||||
// console.log(error);
|
||||
const serverError = createHttpError.InternalServerError();
|
||||
const statusCode = error.status || serverError.status;
|
||||
const message: string = error.message || serverError.message;
|
||||
|
||||
const errorObject: ServerErrorsObject = {
|
||||
status: statusCode,
|
||||
error: {
|
||||
message,
|
||||
},
|
||||
};
|
||||
|
||||
return res.status(statusCode).json(errorObject);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user