-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (118 loc) · 4.67 KB
/
index.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
/*
___ _
/ _ \ (_)
/ /_\ \_ __ ___ _ __ _ _ _ __ ___ _ ___ ___
| _ | '_ \ / _ \| '_ \| | | | '_ ` _ \| |/ __/ _ \
| | | | | | | (_) | | | | |_| | | | | | | | (_| __/
\_| |_/_| |_|\___/|_| |_|\__, |_| |_| |_|_|\___\___|
__/ |
|___/
______ _ _ ______ _
| _ (_) | | | ___ \ | |
| | | |_ ___ ___ ___ _ __ __| | | |_/ / ___ | |_
| | | | / __|/ __/ _ \| '__/ _` | | ___ \/ _ \| __|
| |/ /| \__ \ (_| (_) | | | (_| | | |_/ / (_) | |_
|___/ |_|___/\___\___/|_| \__,_| \____/ \___/ \__|
*/
/*##############################################################################
# File: index.js #
# Project: Anonymice - Discord Bot #
# Author/Updater: Doyler (@NftDoyler) #
# Original Author(s): Oliver Renner (@_orenner) & slingn.eth (@slingncrypto) #
# © 2021 #
###############################################################################*/
const config = require("./src/config");
const logger = require("./src/utils/logger");
const banner = require("./src/utils/banner");
const mongoose = require("mongoose");
const app = require("./src/app");
const DiscordBot = require("./src/discordBot");
const Synchronizer = require("./src/synchronizer");
const dotenv = require("dotenv");
const path = require("path");
dotenv.config({
path: path.join(__dirname, '../../.env')
});
/*##############################################################################
express SSL support - https://expressjs.com/en/api.html
##############################################################################*/
// Default to HTTP in case something weird happens
const http = require('http');
let webServer = http.createServer(app);
// https://dev.to/omergulen/step-by-step-node-express-ssl-certificate-run-https-server-from-scratch-in-5-steps-5b87
if (process.env.APPLICATION_SERVER_PUBLIC_SCHEME === "https") {
const https = require('https');
const fs = require('fs');
webServer = https.createServer({
key: fs.readFileSync(process.env.SSL_PRIVATE_KEY),
cert: fs.readFileSync(process.env.SSL_PUBLIC_CERT),
}, app);
}
logger.info(banner);
logger.info(`Starting ${config.application.name}...`)
let server;
mongoose.connect(config.mongodb.url, config.mongoose.options)
.then(async () => {
logger.info("Connected to MongoDB");
//start the web server
server = await webServer.listen(config.application.port, () => {
logger.info(
`${config.application.name} is running at port ${config.application.port}`
);
});
//todo: migrate to another node hosting script
//start the discord bot
await DiscordBot.start();
//todo: migrate to another node hosting script
//start the daily role verification sync process
await Synchronizer.start();
if(config.sync.syncOnStartup && config.sync.syncOnStartup === "true"){
logger.info(`Synchronize on startup as been enabled, executing first synchronization cycle immediately...`)
await Synchronizer.execute();
}
})
.catch(error => {
exitHandler(error)
});
//todo: deprecate once other services are moved to another node hosting script
const stopServices = (shouldLog) => {
if (server) {
server.close();
logger.info("Server closed");
}
if (DiscordBot) {
DiscordBot.stop();
logger.info("Discord Client closed");
}
if (Synchronizer) {
Synchronizer.stop();
logger.info("Synchronizer stopped");
}
};
const exitHandler = (error) => {
stopServices(true);
if(error) {
logger.error(error.message);
logger.error(error.stack);
process.exit(-1);
}
process.exit(0);
}
const unexpectedErrorHandler = (error) => {
logger.error(error.message);
logger.error(error.stack)
};
process.on("uncaughtException", unexpectedErrorHandler);
process.on("unhandledRejection", unexpectedErrorHandler);
process.on("SIGINT", () => {
logger.info("SIGNINT received");
logger.info(`Stopping ${config.application.name}`);
stopServices();
process.exit(0);
});
process.on("SIGTERM", () => {
logger.info("SIGNTERM received");
logger.info(`Stopping ${config.application.name}`);
stopServices();
process.exit(0);
});