forked from SAMA-Communications/sama-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (70 loc) · 2.16 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
/* Simplified stock exchange made with uWebSockets.js pub/sub */
import uWS from "uWebSockets.js";
import clientManager from "./app/routes/client_manager.js";
import clusterManager from "./app/cluster/cluster_manager.js";
// get MongoDB driver connection
import Minio from "./app/lib/storage/minio.js";
import S3 from "./app/lib/storage/s3.js";
import Spaces from "./app/lib/storage/spaces.js";
import { connectToDB } from "./app/lib/db.js";
//cache storage
import BlockListRepository from "./app/repositories/blocklist_repository.js";
import ClusterSyncer from "./app/cluster/cluster_syncer.js";
import ConversationRepository from "./app/repositories/conversation_repository.js";
import RedisClient from "./app/lib/redis.js";
switch (process.env.STORAGE_DRIVER) {
case "minio":
globalThis.storageClient = new Minio();
break;
case "spaces":
globalThis.storageClient = new Spaces();
break;
default:
globalThis.storageClient = new S3();
break;
}
const APP_OPTIONS = {};
const SSL_APP_OPTIONS = {
key_file_name: process.env.SSL_KEY_FILE_NAME,
cert_file_name: process.env.SSL_CERT_FILE_NAME,
};
const WS_OPTIONS = {
compression: uWS.SHARED_COMPRESSOR,
idleTimeout: 12,
maxBackpressure: 1024,
maxPayloadLength: 16 * 1024 * 1024,
};
const WS_LISTEN_OPTIONS = {
LIBUS_LISTEN_EXCLUSIVE_PORT: 1,
};
const isSSL =
!!SSL_APP_OPTIONS.key_file_name && !!SSL_APP_OPTIONS.cert_file_name;
const appPort = parseInt(process.env.APP_PORT || process.env.PORT);
clientManager.createLocalSocket(
isSSL ? SSL_APP_OPTIONS : APP_OPTIONS,
WS_OPTIONS,
WS_LISTEN_OPTIONS,
isSSL,
appPort
);
//
clusterManager.createLocalSocket(
isSSL ? SSL_APP_OPTIONS : APP_OPTIONS,
WS_OPTIONS,
WS_LISTEN_OPTIONS,
isSSL
);
// perform a database connection when the server starts
connectToDB(async (err) => {
if (err) {
console.error("[connectToDB] Error", err);
process.exit();
} else {
console.log("[connectToDB] Ok");
await ClusterSyncer.startSyncingClusterNodes();
await RedisClient.connect();
await BlockListRepository.warmCache();
await ConversationRepository.warmCache();
}
});
// https://dev.to/mattkrick/replacing-express-with-uwebsockets-48ph