fcsc_ipi_backend/server.js
2025-12-04 13:41:50 +05:30

143 lines
3.4 KiB
JavaScript

const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
const morgan = require("morgan");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const routes = require("./app/routes/routes");
const db = require("./app/models");
const path = require("path");
const cookieParser = require("cookie-parser");
require("dotenv").config();
const app = express();
app.use(express.json());
app.use(helmet());
app.use(morgan("dev"));
app.use(cookieParser());
// app.use(cors());
const allowedOrigins = [
"http://localhost:5173/", //local
"http://13.201.47.205:5173/", //dev
"http://13.201.47.205:5175/" //uat
];
app.use(cors({
origin: allowedOrigins,
credentials: true
}));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Credentials", "true");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, APP_SIGNATURE, x-app-signature"
);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE, OPTIONS"
);
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
next();
});
// // Swagger setup
// const swaggerOptions = {
// definition: {
// openapi: "3.0.0",
// info: {
// title: "FCSC IPI Survey",
// version: "1.0.0",
// description: "Federal Competitiveness and Statistics Centre (FCSC) - Industrial Production Index (IPI)",
// },
// components: {
// securitySchemes: {
// bearerAuth: {
// type: "http",
// scheme: "bearer",
// bearerFormat: "JWT",
// },
// },
// },
// security: [
// {
// bearerAuth: [],
// },
// ],
// },
// apis: ["./app/routes/*.js"],
// };
// const swaggerDocs = swaggerJsdoc(swaggerOptions);
// app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Swagger setup
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "FCSC IPI Survey",
version: "1.0.0",
description: "Federal Competitiveness and Statistics Centre (FCSC) - Industrial Production Index (IPI)",
},
components: {
securitySchemes: {
// Optional: Still support Bearer for Swagger-only testing
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
appSignature: {
type: "apiKey",
in: "header",
name: "x-app-signature"
}
},
},
},
apis: ["./app/routes/*.js"],
};
const swaggerDocs = swaggerJsdoc(swaggerOptions);
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(swaggerDocs, {
swaggerOptions: {
withCredentials: true, // 🔥 VERY IMPORTANT
},
})
);
// Routes
app.use("/api", routes);
apis: [path.join(__dirname, "app/routes/*.js")],
app.get("/api/test", (req, res) => {
res.json({ status: "success", message: "Test API working fine 🚀" });
});
//deployment route
const deploymentController = require("./app/controllers/deployment.controller");
app.post("/deploy", deploymentController.deployment);
// Sync DB
// db.sequelize.sync({ alter: true }).then(() => {
// console.log("✅ Database connected & synced.");
// });
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`🚀 Server running on port ${PORT}`);
});