-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
84 lines (69 loc) · 2.01 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import "dotenv/config";
import express from "express";
import cors from "cors";
import path from "path";
import { dirname } from "path";
import { fileURLToPath } from "url";
import routes from "./routes/routes.js";
import { createServer } from "http";
import logger from "./utils/logger.js";
import cookieParser from "cookie-parser";
import { COOKIE_NAME } from "./utils/constants.js";
import { Server } from "socket.io";
import { initializeSockets } from "./services/streamingService.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PORT = process.env.SERVER_PORT || 5000;
const app = express();
const httpServer = createServer(app);
export const socketService = new Server(httpServer, {
cors: {
origin: "*",
},
});
initializeSockets();
app.use(cookieParser());
const requestMiddleware = (req, res, next) => {
const sessionId = req.cookies[COOKIE_NAME];
if (sessionId) {
// Store the session ID in the request object for later use
req.sessionSid = sessionId;
logger.info("Captured session ID:" + sessionId);
} else {
logger.info("No session.sid cookie found");
}
next();
};
// app.use(
// cors({
// credentials: true,
// origin: (origin, callback) => callback(null, origin),
// })
// );
const allowedOrigins = [
"http://localhost:8100",
"http://localhost:5173",
process.env.UI_ENDPOINT,
];
// CORS configuration function
const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true, // Allow credentials (cookies, authorization headers, etc.)
};
app.use(cors(corsOptions));
app.use(requestMiddleware);
app.use(express.json());
app.use((req, res, next) => {
logger.info(`${req.method}: ${req.url} request received`);
return next();
});
app.use(routes);
httpServer.listen(PORT, () => {
logger.info(`ChatDKG Server is running on http://localhost:${PORT}`);
});