generated from V3N0ME/Node-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
182 lines (156 loc) · 4.84 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
global.env =
process.env.NODE_ENV === undefined ? "development" : process.env.NODE_ENV;
global.isDev = () => {
return global.env === "development";
};
const PORT = process.env.PORT === undefined ? 8080 : process.env.PORT;
const express = require("express");
const app = express();
const compression = require("compression");
const bodyParser = require("body-parser");
const HttpServer = require("http").createServer(app);
const logger = require("./utils/logger");
class Server {
constructor() {
this.drivers = [];
this.init();
}
async init() {
try {
await this.initDrivers();
this.initRepositories();
this.initUsecases();
this.initServices();
this.initExpress();
this.initRoutes();
this.initServer();
} catch (err) {
process.exit(err);
}
}
initExpress() {
app.use(require("cors")());
const colours = {
GET: "\x1b[32m",
POST: "\x1b[34m",
DELETE: "\x1b[31m",
PUT: "\x1b[33m",
};
app.use("*", (req, _, next) => {
if (global.isDev()) {
console.log(colours[req.method] + req.method, "\x1b[0m" + req.baseUrl);
}
next();
});
//Enable request compression
app.use(compression());
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true,
})
);
app.use(express.static(__dirname + "/views", { maxAge: "30 days" }));
}
initServer() {
HttpServer.listen(PORT, () => {
console.log(`Server Running ${PORT}`);
});
}
initDrivers() {
return new Promise(async (resolve, reject) => {
try {
this.mysql = await require("./drivers/mysql")().connect();
//this.mongo = require('./models/mongo')().connect();
this.drivers.push(this.mysql);
//this.models.push(this.mongo);
resolve();
} catch (err) {
reject(err);
}
});
}
initServices() {
this.synker = require("./services/synker")(
this.doctorUsecase,
this.specialisationUsecase
);
this.synker._sync(); //Use for immediate doctor details sync
// this.synker.start(); //Use to start CRON job
}
initRepositories() {
this.userRepo = require("./repository/user")(this.mysql.connection);
this.doctorRepo = require("./repository/doctor")(this.mysql.connection);
this.patientsRepo = require("./repository/patients")(this.mysql.connection);
this.specialisationRepo = require("./repository/specialisation")(
this.mysql.connection
);
this.appointmentRepo = require("./repository/appointment")(
this.mysql.connection
);
// this.doctorRepo = require("./repository/doctor")(this.mysql.connection);
}
initUsecases() {
this.patientsUsecase = require("./usecase/patients")(this.patientsRepo);
this.doctorUsecase = require("./usecase/doctor")(this.doctorRepo);
this.userUsecase = require("./usecase/user")(this.userRepo, this.patientsUsecase, this.doctorUsecase);
this.specialisationUsecase = require("./usecase/specialisation")(this.specialisationRepo);
this.ordersUsecase = require("./usecase/orders")(this.ordersRepo);
this.appointmentUsecase = require("./usecase/appointment")(this.appointmentRepo, this.userUsecase, this.ordersUsecase);
}
initRoutes() {
const authMiddleWare = require("./middlewares/auth");
app.use(authMiddleWare);
const userRouter = require("./routes/user")(this.userUsecase);
const doctorRouter = require("./routes/doctor")(this.doctorUsecase);
const patientsRouter = require("./routes/patients")(this.patientsUsecase);
const specialisationRouter = require("./routes/specialisation")(
this.specialisationUsecase
);
const ordersRouter = require("./routes/orders")(this.ordersUsecase);
const appointmentRouter = require("./routes/appointment")(
this.appointmentUsecase
);
app.use("/user", userRouter.getRouter());
app.use("/doctor", doctorRouter.getRouter());
app.use("/specialisation", specialisationRouter.getRouter());
app.use("/orders", ordersRouter.getRouter());
app.use("/patients", patientsRouter.getRouter());
app.use("/appointment", appointmentRouter.getRouter());
}
onClose() {
//Close all DB Connections
this.drivers.map((m) => {
m.close();
});
HttpServer.close();
}
}
const server = new Server();
[
"SIGINT",
"SIGTERM",
"SIGQUIT",
"exit",
"uncaughtException",
"SIGUSR1",
"SIGUSR2",
].forEach((eventType) => {
process.on(eventType, (err = "") => {
process.removeAllListeners();
let error = err.toString();
if (err.stack) {
error = err.stack;
}
logger.Log({
level: logger.LEVEL.ERROR,
component: "SERVER",
code: "SERVER.EXIT",
description: error,
category: "",
ref: {},
});
server.onClose();
});
});