Use config instead of relying on process.env in server.js
This commit is contained in:
parent
b05e60fbcc
commit
c646fefec1
5
src/config/index.js
Normal file
5
src/config/index.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
force_db_sync: process.env.FORCE_DB_SYNC,
|
||||||
|
node_env: process.env.NODE_ENV,
|
||||||
|
port: process.env.PORT
|
||||||
|
};
|
||||||
@ -1,5 +1,3 @@
|
|||||||
require("dotenv").config();
|
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
@ -10,6 +8,9 @@ const db = models.db;
|
|||||||
const {sequelize, Sequelize} = db;
|
const {sequelize, Sequelize} = db;
|
||||||
const Role = db.role;
|
const Role = db.role;
|
||||||
|
|
||||||
|
const authConfig = require("./config/auth.config");
|
||||||
|
const config = require("./config");
|
||||||
|
|
||||||
const createApp = () => {
|
const createApp = () => {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@ -18,22 +19,22 @@ const createApp = () => {
|
|||||||
|
|
||||||
// CORS restrict in production (set ALLOWED_ORIGINS env to comma-separated list)
|
// CORS restrict in production (set ALLOWED_ORIGINS env to comma-separated list)
|
||||||
const corsOptions = {
|
const corsOptions = {
|
||||||
origin: process.env.NODE_ENV === "production"
|
origin: config.NODE_ENV === "production"
|
||||||
? (process.env.ALLOWED_ORIGINS || "").split(",").map(s => s.trim())
|
? (config.ALLOWED_ORIGINS || "").split(",").map(s => s.trim())
|
||||||
: true,
|
: true,
|
||||||
optionsSuccessStatus: 200
|
optionsSuccessStatus: 200
|
||||||
};
|
};
|
||||||
app.use(cors(corsOptions));
|
app.use(cors(corsOptions));
|
||||||
|
|
||||||
// Logging verbose in dev
|
// Logging verbose in dev
|
||||||
app.use(morgan(process.env.NODE_ENV === "production" ? "combined" : "dev"));
|
app.use(morgan(config.NODE_ENV === "production" ? "combined" : "dev"));
|
||||||
|
|
||||||
// Body parsing with size limits
|
// Body parsing with size limits
|
||||||
app.use(express.json({limit: "10kb"}));
|
app.use(express.json({limit: "10kb"}));
|
||||||
app.use(express.urlencoded({extended: false, limit: "10kb"}));
|
app.use(express.urlencoded({extended: false, limit: "10kb"}));
|
||||||
|
|
||||||
// Apply rate limiting in production
|
// Apply rate limiting in production
|
||||||
if (process.env.NODE_ENV === "production") {
|
if (config.NODE_ENV === "production") {
|
||||||
app.use(rateLimit({
|
app.use(rateLimit({
|
||||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||||
max: 100, // limit per IP
|
max: 100, // limit per IP
|
||||||
@ -74,12 +75,12 @@ const initRoles = async () => {
|
|||||||
const start = async () => {
|
const start = async () => {
|
||||||
try {
|
try {
|
||||||
// Validate important env vars
|
// Validate important env vars
|
||||||
if (!process.env.JWT_SECRET) {
|
if (!authConfig.secret) {
|
||||||
console.warn("JWT_SECRET not set. Ensure secure secret in production.");
|
console.warn("JWT_SECRET not set. Ensure secure secret in production.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync DB (not forced in production)
|
// Sync DB (not forced in production)
|
||||||
const doForce = process.env.FORCE_DB_SYNC === "true" && process.env.NODE_ENV !== "production";
|
const doForce = config.FORCE_DB_SYNC === "true" && config.NODE_ENV !== "production";
|
||||||
await sequelize.authenticate();
|
await sequelize.authenticate();
|
||||||
await sequelize.sync({force: doForce});
|
await sequelize.sync({force: doForce});
|
||||||
if (doForce) console.info("Database dropped and resynced (force=true).");
|
if (doForce) console.info("Database dropped and resynced (force=true).");
|
||||||
@ -89,7 +90,7 @@ const start = async () => {
|
|||||||
const app = createApp();
|
const app = createApp();
|
||||||
|
|
||||||
// Set port and listen
|
// Set port and listen
|
||||||
const PORT = parseInt(process.env.PORT, 10) || 5000;
|
const PORT = parseInt(config.PORT, 10) || 5000;
|
||||||
const server = app.listen(PORT, () => {
|
const server = app.listen(PORT, () => {
|
||||||
console.log(`Server listening on port ${PORT}`);
|
console.log(`Server listening on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user