-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (95 loc) · 3.02 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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const app = express();
const config = require("config");
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const ejs = require("ejs");
// Config Cluster
const cluster = require("cluster");
// Check the number of available CPU.
const numCPUs = require("os").cpus().length;
// CONFIGURING CONFIG JWTPRIVATEKEY
if (!config.get("jwtPrivateKey")) {
console.log("FATAL ERROR : jwtPrivateKey is not defined");
process.exit(1);
}
// CONFIGURING MONODB WITH MONGOOSE
require("./modules/mongoose.config");
app.use(morgan("tiny"));
app.use(cors());
// CONFIGURING BODY PARSER
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
// set the view engine to ejs
app.set("views", "./public");
app.set("view engine", "ejs");
app.get("/downloads/", (req, res) => {
res.render("admin");
});
const students = require("./routes/student");
const records = require("./routes/records");
const auth = require("./routes/auth.student");
const gateway = require("./routes/gate");
const authAdmin = require("./routes/auth.admin");
const admin = require("./routes/admin");
const verification = require("./routes/verification");
const dashboard = require("./routes/warden");
// ROUTES
app.use("/api/student", students);
app.use("/api/records", records);
app.use("/api/gateway", gateway);
app.use("/api/auth", auth);
app.use("/api/auth/admin", authAdmin);
app.use("/api/admin", admin);
app.use("/api/verification", verification);
app.use("/api/warden", dashboard);
app.get("*", (req, res) => {
res.status(400).send("Wrong turn!");
});
/////////////////////////////////////////////
// SOCKET cONNECTION FOR NOTIFICATIONS
/////////////////////////////////////////////
io.on("connection", (socket) => {
// connecting student
socket.on("join", (id) => {
socket.join(id);
});
// waiting for msg from admin
socket.on("msg from admin", (student) => {
// get student id and msg from the gate-app
// send the noitification to the student with provided id
// sending msg to student
socket.broadcast.to(student.id).emit("msg to student", {
msg: student.msg,
success: true,
});
});
});
// config cluster for parallel processing
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// This event is firs when worker died
cluster.on("exit", (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
// For Worker
else {
// Workers can share any TCP connection
// establishing the server on the port 3000
const port = process.env.PORT || config.get("_process").PORT;
const IP = process.env.IP || config.get("_process").IP;
server.listen(port, IP, () => {
console.log(
`Client Server Started Processs ${process.pid} :: http://${IP}:${port} `
);
});
}