@rpcbase/server 0.36.0 → 0.39.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/cli/get_runtime.js +14 -0
- package/cli/native/get_commands.js +3 -0
- package/cli/run_docker.js +31 -0
- package/cli/run_native.js +46 -0
- package/cli/start_server.js +10 -20
- package/database.js +4 -2
- package/express/index.js +5 -3
- package/express/session_middleware.js +24 -26
- package/package.json +4 -3
- package/rpc/rpc_router.js +5 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const os = require("os")
|
|
3
|
+
|
|
4
|
+
const get_runtime = () => {
|
|
5
|
+
if (os.platform() === "darwin") {
|
|
6
|
+
console.warn("warning: native mode is tmp disabled")
|
|
7
|
+
// return "native"
|
|
8
|
+
return "docker"
|
|
9
|
+
} else {
|
|
10
|
+
return "docker"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = get_runtime
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const {execSync} = require("child_process")
|
|
3
|
+
const path = require("path")
|
|
4
|
+
|
|
5
|
+
const is_production = "yes" === process.env.INFRASTRUCTURE_IS_PRODUCTION
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const run_docker = async(infrastructure_dir, proj_prefix) => {
|
|
9
|
+
// env
|
|
10
|
+
const env_path = path.relative(infrastructure_dir, path.join(process.cwd(), "./.env"))
|
|
11
|
+
|
|
12
|
+
// compose files
|
|
13
|
+
const compose_files = ["-f common.yml"]
|
|
14
|
+
if (is_production) compose_files.push("-f prod.yml")
|
|
15
|
+
else compose_files.push("-f dev.yml")
|
|
16
|
+
|
|
17
|
+
const opts = ["--build", "--remove-orphans"]
|
|
18
|
+
if (!is_production) opts.push("--abort-on-container-exit")
|
|
19
|
+
throw "shit"
|
|
20
|
+
const cmd = `docker-compose --quiet --env-file ${env_path} -p ${proj_prefix} ${compose_files.join(" ")} up ${opts.join(" ")}`
|
|
21
|
+
|
|
22
|
+
console.log(cmd)
|
|
23
|
+
try {
|
|
24
|
+
execSync(cmd, {stdio: "inherit", cwd: infrastructure_dir})
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.log("start_server::error\n", err)
|
|
27
|
+
process.exit(1)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = run_docker
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const {spawn} = require("child_process")
|
|
3
|
+
const dotenv = require("dotenv")
|
|
4
|
+
const path = require("path")
|
|
5
|
+
const fs = require("fs")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const run_processes = [
|
|
9
|
+
["mongod", [
|
|
10
|
+
"--quiet",
|
|
11
|
+
"--port", "$DATABASE_PORT_0",
|
|
12
|
+
"--replSet", "rs0",
|
|
13
|
+
"--logpath", "/var/log/logs.txt",
|
|
14
|
+
]],
|
|
15
|
+
["mongod", [
|
|
16
|
+
"--quiet",
|
|
17
|
+
"--port", "$DATABASE_PORT_2",
|
|
18
|
+
"--replSet", "rs0",
|
|
19
|
+
"--logpath", "/var/log/logs.txt",
|
|
20
|
+
]],
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const get_env = () => {
|
|
26
|
+
const env_path = path.join(process.cwd(), ".env")
|
|
27
|
+
const env_buf = fs.readFileSync(env_path)
|
|
28
|
+
const env = dotenv.parse(env_buf)
|
|
29
|
+
|
|
30
|
+
return env
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
const run_native = (infrastructure_dir, proj_prefix) => {
|
|
36
|
+
console.log("native mode")
|
|
37
|
+
const env = get_env()
|
|
38
|
+
|
|
39
|
+
const ps = spawn("ls", ["-la"])
|
|
40
|
+
ps.stdout.on("data", (data) => {
|
|
41
|
+
console.log("stdout", data.toString())
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = run_native
|
package/cli/start_server.js
CHANGED
|
@@ -1,40 +1,30 @@
|
|
|
1
1
|
/* @flow */
|
|
2
|
-
const {execSync} = require("child_process")
|
|
3
2
|
const path = require("path")
|
|
4
3
|
const fs = require("fs")
|
|
5
4
|
|
|
6
|
-
const
|
|
5
|
+
const get_runtime = require("./get_runtime")
|
|
6
|
+
const run_docker = require("./run_docker")
|
|
7
|
+
const run_native = require("./run_native")
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
// TODO: auto create network if necessary
|
|
9
11
|
// docker network create local-network
|
|
10
12
|
|
|
11
13
|
const start_server = async() => {
|
|
12
|
-
if (is_production) {
|
|
13
|
-
console.log("server will run in production")
|
|
14
|
-
}
|
|
15
14
|
|
|
16
15
|
const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
|
|
17
16
|
|
|
18
|
-
// env
|
|
19
|
-
const env_path = path.relative(infrastructure_dir, path.join(process.cwd(), "./.env"))
|
|
20
|
-
|
|
21
17
|
// prefix
|
|
22
18
|
const proj_parent_dir = path.join(infrastructure_dir, "../../")
|
|
23
19
|
const proj_prefix = path.basename(proj_parent_dir)
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
const compose_files = ["-f common.yml"]
|
|
27
|
-
if (is_production) compose_files.push("-f prod.yml")
|
|
28
|
-
else compose_files.push("-f dev.yml")
|
|
29
|
-
|
|
30
|
-
const cmd = `docker-compose --env-file ${env_path} -p ${proj_prefix} ${compose_files.join(" ")} up --build --remove-orphans`
|
|
21
|
+
const runtime = get_runtime()
|
|
31
22
|
|
|
32
|
-
console.log(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
process.exit(1)
|
|
23
|
+
console.log("RUN", runtime)
|
|
24
|
+
if (runtime === "native") {
|
|
25
|
+
run_native(infrastructure_dir, proj_prefix)
|
|
26
|
+
} else {
|
|
27
|
+
run_docker(infrastructure_dir, proj_prefix)
|
|
38
28
|
}
|
|
39
29
|
|
|
40
30
|
}
|
package/database.js
CHANGED
|
@@ -18,10 +18,12 @@ const replicaset_str = [0, 1, 2].map((i) => `database${i}:${process.env["DATABAS
|
|
|
18
18
|
console.log("db connection str:", replicaset_str)
|
|
19
19
|
|
|
20
20
|
const mongo_url = `mongodb://${replicaset_str}/${DATABASE_NAME}?`
|
|
21
|
-
+ `replicaSet=rs0
|
|
21
|
+
+ `replicaSet=rs0`
|
|
22
|
+
|
|
23
|
+
// &readPreference=secondary
|
|
22
24
|
|
|
23
25
|
module.exports = async() => {
|
|
24
|
-
console.log("
|
|
26
|
+
console.log("MONGO_URL", mongo_url)
|
|
25
27
|
const ret = await mongoose.connect(mongo_url)
|
|
26
28
|
// console.log("waiting after return", ret)
|
|
27
29
|
// TODO: handle connection retry and pools
|
package/express/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const {APP_DOMAIN, CLIENT_PORT} = process.env
|
|
|
11
11
|
|
|
12
12
|
module.exports = () => {
|
|
13
13
|
const app = express()
|
|
14
|
-
app.use(body_parser.json())
|
|
14
|
+
app.use(body_parser.json({limit: "2mb"}))
|
|
15
15
|
|
|
16
16
|
app.set("trust proxy", 1)
|
|
17
17
|
|
|
@@ -38,9 +38,10 @@ module.exports = () => {
|
|
|
38
38
|
// local dev origins
|
|
39
39
|
[
|
|
40
40
|
`http://localhost:${CLIENT_PORT}`,
|
|
41
|
-
"http://localhost:8090",
|
|
41
|
+
"http://localhost:8090", // TMP: used by client of admin
|
|
42
42
|
"localhost",
|
|
43
43
|
]
|
|
44
|
+
|
|
44
45
|
// console.log("SETUP CORS", cors_origins)
|
|
45
46
|
|
|
46
47
|
app.use(cors({
|
|
@@ -52,7 +53,8 @@ module.exports = () => {
|
|
|
52
53
|
console.log("use session middleware")
|
|
53
54
|
app.use(session_middleware)
|
|
54
55
|
|
|
55
|
-
app.get("/api/ping", (req, res) => res.json({
|
|
56
|
+
app.get("/api/ping", (req, res) => res.json({message: "pong"}))
|
|
57
|
+
app.post("/api/ping", (req, res) => res.json({message: "pong"}))
|
|
56
58
|
|
|
57
59
|
return app
|
|
58
60
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
const session = require("express-session")
|
|
3
|
-
// const redis = require("redis")
|
|
4
3
|
const validator = require("validator")
|
|
4
|
+
const {createClient} = require("redis")
|
|
5
5
|
const redis_store = require("connect-redis")(session)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
// WARNING:
|
|
8
|
+
// https://stackoverflow.com/questions/70867229/error-connection-timeout-when-connecting-to-redis-docker-instance
|
|
9
|
+
// https://github.com/redis/node-redis/issues/1656/
|
|
9
10
|
|
|
10
11
|
const {SESSION_STORE_PORT} = process.env
|
|
11
12
|
|
|
@@ -13,13 +14,10 @@ if (typeof SESSION_STORE_PORT === "string" && !validator.isPort(SESSION_STORE_PO
|
|
|
13
14
|
throw new Error("expected SESSION_STORE_PORT to be a valid port number")
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
console.log("will connect to session-store", session_store_url)
|
|
19
|
-
|
|
17
|
+
// console.log("ICIII", execSync("redis-cli -p 7979 -h session-store ping", {stdio: "inherit"}))
|
|
20
18
|
|
|
21
19
|
const reconnectStrategy = (retries) => {
|
|
22
|
-
console.log("retrying with arg", retries)
|
|
20
|
+
console.log("reconnectStrategy::retrying with arg", retries)
|
|
23
21
|
if (retries < 5) {
|
|
24
22
|
console.log("retry count:", retries, "retrying in 1s")
|
|
25
23
|
return 1000
|
|
@@ -29,31 +27,31 @@ const reconnectStrategy = (retries) => {
|
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
const redis_client = createClient({
|
|
31
|
+
socket: {
|
|
32
|
+
host: "session-store",
|
|
33
|
+
port: SESSION_STORE_PORT,
|
|
34
|
+
reconnectStrategy
|
|
35
|
+
},
|
|
36
36
|
legacyMode: true,
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// })
|
|
39
|
+
// extreme warning: when there is blocking io redis client won't connect
|
|
40
|
+
process.nextTick(() => {
|
|
41
|
+
redis_client.connect()
|
|
42
|
+
.catch((err) => {
|
|
43
|
+
console.log("session store error ohh", err)
|
|
44
|
+
})
|
|
45
|
+
.then(() => {
|
|
46
|
+
console.log("redis session-store connected port:", SESSION_STORE_PORT)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
50
49
|
|
|
51
50
|
const session_middleware = session({
|
|
52
|
-
|
|
53
|
-
store: new redis_store({client: redisClient}),
|
|
51
|
+
store: new redis_store({client: redis_client}),
|
|
54
52
|
proxy: true,
|
|
55
53
|
saveUninitialized: false,
|
|
56
|
-
// TODO:
|
|
54
|
+
// TODO: use session secret from env
|
|
57
55
|
secret: "session secret wowow",
|
|
58
56
|
resave: false,
|
|
59
57
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -11,13 +11,14 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"body-parser": "1.19.2",
|
|
14
|
-
"connect-redis": "6.1.
|
|
14
|
+
"connect-redis": "6.1.2",
|
|
15
15
|
"cors": "2.8.5",
|
|
16
|
+
"debug": "4.3.3",
|
|
16
17
|
"dotenv": "16.0.0",
|
|
17
18
|
"express": "4.17.3",
|
|
18
19
|
"express-session": "1.17.2",
|
|
19
20
|
"glob": "7.2.0",
|
|
20
|
-
"mongoose": "6.2.
|
|
21
|
+
"mongoose": "6.2.6",
|
|
21
22
|
"redis": "4.0.4",
|
|
22
23
|
"validator": "13.7.0",
|
|
23
24
|
"yargs": "17.3.1"
|
package/rpc/rpc_router.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const fs = require("fs")
|
|
3
3
|
const path = require("path")
|
|
4
4
|
const glob = require("glob")
|
|
5
|
+
const debug = require("debug")
|
|
5
6
|
|
|
6
7
|
const src_path = path.join(process.cwd(), "./src/")
|
|
7
8
|
const build_dir = path.join(process.cwd(), "build/")
|
|
@@ -16,6 +17,9 @@ const async_wrapper = fn => (req, res, next) => {
|
|
|
16
17
|
const rpc_router = (app) => {
|
|
17
18
|
const rpc_routes = glob.sync(path.join(build_dir, "./rpc/*"))
|
|
18
19
|
|
|
20
|
+
|
|
21
|
+
console.log("rpc router setup")
|
|
22
|
+
|
|
19
23
|
rpc_routes.forEach((file_path) => {
|
|
20
24
|
const basename = path.basename(file_path)
|
|
21
25
|
const route_path = basename.replace(/__slash__/g, "/")
|
|
@@ -30,6 +34,7 @@ const rpc_router = (app) => {
|
|
|
30
34
|
const rpc_fn = require(module_path)
|
|
31
35
|
app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
|
|
32
36
|
// console.log("GOT REQ", req.body)
|
|
37
|
+
console.log(`POST /rpc/${route_path}`)
|
|
33
38
|
// TODO: add jaegerclient / opentelemetry
|
|
34
39
|
const result = await rpc_fn(req.body, {req})
|
|
35
40
|
|