@rpcbase/server 0.49.0 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +5 -0
- package/cli/build_server.js +29 -0
- package/cli/run_docker.js +3 -1
- package/cli/run_native.js +0 -1
- package/cli/start_server.js +0 -1
- package/database.js +10 -35
- package/express/session_middleware.js +50 -34
- package/package.json +4 -4
package/bin.js
CHANGED
|
@@ -7,6 +7,7 @@ const yargs = require("yargs/yargs")
|
|
|
7
7
|
const {hideBin} = require("yargs/helpers")
|
|
8
8
|
|
|
9
9
|
const start_server = require("./cli/start_server")
|
|
10
|
+
const build_server = require("./cli/build_server")
|
|
10
11
|
|
|
11
12
|
let is_command = false
|
|
12
13
|
|
|
@@ -15,6 +16,10 @@ const args = yargs(hideBin(process.argv))
|
|
|
15
16
|
is_command = true
|
|
16
17
|
start_server()
|
|
17
18
|
})
|
|
19
|
+
.command("build", "build server", () => {}, (argv) => {
|
|
20
|
+
is_command = true
|
|
21
|
+
build_server()
|
|
22
|
+
})
|
|
18
23
|
.command("ping", "ping", () => {}, (argv) => {
|
|
19
24
|
is_command = true
|
|
20
25
|
console.log("PING")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const path = require("path")
|
|
3
|
+
const fs = require("fs")
|
|
4
|
+
|
|
5
|
+
const get_runtime = require("./get_runtime")
|
|
6
|
+
const run_docker = require("./run_docker")
|
|
7
|
+
const run_native = require("./run_native")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const build_server = async() => {
|
|
11
|
+
// const build_schemas =
|
|
12
|
+
// const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
|
|
13
|
+
//
|
|
14
|
+
// // prefix
|
|
15
|
+
// const proj_parent_dir = path.join(infrastructure_dir, "../../")
|
|
16
|
+
// const proj_prefix = path.basename(proj_parent_dir)
|
|
17
|
+
//
|
|
18
|
+
// const runtime = get_runtime()
|
|
19
|
+
//
|
|
20
|
+
// if (runtime === "native") {
|
|
21
|
+
// run_native(infrastructure_dir, proj_prefix)
|
|
22
|
+
// } else {
|
|
23
|
+
// run_docker(infrastructure_dir, proj_prefix)
|
|
24
|
+
// }
|
|
25
|
+
console.log("WOW run build server")
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = build_server
|
package/cli/run_docker.js
CHANGED
|
@@ -10,7 +10,9 @@ const run_docker = async(infrastructure_dir, proj_prefix) => {
|
|
|
10
10
|
|
|
11
11
|
// compose files
|
|
12
12
|
const compose_files = ["-f common.yml"]
|
|
13
|
-
if (is_production)
|
|
13
|
+
if (is_production) {
|
|
14
|
+
compose_files.push("-f prod.yml")
|
|
15
|
+
}
|
|
14
16
|
else compose_files.push("-f dev.yml")
|
|
15
17
|
|
|
16
18
|
const opts = ["--build", "--remove-orphans"]
|
package/cli/run_native.js
CHANGED
package/cli/start_server.js
CHANGED
package/database.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
const mongoose = require("mongoose")
|
|
3
3
|
const validator = require("validator")
|
|
4
4
|
|
|
5
|
-
const {DATABASE_NAME} = process.env
|
|
5
|
+
const {DATABASE_NAME, DATABASE_PORT} = process.env
|
|
6
6
|
|
|
7
|
-
if (typeof DATABASE_PORT
|
|
7
|
+
if (typeof DATABASE_PORT !== "string" || !validator.isPort(DATABASE_PORT)) {
|
|
8
8
|
throw new Error("expected DATABASE_PORT to be a valid port number")
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -12,12 +12,7 @@ if (typeof DATABASE_NAME !== "string") {
|
|
|
12
12
|
throw new Error("expected DATABASE_NAME to be a string")
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const
|
|
16
|
-
.join(",")
|
|
17
|
-
|
|
18
|
-
const mongo_url = `mongodb://${replicaset_str}/${DATABASE_NAME}`
|
|
19
|
-
// &readPreference=secondary
|
|
20
|
-
|
|
15
|
+
const mongo_url = `mongodb://127.0.0.1:${DATABASE_PORT}/${DATABASE_NAME}`
|
|
21
16
|
|
|
22
17
|
module.exports = async() => {
|
|
23
18
|
|
|
@@ -32,29 +27,17 @@ module.exports = async() => {
|
|
|
32
27
|
|
|
33
28
|
// TODO: should we be retrying in dev only
|
|
34
29
|
mongoose.connection.on("error", (err) => {
|
|
35
|
-
console.log("Mongoose connection error", /*err*/)
|
|
36
30
|
// replicaSet has no primary, retry connecting in a moment
|
|
37
|
-
|
|
38
|
-
console.log("no primary, will retry")
|
|
39
|
-
setTimeout(async() => {
|
|
40
|
-
console.log("retrying now")
|
|
41
|
-
await mongoose.connect(mongo_url, connect_opts)
|
|
42
|
-
}, 3000)
|
|
43
|
-
} else {
|
|
44
|
-
console.log("WOW ici mongoose connection error", err)
|
|
45
|
-
}
|
|
31
|
+
console.log("mongoose connection error", err)
|
|
46
32
|
})
|
|
47
33
|
|
|
48
34
|
|
|
49
|
-
console.log("mongo_url", mongo_url)
|
|
35
|
+
console.log("connect mongo_url", mongo_url)
|
|
50
36
|
|
|
51
37
|
const connect_opts = {
|
|
52
|
-
replicaSet: "rs0",
|
|
53
38
|
keepAlive: true,
|
|
54
39
|
keepAliveInitialDelay: 5000,
|
|
55
|
-
minPoolSize:
|
|
56
|
-
serverSelectionTimeoutMS: 5000,
|
|
57
|
-
heartbeatFrequencyMS: 1000,
|
|
40
|
+
minPoolSize: 5,
|
|
58
41
|
family: 4, // force ipv4
|
|
59
42
|
}
|
|
60
43
|
|
|
@@ -65,18 +48,10 @@ module.exports = async() => {
|
|
|
65
48
|
await mongoose.connect(mongo_url, connect_opts)
|
|
66
49
|
} catch (err) {
|
|
67
50
|
// TODO: handle error ?
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// await mongoose.connect(mongo_url, connect_opts)
|
|
73
|
-
// }, 3000)
|
|
74
|
-
} else {
|
|
75
|
-
console.log("ICI SERVER GOT ERROR")
|
|
76
|
-
console.log(err)
|
|
77
|
-
console.log("JSON:", JSON.stringify(err, null, 2))
|
|
78
|
-
console.log("resaon", err.reason)
|
|
79
|
-
}
|
|
51
|
+
console.log("mongoose connect SERVER GOT ERROR")
|
|
52
|
+
console.log(err)
|
|
53
|
+
console.log("JSON:", JSON.stringify(err, null, 2))
|
|
54
|
+
console.log("resaon", err.reason)
|
|
80
55
|
}
|
|
81
56
|
|
|
82
57
|
}
|
|
@@ -14,44 +14,60 @@ if (typeof SESSION_STORE_PORT === "string" && !validator.isPort(SESSION_STORE_PO
|
|
|
14
14
|
throw new Error("expected SESSION_STORE_PORT to be a valid port number")
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
17
|
+
let session_middleware = null
|
|
18
|
+
|
|
19
|
+
// extreme warning: when there is blocking io redis client won't connect
|
|
20
|
+
process.nextTick(async() => {
|
|
21
|
+
|
|
22
|
+
if (!SESSION_STORE_PORT) {
|
|
23
|
+
return
|
|
24
24
|
}
|
|
25
|
-
}
|
|
26
25
|
|
|
26
|
+
const reconnectStrategy = (retries) => {
|
|
27
|
+
console.log("redis_client::reconnectStrategy::retrying with arg", retries)
|
|
28
|
+
if (retries < 5) {
|
|
29
|
+
console.log("retry count:", retries, "retrying in 1s")
|
|
30
|
+
return 4000
|
|
31
|
+
} else {
|
|
32
|
+
return new Error("max retries expiered")
|
|
33
|
+
}
|
|
34
|
+
}
|
|
27
35
|
|
|
28
|
-
const redis_client = createClient({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
+
const redis_client = createClient({
|
|
37
|
+
socket: {
|
|
38
|
+
host: "127.0.0.1",
|
|
39
|
+
port: SESSION_STORE_PORT,
|
|
40
|
+
reconnectStrategy,
|
|
41
|
+
connectTimeout: 10000,
|
|
42
|
+
keepAlive: 0,
|
|
43
|
+
},
|
|
44
|
+
legacyMode: true,
|
|
45
|
+
})
|
|
36
46
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
console.log("redis session willconnect")
|
|
48
|
+
await redis_client.connect()
|
|
49
|
+
console.log("redis session connected")
|
|
50
|
+
|
|
51
|
+
session_middleware = session({
|
|
52
|
+
store: new redis_store({client: redis_client}),
|
|
53
|
+
proxy: true,
|
|
54
|
+
saveUninitialized: false,
|
|
55
|
+
// TODO: use session secret from env
|
|
56
|
+
secret: "session secret wowow",
|
|
57
|
+
resave: false,
|
|
58
|
+
})
|
|
47
59
|
|
|
48
|
-
const session_middleware = session({
|
|
49
|
-
store: new redis_store({client: redis_client}),
|
|
50
|
-
proxy: true,
|
|
51
|
-
saveUninitialized: false,
|
|
52
|
-
// TODO: use session secret from env
|
|
53
|
-
secret: "session secret wowow",
|
|
54
|
-
resave: false,
|
|
55
60
|
})
|
|
56
61
|
|
|
57
|
-
|
|
62
|
+
|
|
63
|
+
module.exports = (req, res, next) => {
|
|
64
|
+
if (!SESSION_STORE_PORT) {
|
|
65
|
+
return next()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (session_middleware) {
|
|
69
|
+
session_middleware(req, res, next)
|
|
70
|
+
} else {
|
|
71
|
+
console.warn("session middleware not set yet")
|
|
72
|
+
}
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"cors": "2.8.5",
|
|
16
16
|
"debug": "4.3.4",
|
|
17
17
|
"dotenv": "16.0.0",
|
|
18
|
-
"express": "4.
|
|
18
|
+
"express": "4.18.1",
|
|
19
19
|
"express-session": "1.17.2",
|
|
20
20
|
"glob": "8.0.1",
|
|
21
|
-
"mongoose": "6.3.
|
|
21
|
+
"mongoose": "6.3.2",
|
|
22
22
|
"postmark": "3.0.1",
|
|
23
|
-
"redis": "4.0
|
|
23
|
+
"redis": "4.1.0",
|
|
24
24
|
"validator": "13.7.0",
|
|
25
25
|
"yargs": "17.4.1"
|
|
26
26
|
}
|