@rpcbase/server 0.1.0 → 0.5.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 +32 -0
- package/cli/start_server.js +11 -0
- package/client/client_router.js +54 -0
- package/index.js +3 -1
- package/package.json +8 -2
package/bin.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* @flow */
|
|
3
|
+
const path = require("path")
|
|
4
|
+
require("dotenv").config({path: path.join(__dirname, "./.env")})
|
|
5
|
+
const yargs = require("yargs/yargs")
|
|
6
|
+
const {hideBin} = require("yargs/helpers")
|
|
7
|
+
|
|
8
|
+
const start_server = require("./start_server")
|
|
9
|
+
|
|
10
|
+
let is_command = false
|
|
11
|
+
|
|
12
|
+
const args = yargs(hideBin(process.argv))
|
|
13
|
+
.command("start", "run server/infrastructure", () => {}, (argv) => {
|
|
14
|
+
is_command = true
|
|
15
|
+
console.log("run start server/infrastructure")
|
|
16
|
+
start_server()
|
|
17
|
+
})
|
|
18
|
+
.command("ping", "ping", () => {}, (argv) => {
|
|
19
|
+
is_command = true
|
|
20
|
+
console.log("OING")
|
|
21
|
+
})
|
|
22
|
+
.option("verbose", {
|
|
23
|
+
alias: "v",
|
|
24
|
+
type: "boolean",
|
|
25
|
+
default: false,
|
|
26
|
+
description: "Run with verbose logging"
|
|
27
|
+
})
|
|
28
|
+
.parse()
|
|
29
|
+
|
|
30
|
+
if (!is_command) {
|
|
31
|
+
console.log("default command")
|
|
32
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const fs = require("fs")
|
|
3
|
+
const path = require("path")
|
|
4
|
+
const glob = require("glob")
|
|
5
|
+
|
|
6
|
+
const src_path = path.join(process.cwd(), "./src/")
|
|
7
|
+
const build_dir = path.join(process.cwd(), "build/")
|
|
8
|
+
const client_build_dir = path.join(build_dir, "./client")
|
|
9
|
+
const index_file_buffer = fs.readFileSync(path.join(client_build_dir, "./index.html"))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// TODO: add build time static assets compression
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const async_wrapper = fn => (req, res, next) => {
|
|
16
|
+
Promise.resolve(fn(req, res, next))
|
|
17
|
+
.catch(next)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const get_client_routes = () => {
|
|
21
|
+
const client_files = glob.sync(path.join(client_build_dir, "./**/*"))
|
|
22
|
+
const routes = client_files
|
|
23
|
+
.filter((f) => {
|
|
24
|
+
const stat = fs.statSync(f)
|
|
25
|
+
return !stat.isDirectory()
|
|
26
|
+
})
|
|
27
|
+
.map((f) => `/${path.relative(client_build_dir, f)}`)
|
|
28
|
+
return routes
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
const client_router = (app) => {
|
|
33
|
+
const client_routes = get_client_routes()
|
|
34
|
+
|
|
35
|
+
app.get("*", async_wrapper(async(req, res, next) => {
|
|
36
|
+
const full_path = req.baseUrl + req.path
|
|
37
|
+
|
|
38
|
+
if (full_path.startsWith("/api/") || full_path.startsWith("/rpc/")) {
|
|
39
|
+
return next()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (client_routes.indexOf(full_path) > -1) {
|
|
43
|
+
const file_path = path.join(client_build_dir, full_path)
|
|
44
|
+
return res.sendFile(file_path)
|
|
45
|
+
} else {
|
|
46
|
+
res.writeHead(200, {
|
|
47
|
+
"Content-Type": "text/html"
|
|
48
|
+
})
|
|
49
|
+
res.end(index_file_buffer)
|
|
50
|
+
}
|
|
51
|
+
}))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = client_router
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"rb-server": "./bin.js"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
10
|
"test": "echo \"Error: no test specified\" && exit 0"
|
|
8
11
|
},
|
|
9
12
|
"dependencies": {
|
|
10
|
-
"
|
|
13
|
+
"connect-redis": "6.0.0",
|
|
14
|
+
"glob": "7.2.0",
|
|
15
|
+
"redis": "3.1.2",
|
|
16
|
+
"yargs": "17.3.0"
|
|
11
17
|
}
|
|
12
18
|
}
|