-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
148 lines (124 loc) · 4.06 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
141
142
143
144
145
146
147
148
/* Simplified stock exchange made with uWebSockets.js pub/sub */
import ip from "ip"
import os from "os"
import uWS from "uWebSockets.js"
import RuntimeDefinedContext from "./app/store/RuntimeDefinedContext.js"
import ServiceLocatorContainer from "./app/common/ServiceLocatorContainer.js"
import providers from "./app/providers/index.js"
import RegisterProvider from "./app/common/RegisterProvider.js"
import clusterManager from "./app/cluster/cluster_manager.js"
import clusterSyncer from "./app/cluster/cluster_syncer.js"
import clientManager from "./app/networking/client_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 SamaNativePushQueue from "./app/lib/push_queue/sama_native_queue.js"
import { connectToDBPromise, getDb } from "./app/lib/db.js"
import RedisClient from "./app/lib/redis.js"
import { APIs } from "./app/networking/APIs.js"
RuntimeDefinedContext.APP_HOSTNAME = process.env.HOSTNAME || os.hostname()
RuntimeDefinedContext.APP_IP = ip.address()
switch (process.env.STORAGE_DRIVER) {
case "minio":
RuntimeDefinedContext.STORAGE_DRIVER = new Minio()
break
case "spaces":
RuntimeDefinedContext.STORAGE_DRIVER = new Spaces()
break
default:
RuntimeDefinedContext.STORAGE_DRIVER = new S3()
break
}
switch (process.env.PUSH_QUEUE_DRIVER) {
case "sama_native":
default:
RuntimeDefinedContext.PUSH_QUEUE_DRIVER = new SamaNativePushQueue(
process.env.SAMA_NATIVE_PUSH_QUEUE_NAME,
process.env.REDIS_URL
)
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)
await clientManager.createLocalSocket(
isSSL ? SSL_APP_OPTIONS : APP_OPTIONS,
WS_OPTIONS,
WS_LISTEN_OPTIONS,
isSSL,
appPort
)
RuntimeDefinedContext.CLUSTER_PORT = await clusterManager.createLocalSocket(
isSSL ? SSL_APP_OPTIONS : APP_OPTIONS,
WS_OPTIONS,
WS_LISTEN_OPTIONS,
isSSL
)
console.log("[RuntimeDefinedContext]", RuntimeDefinedContext)
// perform a database connection when the server starts
await connectToDBPromise()
.then(() => {
console.log("[connectToDB] Ok")
})
.catch((err) => {
console.error("[connectToDB] Error", err)
process.exit()
})
await RedisClient.connect()
// Register providers
ServiceLocatorContainer.register(
new (class extends RegisterProvider {
register(slc) {
return RuntimeDefinedContext
}
})({ name: "RuntimeDefinedContext", implementationName: RuntimeDefinedContext.name })
)
ServiceLocatorContainer.register(
new (class extends RegisterProvider {
register(slc) {
return RedisClient
}
})({ name: "RedisClient", implementationName: RedisClient.constructor.name })
)
ServiceLocatorContainer.register(
new (class extends RegisterProvider {
register(slc) {
return getDb()
}
})({ name: "MongoConnection", implementationName: "MongoConnection" })
)
ServiceLocatorContainer.register(
new (class extends RegisterProvider {
register(slc) {
return RuntimeDefinedContext.STORAGE_DRIVER
}
})({ name: "StorageDriverClient", implementationName: RuntimeDefinedContext.STORAGE_DRIVER.constructor.name })
)
for (const provider of providers) {
ServiceLocatorContainer.register(provider)
}
for (const api of Object.values(APIs)) {
for (const provider of api.providers) {
ServiceLocatorContainer.register(provider)
}
}
// Boot providers
await ServiceLocatorContainer.createAllSingletonInstances()
await ServiceLocatorContainer.boot()
// Start Cluster Sync
await clusterSyncer.startSyncingClusterNodes()
// https://dev.to/mattkrick/replacing-express-with-uwebsockets-48ph