binhend 1.0.2 → 1.0.4
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/example/routes/test/index.js +1 -3
- package/example/routes/test/some.js +1 -3
- package/example/routes/testa.js +1 -3
- package/package.json +2 -2
- package/src/api.js +7 -2
- package/src/binh.js +31 -11
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { router, get, post } = binh().Router();
|
|
1
|
+
const { router, get, post } = binh().Router(module);
|
|
2
2
|
|
|
3
3
|
get('/', function(req, res) {
|
|
4
4
|
res.json({ path:'/', dir: __dirname, file: __filename });
|
|
@@ -11,5 +11,3 @@ get('/sub', function(req, res) {
|
|
|
11
11
|
get('/goo', function(req, res) {
|
|
12
12
|
res.json({ path:'/goo', dir: __dirname, file: __filename });
|
|
13
13
|
});
|
|
14
|
-
|
|
15
|
-
module.exports = router;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { router, get, post } = binh().Router();
|
|
1
|
+
const { router, get, post } = binh().Router(module);
|
|
2
2
|
|
|
3
3
|
get('/', function(req, res) {
|
|
4
4
|
res.json({ path:'/', dir: __dirname, file: __filename });
|
|
@@ -7,5 +7,3 @@ get('/', function(req, res) {
|
|
|
7
7
|
get('/sub', function(req, res) {
|
|
8
8
|
res.json({ path:'/sub', dir: __dirname, file: __filename });
|
|
9
9
|
});
|
|
10
|
-
|
|
11
|
-
module.exports = router;
|
package/example/routes/testa.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { router, get, post } = binh().Router();
|
|
1
|
+
const { router, get, post } = binh().Router(module);
|
|
2
2
|
|
|
3
3
|
get('/', function(req, res) {
|
|
4
4
|
res.json({ path:'/', dir: __dirname, file: __filename });
|
|
@@ -11,5 +11,3 @@ get('/sub', function(req, res) {
|
|
|
11
11
|
get('/gaa', function(req, res) {
|
|
12
12
|
res.json({ path:'/gaa', dir: __dirname, file: __filename });
|
|
13
13
|
});
|
|
14
|
-
|
|
15
|
-
module.exports = router;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binhend",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Nguyen Duc Binh",
|
|
7
7
|
"license": "UNLICENSED",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
10
|
-
"example": "node example"
|
|
10
|
+
"example": "node example PORT=5555"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"express": "^4.17.1"
|
package/src/api.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
const { Module } = require('module');
|
|
1
2
|
const { readdir, statSync } = require('fs');
|
|
2
3
|
const { join, parse } = require('path');
|
|
3
|
-
|
|
4
|
+
|
|
4
5
|
const express = require('express');
|
|
5
6
|
|
|
6
7
|
function mapAPIs(dirpath, router) {
|
|
@@ -57,7 +58,7 @@ function APIs(rootpath) {
|
|
|
57
58
|
return router;
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
function Router() {
|
|
61
|
+
function Router(module) {
|
|
61
62
|
var router = express.Router();
|
|
62
63
|
|
|
63
64
|
var output = { router };
|
|
@@ -68,6 +69,10 @@ function Router() {
|
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
|
|
72
|
+
if (module instanceof Module) {
|
|
73
|
+
module.exports = router;
|
|
74
|
+
}
|
|
75
|
+
|
|
71
76
|
return output;
|
|
72
77
|
}
|
|
73
78
|
|
package/src/binh.js
CHANGED
|
@@ -15,12 +15,23 @@ function generateId() {
|
|
|
15
15
|
crypto.randomUUID({ disableEntropyCache: true });
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function defaultConfigLoader(loader) {
|
|
19
|
+
loader.object(process.env);
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
loader.file('.env');
|
|
23
|
+
}
|
|
24
|
+
catch(e) {}
|
|
25
|
+
|
|
26
|
+
loader.cli();
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
function Binh() {
|
|
19
30
|
var _this = this,
|
|
20
31
|
forkcount = 0,
|
|
21
32
|
rootpath = require.main.path;
|
|
22
33
|
|
|
23
|
-
var config = {}, loadConfigs = null, configloader = new ConfigLoader(config);
|
|
34
|
+
var config = {}, loadConfigs = null, groupLoadConfigs = [], configloader = new ConfigLoader(config);
|
|
24
35
|
|
|
25
36
|
var binh = function(module_path) {
|
|
26
37
|
return module_path == undefined ? module.exports : require(`${rootpath}/${module_path}`);
|
|
@@ -32,12 +43,15 @@ function Binh() {
|
|
|
32
43
|
|
|
33
44
|
binh.config.reload = function() {
|
|
34
45
|
return new Promise(function(resolve) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
var loader = new ConfigLoader(config);
|
|
47
|
+
|
|
48
|
+
groupLoadConfigs.forEach(function(loadConfigs) {
|
|
49
|
+
if (loadConfigs instanceof Function) {
|
|
50
|
+
loadConfigs(loader);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
loader.done(resolve);
|
|
41
55
|
});
|
|
42
56
|
};
|
|
43
57
|
|
|
@@ -55,25 +69,31 @@ function Binh() {
|
|
|
55
69
|
this.id = function(id) {
|
|
56
70
|
binh.id = id == undefined ? generateId() : JSON.stringify(id);
|
|
57
71
|
return this;
|
|
58
|
-
}
|
|
72
|
+
};
|
|
59
73
|
|
|
60
74
|
this.global = function(name, target) {
|
|
61
75
|
global[name] = target;
|
|
62
76
|
return this;
|
|
63
|
-
}
|
|
77
|
+
};
|
|
64
78
|
|
|
65
79
|
this.config = function(module) {
|
|
66
80
|
if (module instanceof Function) {
|
|
67
81
|
loadConfigs = module;
|
|
68
|
-
|
|
82
|
+
loadConfigs(configloader);
|
|
69
83
|
}
|
|
70
84
|
else if (typeof module === 'string') {
|
|
71
85
|
loadConfigs = binh(module);
|
|
72
86
|
loadConfigs(configloader);
|
|
73
87
|
}
|
|
88
|
+
else {
|
|
89
|
+
loadConfigs = defaultConfigLoader;
|
|
90
|
+
loadConfigs(configloader);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
groupLoadConfigs.push(loadConfigs);
|
|
74
94
|
|
|
75
95
|
return this;
|
|
76
|
-
}
|
|
96
|
+
};
|
|
77
97
|
|
|
78
98
|
this.cluster = {
|
|
79
99
|
fork: function(number) {
|