345 lines
11 KiB
JavaScript
345 lines
11 KiB
JavaScript
const express = require("express");
|
|
const cors = require("cors");
|
|
const bodyParser = require("body-parser");
|
|
const path = require("path");
|
|
const compression = require("compression");
|
|
const cookieParser = require("cookie-parser");
|
|
const http = require("http");
|
|
const { Server } = require("socket.io");
|
|
const { createClient } = require("redis");
|
|
const { createAdapter } = require("@socket.io/redis-adapter");
|
|
const logger = require("./server/utils/logger");
|
|
const { redisSocketEvents } = require("./server/web-sockets/redisSocketEvents");
|
|
|
|
// Load environment variables
|
|
require("dotenv").config({
|
|
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
|
});
|
|
|
|
/**
|
|
* CORS Origin for Socket.IO
|
|
* @type {string[][]}
|
|
*/
|
|
const SOCKETIO_CORS_ORIGIN = [
|
|
"https://test.imex.online",
|
|
"https://www.test.imex.online",
|
|
"http://localhost:3000",
|
|
"https://imex.online",
|
|
"https://www.imex.online",
|
|
"https://romeonline.io",
|
|
"https://www.romeonline.io",
|
|
"https://beta.test.romeonline.io",
|
|
"https://www.beta.test.romeonline.io",
|
|
"https://beta.romeonline.io",
|
|
"https://www.beta.romeonline.io",
|
|
"https://beta.test.imex.online",
|
|
"https://www.beta.test.imex.online",
|
|
"https://beta.imex.online",
|
|
"https://www.beta.imex.online",
|
|
"https://www.test.promanager.web-est.com",
|
|
"https://test.promanager.web-est.com",
|
|
"https://www.promanager.web-est.com",
|
|
"https://www.promanager.web-est.com",
|
|
"https://old.imex.online",
|
|
"https://www.old.imex.online"
|
|
];
|
|
|
|
/**
|
|
* Middleware for Express app
|
|
* @param app
|
|
*/
|
|
const applyMiddleware = (app) => {
|
|
app.use(compression());
|
|
app.use(cookieParser());
|
|
app.use(bodyParser.json({ limit: "50mb" }));
|
|
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
|
|
app.use(cors({ credentials: true, exposedHeaders: ["set-cookie"] }));
|
|
|
|
// Helper middleware
|
|
app.use((req, res, next) => {
|
|
req.logger = logger;
|
|
next();
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Route groupings for Express app
|
|
* @param app
|
|
*/
|
|
const applyRoutes = (app) => {
|
|
app.use("/", require("./server/routes/miscellaneousRoutes"));
|
|
app.use("/notifications", require("./server/routes/notificationsRoutes"));
|
|
app.use("/render", require("./server/routes/renderRoutes"));
|
|
app.use("/mixdata", require("./server/routes/mixDataRoutes"));
|
|
app.use("/accounting", require("./server/routes/accountingRoutes"));
|
|
app.use("/qbo", require("./server/routes/qboRoutes"));
|
|
app.use("/media", require("./server/routes/mediaRoutes"));
|
|
app.use("/sms", require("./server/routes/smsRoutes"));
|
|
app.use("/job", require("./server/routes/jobRoutes"));
|
|
app.use("/scheduling", require("./server/routes/schedulingRoutes"));
|
|
app.use("/utils", require("./server/routes/utilRoutes"));
|
|
app.use("/data", require("./server/routes/dataRoutes"));
|
|
app.use("/adm", require("./server/routes/adminRoutes"));
|
|
app.use("/tech", require("./server/routes/techRoutes"));
|
|
app.use("/intellipay", require("./server/routes/intellipayRoutes"));
|
|
app.use("/cdk", require("./server/routes/cdkRoutes"));
|
|
app.use("/csi", require("./server/routes/csiRoutes"));
|
|
app.use("/payroll", require("./server/routes/payrollRoutes"));
|
|
|
|
// Default route for forbidden access
|
|
app.get("/", (req, res) => {
|
|
res.status(200).send("Access Forbidden.");
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Apply Redis to the server
|
|
* @param server
|
|
* @param app
|
|
*/
|
|
const applySocketIO = async (server, app) => {
|
|
// Redis client setup for Pub/Sub and Key-Value Store
|
|
const pubClient = createClient({ url: process.env.REDIS_URL || "redis://localhost:6379" });
|
|
const subClient = pubClient.duplicate();
|
|
|
|
pubClient.on("error", (err) => logger.log(`Redis pubClient error: ${err}`, "ERROR", "redis"));
|
|
subClient.on("error", (err) => logger.log(`Redis subClient error: ${err}`, "ERROR", "redis"));
|
|
|
|
try {
|
|
await Promise.all([pubClient.connect(), subClient.connect()]);
|
|
logger.log(`[${process.env.NODE_ENV}] Connected to Redis`, "INFO", "redis", "api");
|
|
} catch (redisError) {
|
|
logger.log("Failed to connect to Redis", "ERROR", "redis", redisError);
|
|
}
|
|
|
|
process.on("SIGINT", async () => {
|
|
logger.log("Closing Redis connections...", "INFO", "redis", "api");
|
|
await Promise.all([pubClient.disconnect(), subClient.disconnect()]);
|
|
process.exit(0);
|
|
});
|
|
|
|
const ioRedis = new Server(server, {
|
|
path: "/wss",
|
|
adapter: createAdapter(pubClient, subClient),
|
|
cors: {
|
|
origin: SOCKETIO_CORS_ORIGIN,
|
|
methods: ["GET", "POST"],
|
|
credentials: true,
|
|
exposedHeaders: ["set-cookie"]
|
|
}
|
|
});
|
|
|
|
const io = new Server(server, {
|
|
path: "/ws",
|
|
cors: {
|
|
origin: SOCKETIO_CORS_ORIGIN,
|
|
methods: ["GET", "POST"],
|
|
credentials: true,
|
|
exposedHeaders: ["set-cookie"]
|
|
}
|
|
});
|
|
|
|
app.use((req, res, next) => {
|
|
Object.assign(req, {
|
|
pubClient,
|
|
io,
|
|
ioRedis
|
|
});
|
|
|
|
next();
|
|
});
|
|
|
|
Object.assign(module.exports, { io, pubClient, ioRedis });
|
|
|
|
return { pubClient, io, ioRedis };
|
|
};
|
|
|
|
/**
|
|
* Apply Redis helper functions
|
|
* @param pubClient
|
|
* @param app
|
|
*/
|
|
const applyRedisHelpers = (pubClient, app) => {
|
|
// Store session data in Redis
|
|
const setSessionData = async (socketId, key, value) => {
|
|
await pubClient.hSet(`socket:${socketId}`, key, JSON.stringify(value)); // Use Redis pubClient
|
|
};
|
|
|
|
// Retrieve session data from Redis
|
|
const getSessionData = async (socketId, key) => {
|
|
const data = await pubClient.hGet(`socket:${socketId}`, key);
|
|
return data ? JSON.parse(data) : null;
|
|
};
|
|
|
|
// Clear session data from Redis
|
|
const clearSessionData = async (socketId) => {
|
|
await pubClient.del(`socket:${socketId}`);
|
|
};
|
|
|
|
// Store multiple session data in Redis
|
|
const setMultipleSessionData = async (socketId, keyValues) => {
|
|
// keyValues is expected to be an object { key1: value1, key2: value2, ... }
|
|
const entries = Object.entries(keyValues).map(([key, value]) => [key, JSON.stringify(value)]);
|
|
await pubClient.hSet(`socket:${socketId}`, ...entries.flat());
|
|
};
|
|
|
|
// Retrieve multiple session data from Redis
|
|
const getMultipleSessionData = async (socketId, keys) => {
|
|
const data = await pubClient.hmGet(`socket:${socketId}`, keys);
|
|
// Redis returns an object with null values for missing keys, so we parse the non-null ones
|
|
return Object.fromEntries(keys.map((key, index) => [key, data[index] ? JSON.parse(data[index]) : null]));
|
|
};
|
|
|
|
const setMultipleFromArraySessionData = async (socketId, keyValueArray) => {
|
|
// Use Redis multi/pipeline to batch the commands
|
|
const multi = pubClient.multi();
|
|
|
|
keyValueArray.forEach(([key, value]) => {
|
|
multi.hSet(`socket:${socketId}`, key, JSON.stringify(value));
|
|
});
|
|
|
|
await multi.exec(); // Execute all queued commands
|
|
};
|
|
|
|
// Helper function to add an item to the end of the Redis list
|
|
const addItemToEndOfList = async (socketId, key, newItem) => {
|
|
try {
|
|
await pubClient.rPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
|
|
} catch (error) {
|
|
console.error(`Error adding item to the end of the list for socket ${socketId}:`, error);
|
|
}
|
|
};
|
|
|
|
// Helper function to add an item to the beginning of the Redis list
|
|
const addItemToBeginningOfList = async (socketId, key, newItem) => {
|
|
try {
|
|
await pubClient.lPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
|
|
} catch (error) {
|
|
console.error(`Error adding item to the beginning of the list for socket ${socketId}:`, error);
|
|
}
|
|
};
|
|
|
|
// Helper function to clear a list in Redis
|
|
const clearList = async (socketId, key) => {
|
|
try {
|
|
await pubClient.del(`socket:${socketId}:${key}`);
|
|
} catch (error) {
|
|
console.error(`Error clearing list for socket ${socketId}:`, error);
|
|
}
|
|
};
|
|
|
|
const api = {
|
|
setSessionData,
|
|
getSessionData,
|
|
clearSessionData,
|
|
setMultipleSessionData,
|
|
getMultipleSessionData,
|
|
setMultipleFromArraySessionData,
|
|
addItemToEndOfList,
|
|
addItemToBeginningOfList,
|
|
clearList
|
|
};
|
|
|
|
Object.assign(module.exports, api);
|
|
|
|
app.use((req, res, next) => {
|
|
req.sessionUtils = api;
|
|
next();
|
|
});
|
|
|
|
// // Demo to show how all the helper functions work
|
|
// const demoSessionData = async () => {
|
|
// const socketId = "testSocketId";
|
|
//
|
|
// // Store session data using setSessionData
|
|
// await exports.setSessionData(socketId, "field1", "Hello, Redis!");
|
|
//
|
|
// // Retrieve session data using getSessionData
|
|
// const field1Value = await exports.getSessionData(socketId, "field1");
|
|
// console.log("Retrieved single field value:", field1Value);
|
|
//
|
|
// // Store multiple session data using setMultipleSessionData
|
|
// await exports.setMultipleSessionData(socketId, { field2: "Second Value", field3: "Third Value" });
|
|
//
|
|
// // Retrieve multiple session data using getMultipleSessionData
|
|
// const multipleFields = await exports.getMultipleSessionData(socketId, ["field2", "field3"]);
|
|
// console.log("Retrieved multiple field values:", multipleFields);
|
|
//
|
|
// // Store multiple session data using setMultipleFromArraySessionData
|
|
// await exports.setMultipleFromArraySessionData(socketId, [
|
|
// ["field4", "Fourth Value"],
|
|
// ["field5", "Fifth Value"]
|
|
// ]);
|
|
//
|
|
// // Retrieve and log all fields
|
|
// const allFields = await exports.getMultipleSessionData(socketId, [
|
|
// "field1",
|
|
// "field2",
|
|
// "field3",
|
|
// "field4",
|
|
// "field5"
|
|
// ]);
|
|
// console.log("Retrieved all field values:", allFields);
|
|
//
|
|
// // Add item to the end of a Redis list
|
|
// await exports.addItemToEndOfList(socketId, "logEvents", { event: "Log Event 1", timestamp: new Date() });
|
|
// await exports.addItemToEndOfList(socketId, "logEvents", { event: "Log Event 2", timestamp: new Date() });
|
|
//
|
|
// // Add item to the beginning of a Redis list
|
|
// await exports.addItemToBeginningOfList(socketId, "logEvents", { event: "First Log Event", timestamp: new Date() });
|
|
//
|
|
// // Retrieve the entire list (using lRange)
|
|
// const logEvents = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1);
|
|
// console.log("Log Events List:", logEvents.map(JSON.parse));
|
|
//
|
|
// // **Add the new code below to test clearList**
|
|
//
|
|
// // Clear the list using clearList
|
|
// await exports.clearList(socketId, "logEvents");
|
|
// console.log("Log Events List cleared.");
|
|
//
|
|
// // Retrieve the list after clearing to confirm it's empty
|
|
// const logEventsAfterClear = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1);
|
|
// console.log("Log Events List after clearing:", logEventsAfterClear); // Should be an empty array
|
|
//
|
|
// // Clear session data
|
|
// await exports.clearSessionData(socketId);
|
|
// console.log("Session data cleared.");
|
|
// };
|
|
//
|
|
// if (process.env.NODE_ENV === "development") {
|
|
// demoSessionData();
|
|
// }
|
|
};
|
|
|
|
/**
|
|
* Main function to start the server
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const main = async () => {
|
|
const app = express();
|
|
const port = process.env.PORT || 5000;
|
|
|
|
const server = http.createServer(app);
|
|
|
|
const { pubClient, ioRedis } = await applySocketIO(server, app);
|
|
applyRedisHelpers(pubClient, app);
|
|
|
|
// Legacy Socket Events
|
|
require("./server/web-sockets/web-socket");
|
|
|
|
applyMiddleware(app);
|
|
applyRoutes(app);
|
|
redisSocketEvents(ioRedis);
|
|
|
|
try {
|
|
await server.listen(port);
|
|
logger.log(`[${process.env.NODE_ENV}] Server started on port ${port}`, "INFO", "api");
|
|
} catch (error) {
|
|
logger.log(`[${process.env.NODE_ENV}] Server failed to start on port ${port}`, "ERROR", "api", error);
|
|
}
|
|
};
|
|
|
|
// Start server
|
|
main();
|