binhend 1.0.0 → 1.0.1
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/config.js +4 -0
- package/example/index.js +5 -7
- package/package.json +1 -3
- package/src/api.js +3 -1
- package/src/binh.js +29 -16
- package/src/server.js +4 -0
package/example/index.js
CHANGED
|
@@ -4,13 +4,11 @@ const { Binh } = require('../src/binh');
|
|
|
4
4
|
|
|
5
5
|
new Binh()
|
|
6
6
|
.id()
|
|
7
|
-
.
|
|
8
|
-
.config(
|
|
9
|
-
loader.object({ PORT: 1400 });
|
|
10
|
-
})
|
|
7
|
+
.global('config', binh.config)
|
|
8
|
+
.config('config')
|
|
11
9
|
.api('routes')
|
|
12
|
-
.
|
|
13
|
-
.
|
|
10
|
+
.port('PORT')
|
|
11
|
+
.cluster.fork(0)
|
|
14
12
|
.start(function(server, configs) {
|
|
15
13
|
console.log('### START >>> configs:', configs);
|
|
16
|
-
});
|
|
14
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binhend",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Nguyen Duc Binh",
|
|
@@ -12,8 +12,6 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"express": "^4.17.1"
|
|
14
14
|
},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
},
|
|
17
15
|
"engines": {
|
|
18
16
|
"node": ">= 16.17.1"
|
|
19
17
|
}
|
package/src/api.js
CHANGED
package/src/binh.js
CHANGED
|
@@ -18,14 +18,14 @@ function generateId() {
|
|
|
18
18
|
function Binh() {
|
|
19
19
|
var _this = this,
|
|
20
20
|
forkcount = 0,
|
|
21
|
-
rootpath =
|
|
21
|
+
rootpath = require.main.path;
|
|
22
|
+
|
|
23
|
+
var config = {}, loadConfigs = null, configloader = new ConfigLoader(config);
|
|
22
24
|
|
|
23
25
|
var binh = function(module_path) {
|
|
24
26
|
return module_path == undefined ? module.exports : require(`${rootpath}/${module_path}`);
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
var config = {}, loadConfigs = null, configloader = new ConfigLoader(config);
|
|
28
|
-
|
|
29
29
|
binh.config = function(key) {
|
|
30
30
|
return typeof key === 'string' ? config[key] : config;
|
|
31
31
|
};
|
|
@@ -48,7 +48,7 @@ function Binh() {
|
|
|
48
48
|
global.binh = binh;
|
|
49
49
|
|
|
50
50
|
this.rootpath = function(path) {
|
|
51
|
-
rootpath = typeof path === 'string' ? path :
|
|
51
|
+
rootpath = typeof path === 'string' ? path : require.main.path;
|
|
52
52
|
return this;
|
|
53
53
|
};
|
|
54
54
|
|
|
@@ -57,11 +57,21 @@ function Binh() {
|
|
|
57
57
|
return this;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
this.global = function(name, target) {
|
|
61
|
+
global[name] = target;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.config = function(module) {
|
|
66
|
+
if (module instanceof Function) {
|
|
67
|
+
loadConfigs = module;
|
|
68
|
+
module(configloader);
|
|
64
69
|
}
|
|
70
|
+
else if (typeof module === 'string') {
|
|
71
|
+
loadConfigs = binh(module);
|
|
72
|
+
loadConfigs(configloader);
|
|
73
|
+
}
|
|
74
|
+
|
|
65
75
|
return this;
|
|
66
76
|
}
|
|
67
77
|
|
|
@@ -77,12 +87,17 @@ function Binh() {
|
|
|
77
87
|
};
|
|
78
88
|
|
|
79
89
|
this.api = function(api_path) {
|
|
80
|
-
this.api.value
|
|
90
|
+
this.api.value =`${rootpath}/${api_path}`;
|
|
81
91
|
return this;
|
|
82
92
|
};
|
|
83
93
|
|
|
84
94
|
this.port = function(port) {
|
|
85
|
-
|
|
95
|
+
if (typeof port === 'number') {
|
|
96
|
+
this.port.value = port;
|
|
97
|
+
}
|
|
98
|
+
else if (typeof port === 'string') {
|
|
99
|
+
this.port.value = config[port];
|
|
100
|
+
}
|
|
86
101
|
return this;
|
|
87
102
|
};
|
|
88
103
|
|
|
@@ -99,14 +114,12 @@ function Binh() {
|
|
|
99
114
|
|
|
100
115
|
function launch(callback) {
|
|
101
116
|
configloader.done(function(configs) {
|
|
102
|
-
|
|
117
|
+
new Server({
|
|
103
118
|
port: _this.port.value,
|
|
104
|
-
api: _this.api.value
|
|
119
|
+
api: new APIs(_this.api.value),
|
|
120
|
+
callback: callback,
|
|
121
|
+
configs: configs
|
|
105
122
|
});
|
|
106
|
-
|
|
107
|
-
if (callback instanceof Function) {
|
|
108
|
-
callback(server, configs);
|
|
109
|
-
}
|
|
110
123
|
});
|
|
111
124
|
}
|
|
112
125
|
|
package/src/server.js
CHANGED
|
@@ -10,6 +10,10 @@ function Server(options) {
|
|
|
10
10
|
|
|
11
11
|
server.listen(PORT, function() {
|
|
12
12
|
console.log(`[NODE] Server is listening on http://localhost:${PORT}/`);
|
|
13
|
+
|
|
14
|
+
if (options.callback instanceof Function) {
|
|
15
|
+
options.callback(server, options.configs);
|
|
16
|
+
}
|
|
13
17
|
});
|
|
14
18
|
|
|
15
19
|
return server;
|