-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
146 lines (121 loc) · 3.13 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
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.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);
}
});
}
initRepositories() {
this.exampleRepo = require("./repository/example")(this.mysql.connection);
}
initUsecases() {
this.exampleUsecase = require("./usecase/example")(this.exampleRepo);
}
initRoutes() {
const authMiddleWare = require("./middlewares/auth");
app.use(authMiddleWare);
const exampleRouter = require("./routes/example")(this.exampleUsecase);
//const exampleRouter = require('./routes/example')( this.example_controller );
app.use("/example", exampleRouter.getRouter());
//app.use('/example', displayRouter.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();
});
});