113 lines
2.6 KiB
JavaScript
113 lines
2.6 KiB
JavaScript
const express = require("express");
|
|
const database = require("./database");
|
|
const fileUpload = require("express-fileupload");
|
|
const morgan = require("morgan");
|
|
const {
|
|
isUser,
|
|
isAdmin,
|
|
isLoggedIn,
|
|
hasPermission,
|
|
} = require("./authentication");
|
|
// const bodyParser = require("body-parser");
|
|
const cronJobs = require("./plugins/cronJobs");
|
|
const swaggerJsdoc = require("swagger-jsdoc");
|
|
const cors = require("cors");
|
|
const swaggerUi = require("swagger-ui-express");
|
|
const { createAdmins } = require("./plugins/privateAdmin");
|
|
const portals = require("./plugins/portals");
|
|
global._ = require("lodash");
|
|
global.lodash = require("lodash-contrib");
|
|
global.portal = [];
|
|
|
|
for (const item of portals) {
|
|
portal.push(item.value);
|
|
}
|
|
|
|
// Create express instance
|
|
const app = express();
|
|
|
|
app.set("trust proxy", 1);
|
|
app.disable("x-powered-by");
|
|
|
|
app.use(morgan("combined"));
|
|
|
|
const options = {
|
|
definition: {
|
|
openapi: "3.0.0",
|
|
info: {
|
|
title: "Ostandari Swagger RestFul API",
|
|
version: "1.0.0",
|
|
description: "API documentation",
|
|
license: {
|
|
name: "MIT",
|
|
},
|
|
},
|
|
components: {
|
|
securitySchemes: {
|
|
bearerAuth: {
|
|
type: "http",
|
|
in: "header",
|
|
name: "Authorization",
|
|
description: "Bearer Token",
|
|
scheme: "bearer",
|
|
bearerFormat: "JWT",
|
|
},
|
|
},
|
|
},
|
|
security: {
|
|
bearerAuth: [],
|
|
},
|
|
servers: [
|
|
{
|
|
url: "http://localhost:3756",
|
|
},
|
|
],
|
|
},
|
|
apis: ["./server/routes/*.js"],
|
|
};
|
|
|
|
const specs = swaggerJsdoc(options);
|
|
|
|
cronJobs();
|
|
createAdmins();
|
|
|
|
const corsOptions = {
|
|
origin: "*",
|
|
credentials: true,
|
|
optionSuccessStatus: 200,
|
|
};
|
|
|
|
// Init body-parser options (inbuilt with express)
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(fileUpload());
|
|
app.use(cors(corsOptions));
|
|
app.use("/v1", swaggerUi.serve, swaggerUi.setup(specs, { explorer: true }));
|
|
// Require & Import API routes
|
|
const Public = require("./routes/public");
|
|
const admin = require("./routes/admin");
|
|
const auth = require("./routes/auth");
|
|
const user = require("./routes/user");
|
|
|
|
// Use API Routes
|
|
app.use("/public", Public);
|
|
app.use("/auth", auth);
|
|
app.use("/admin", [isLoggedIn, isAdmin], admin);
|
|
// app.use('/user', isUser, user);
|
|
|
|
// Error handling middleware to catch buffer issues
|
|
app.use((err, req, res, next) => {
|
|
if (err.code === "ENOBUFS") {
|
|
console.error("Buffer space error:", err);
|
|
res.status(500).send("Server is experiencing buffer space issues.");
|
|
} else {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
// Export the server middleware
|
|
module.exports = {
|
|
path: "/api",
|
|
handler: app,
|
|
};
|