-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
179 lines (158 loc) · 4.93 KB
/
server.js
File metadata and controls
179 lines (158 loc) · 4.93 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import "dotenv/config";
import express from "express";
import { createServer } from "http";
import { initializeSocket } from "./socket/socketServer.js";
import expressLayouts from "express-ejs-layouts";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import authRoutes from "./routes/auth.js";
import dashboardRoutes from "./routes/dashboard.js";
import friendPageRouter from "./routes/friendsPage.js";
import friendApiRouter from "./routes/friendsApi.js";
import matchHistoryPageRouter from "./routes/matchHistoryPage.js";
import matchHistoryApiRouter from "./routes/matchHistoryApi.js";
import gameLobbyPage from "./routes/gameLobbyPage.js";
import notificationsPage from "./routes/notificationsPage.js";
import notificationsApi from "./routes/notificationsApi.js";
import roomPage from "./routes/roomPage.js";
import roomApi from "./routes/roomApi.js";
import gameplayRoute from "./routes/gameplayRoute.js";
import path from "path";
import helmet from "helmet";
import cors from "cors";
import rateLimit from "express-rate-limit";
import compression from "compression";
import { errorHandler, notFound } from "./middleware/errorMiddleware.js";
import logger from "./utils/logger.js";
import User from "./models/User.js";
const PORT = process.env.PORT;
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(expressLayouts);
app.set("layout", "layouts/layout");
// Helmet to Secure content Policy
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: [
"'self'",
"'unsafe-inline'",
"https://cdnjs.cloudflare.com",
"https://fonts.googleapis.com",
],
styleSrcElem: [
"'self'",
"https://cdnjs.cloudflare.com",
"https://fonts.googleapis.com",
"https://cdn.jsdelivr.net",
],
// images
imgSrc: ["'self'", "data:", "https:"],
fontSrc: [
"'self'",
"https://cdnjs.cloudflare.com",
"https://fonts.gstatic.com",
],
scriptSrc: [
"'self'",
"'unsafe-inline'",
"https://cdn.jsdelivr.net",
"https://cdn.socket.io",
],
connectSrc: [
"'self'",
process.env.CLIENT_URL,
"https://cdn.jsdelivr.net",
"https://cdn.socket.io",
],
},
},
})
);
// CORS - environment based
app.use(
cors({
origin: process.env.CLIENT_URL,
credentials: true,
optionsSuccessStatus: 200,
})
);
// Compression
app.use(compression());
// Rate limiting - more lenient in development
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: process.env.NODE_ENV === "production" ? 100 : 10000, // Higher limit in dev
message: {
success: false,
message: "Too many requests, please try again later.",
},
});
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: process.env.NODE_ENV === "production" ? 5 : 10000, // Higher limit in dev
message: {
success: false,
message: "Too many authentication attempts, please try again later.",
},
});
app.use(limiter);
app.use("/auth", authLimiter);
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(express.static(join(__dirname, "public")));
// Routes
app.use("/auth", authRoutes);
app.use("/dashboard", dashboardRoutes);
app.use("/friends", friendPageRouter);
app.use("/api/friends", friendApiRouter);
app.use("/matchHistory", matchHistoryPageRouter);
app.use("/api/matchHistory", matchHistoryApiRouter);
app.use("/lobby", gameLobbyPage);
app.use("/notifications", notificationsPage);
app.use("/api/notifications", notificationsApi);
app.use("/room", roomPage);
app.use("/api/room", roomApi);
app.use("/gameplay", gameplayRoute);
// 404 handler
app.use(notFound);
// Error handler
app.use(errorHandler);
const server = createServer(app);
initializeSocket(server);
// setup Mongo database
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log("✅ MongoDB connected");
setInterval(async () => {
try {
const oneMinuteAgo = new Date(Date.now() - 60 * 1000);
const result = await User.updateMany(
{
status: "online",
lastSeen: { $lt: oneMinuteAgo },
},
{ status: "offline" }
);
if (result.modifiedCount > 0) {
console.log(`Marked ${result.modifiedCount} users as offline`);
}
} catch (error) {
console.log("Cleanup error:", error);
}
}, 1000);
})
.catch((err) => console.error("❌ MongoDB error:", err));
server.listen(PORT, () => {
console.log("Server is Running on port: " + PORT);
});