@rpcbase/server 0.2.0 → 0.6.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 ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env node
2
+ /* @flow */
3
+ const path = require("path")
4
+ require("dotenv").config({path: path.join(__dirname, "./.env")})
5
+
6
+ const yargs = require("yargs/yargs")
7
+ const {hideBin} = require("yargs/helpers")
8
+
9
+ const start_server = require("./cli/start_server")
10
+
11
+ let is_command = false
12
+
13
+ const args = yargs(hideBin(process.argv))
14
+ .command("start", "run server/infrastructure", () => {}, (argv) => {
15
+ is_command = true
16
+ console.log("run start server/infrastructure")
17
+ start_server()
18
+ })
19
+ .command("ping", "ping", () => {}, (argv) => {
20
+ is_command = true
21
+ console.log("OING")
22
+ })
23
+ .option("verbose", {
24
+ alias: "v",
25
+ type: "boolean",
26
+ default: false,
27
+ description: "Run with verbose logging"
28
+ })
29
+ .parse()
30
+
31
+ if (!is_command) {
32
+ console.log("default command")
33
+ }
@@ -0,0 +1,34 @@
1
+ /* @flow */
2
+ const {execSync} = require("child_process")
3
+ const path = require("path")
4
+ const fs = require("fs")
5
+
6
+ const is_production = "yes" === process.env.INFRASTRUCTURE_IS_PRODUCTION
7
+
8
+ const start_server = async() => {
9
+ if (is_production) {
10
+ console.log("server will run in production")
11
+ }
12
+
13
+ const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
14
+
15
+ // env
16
+ const env_path = path.relative(infrastructure_dir, path.join(process.cwd(), "./.env"))
17
+
18
+ // prefix
19
+ const proj_parent_dir = path.join(infrastructure_dir, "../../")
20
+ const proj_prefix = path.basename(proj_parent_dir)
21
+
22
+ // compose files
23
+ const compose_files = ["-f common.yml"]
24
+ if (is_production) compose_files.push("-f prod.yml")
25
+ else compose_files.push("-f dev.yml")
26
+
27
+ const cmd = `docker-compose --env-file ${env_path} -p ${proj_prefix} ${compose_files.join(" ")} up --build --remove-orphans`
28
+
29
+ console.log("RUN", cmd)
30
+ execSync(cmd, {stdio: "inherit", cwd: infrastructure_dir})
31
+
32
+ }
33
+
34
+ module.exports = start_server
@@ -5,6 +5,11 @@ const glob = require("glob")
5
5
 
6
6
  const src_path = path.join(process.cwd(), "./src/")
7
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
8
13
 
9
14
 
10
15
  const async_wrapper = fn => (req, res, next) => {
@@ -12,36 +17,38 @@ const async_wrapper = fn => (req, res, next) => {
12
17
  .catch(next)
13
18
  }
14
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
+
15
31
 
16
32
  const client_router = (app) => {
33
+ const client_routes = get_client_routes()
17
34
 
18
35
  app.get("*", async_wrapper(async(req, res, next) => {
19
- console.log("client router wow", req)
20
- 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
+ }
21
51
  }))
22
-
23
- // const rpc_routes = glob.sync(path.join(build_dir, "./rpc/*"))
24
- //
25
- // rpc_routes.forEach((file_path) => {
26
- // const basename = path.basename(file_path)
27
- // const route_path = basename.replace(/__slash__/g, "/")
28
- // const module_path = path.join(src_path, `${route_path}.js`)
29
- //
30
- // // if module not found, remove created route file
31
- // if (!fs.existsSync(module_path)) {
32
- // fs.unlinkSync(file_path)
33
- // console.log("removed inexistant rpc route", basename, "with module path", module_path)
34
- // }
35
- //
36
- // const rpc_fn = require(module_path)
37
- // app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
38
- // // console.log("GOT REQ", req.body)
39
- // const result = await rpc_fn(req.body, req)
40
- // // console.log("rpc", route_path, result)
41
- // res.json(result)
42
- // }))
43
- // })
44
-
45
52
  }
46
53
 
47
54
  module.exports = client_router
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.2.0",
3
+ "version": "0.6.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
- "glob": "7.2.0"
13
+ "connect-redis": "6.0.0",
14
+ "dotenv": "10.0.0",
15
+ "glob": "7.2.0",
16
+ "redis": "3.1.2",
17
+ "yargs": "17.3.0"
11
18
  }
12
19
  }