-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (54 loc) · 1.41 KB
/
Copy pathserver.js
File metadata and controls
61 lines (54 loc) · 1.41 KB
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
import http from "http";
import app from "./src/app.js";
import { initSocket } from "./socket.js";
import { registerChatSocket } from "./src/api/socket/chat.socket.js";
import { registerCallSocket } from "./src/api/socket/call.socket.js";
import connectDB from "./src/config/mongodb.js";
import { connectRedis } from "./src/config/redis.js";
import { webPushStart } from "./src/config/webpush.js";
import chalk, {
printASCII,
errorLog,
success,
info
} from "./logs/printLogs.js";
// configure server
const server = http.createServer(app);
const port = process.env.PORT || 3000;
function startSocket() {
const { callIO, chatIO } = initSocket(server);
registerChatSocket(chatIO);
registerCallSocket(callIO);
}
function startServer() {
startSocket();
server.listen(port, () => {
info("STARTING SERVER ...");
console.log(chalk.gray(`server is listening on port ${port} ...`));
success("SERVER STARTED ✓");
});
}
async function init() {
info("SERVICE WAKING UP ...");
if (process.env.NODE_ENV === "production") {
console.clear();
try {
await connectDB();
connectRedis();
startServer();
webPushStart();
printASCII("PRODUCTION");
return;
} catch (err) {
errorLog("BOOT FAILED ❌");
console.error(err);
process.exit(1);
}
}
startServer();
connectRedis();
connectDB();
webPushStart();
printASCII("DEVLOPEMNT");
}
init();