-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
45 lines (36 loc) · 1.27 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
const config = require("./configs/config.js");
const db = require("./scripts/db.js");
const app = require("./scripts/app.js");
function log(...msg) {
const time = new Date();
console.log(`${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`, ...msg);
}
function error(...msg) {
const time = new Date();
console.error(`${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`, ...msg);
}
(async function() {
log("TME_API: Starting the API server...");
let dbConnection = null;
const onDBDisconnect = (e) => {
error("TME_API: Couldn't connect to database: ", e);
app.onDBDisconnect();
}
const onDBReconnect = (dbConnection) => {
log("TME_API: Connection to database restored...");
app.onDBReconnect(dbConnection);
}
dbConnection = await db.create(config, onDBDisconnect, onDBReconnect);
if (dbConnection === null)
log("TME_API: Starting the server without database...");
else
log("TME_API: Connected to database with id " + dbConnection.threadId + "...");
try {
app.create(config, dbConnection);
app.start(() => {
log("TME_API: listening on port " + config.port);
});
} catch(e) {
error("TME_API: Fatal app error:", e);
}
})();