-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
190 lines (158 loc) · 4.96 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
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
180
181
182
183
184
185
186
187
188
189
190
import "express-async-errors";
import { Server } from "socket.io";
("socket.io");
import * as dotenv from "dotenv";
dotenv.config();
import express from "express";
const app = express();
import morgan from "morgan";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
import fileUpload from "express-fileupload";
import rateLimiter from "express-rate-limit";
import xss from "xss-clean";
import cors from "cors";
// database
import connectDB from "./configs/connect.js";
// routers
import studentRouter from "./routes/studentRoutes.js";
import adminRouter from "./routes/adminRoutes.js";
import teacherRouter from "./routes/teacherRoutes.js";
import collageRouter from "./routes/collageRoutes.js";
import subjectRouter from "./routes/subjectRoutes.js";
import semesterRouter from "./routes/semesterRoutes.js";
import courseRouter from "./routes/courseRoutes.js";
import sectionRouter from "./routes/sectionRoutes.js";
import gradeRouter from "./routes/gradeRoutes.js";
import majorRouter from "./routes/majorRoutes.js";
import semesterTemplateRoute from "./routes/semesterTemplateRoutes.js";
import authRouter from "./routes/authRouter.js";
import userRouter from "./routes/userRouter.js";
import systemRouter from "./routes/systemRoutes.js";
import userRoutes from "./routes/userRoutes.js";
import messageRoutes from "./routes/messagesRoute.js";
import chatRoutes from "./routes/chatRoutes.js";
// public
import { dirname } from "path";
import { fileURLToPath } from "url";
import path from "path";
// middleware
import errorHandlerMiddleware from "./middleware/errorHandlerMiddleware.js";
import {
authenticateUser,
authorizePermissions,
} from "./middleware/authMiddleware.js";
import { activityLogger } from "./middleware/systemMiddleware.js";
import { collectAndStoreMetrics } from "./utils/dataCollector.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// app.set('trust proxy', 1);
// app.use(
// rateLimiter({
// windowMs: 15 * 60 * 1000,
// max: 60,
// })
// );
app.use(express.static(path.resolve(__dirname, "./client/dist")));
app.use(cookieParser());
app.use(express.json());
app.use(
helmet({
contentSecurityPolicy: {
useDefaults: true,
directives: {
defaultSrc: ["'self'"],
connectSrc: ["'self'", "http://localhost:5000", "ws://localhost:5000"],
// Add other directives as needed
},
},
})
);
app.use(cors());
app.use(mongoSanitize());
app.get("/", (req, res) => {
res.send("Hello World");
});
app.get("/api/v1/test", (req, res) => {
res.json({ msg: "test route" });
});
app.use("/api/v1/auth", authRouter);
app.use("/api/v1",authenticateUser, authorizePermissions);
//setup apis
app.use("/api/v1/users", userRouter);
app.use("/api/v1/students", studentRouter);
app.use("/api/v1/teachers", teacherRouter);
app.use("/api/v1/admins", adminRouter);
app.use("/api/v1/collages", collageRouter);
app.use("/api/v1/courses", courseRouter);
app.use("/api/v1/grades", gradeRouter);
app.use("/api/v1/sections", sectionRouter);
app.use("/api/v1/semesters", semesterRouter);
app.use("/api/v1/majors", majorRouter);
app.use("/api/v1/subjects", subjectRouter);
app.use("/api/v1/system", systemRouter);
app.use(
"/api/v1/semester-templates",
semesterTemplateRoute
);
app.use("/images", express.static("images"));
app.use("/api/v1/au", userRoutes);
app.use("/api/v1/messages", messageRoutes);
app.use("/api/v1/chats", chatRoutes);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "./client/dist", "index.html"));
});
app.use("*", (req, res) => {
res.status(404).json({ msg: "not found" });
});
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 6000;
const server = app.listen(port, () =>
console.log(`Server is listening on port ${port}...`)
);
const start = async () => {
try {
await connectDB(process.env.MONGO_URL);
} catch (error) {
console.log(error);
}
};
start();
const io = new Server(server, {
cors: {
// origin: "http://localhost:5173",
credentials: true,
},
});
//creating sockets
io.on("connection", (socket) => {
console.log("Connected to socket.io");
socket.on("setup", (userData) => {
socket.join(userData._id);
socket.emit("connected");
});
socket.on("join chat", (room) => {
socket.join(room);
});
socket.on("contacts", (data) => {
if (data) {
socket.emit("contacts", data);
}
});
socket.on("new message", (newMessageRecieved) => {
socket
.in(newMessageRecieved.chatId)
.emit("message recieved", newMessageRecieved);
});
socket.off("setup", () => {
console.log("USER DISCONNECTED");
socket.leave(userData._id);
});
});
// console.log('EMAIL_USER:', process.env.EMAIL_USER);
// console.log('EMAIL_PASS:', process.env.EMAIL_PASS);
// setInterval(collectAndStoreMetrics, 100000); // Collect and store metrics every 10 seconds