binhend 1.0.0 → 1.0.2

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.
@@ -0,0 +1,4 @@
1
+
2
+ module.exports = function(loader) {
3
+ loader.object({ PORT: 1400 });
4
+ };
package/example/index.js CHANGED
@@ -4,13 +4,11 @@ const { Binh } = require('../src/binh');
4
4
 
5
5
  new Binh()
6
6
  .id()
7
- .rootpath(__dirname)
8
- .config(function(loader) {
9
- loader.object({ PORT: 1400 });
10
- })
7
+ .global('config', binh.config)
8
+ .config('config')
11
9
  .api('routes')
12
- .cluster.fork(-100)
13
- .port(binh.config('PORT'))
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.0",
3
+ "version": "1.0.2",
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
@@ -44,13 +44,15 @@ function mapAPIs(dirpath, router) {
44
44
  }
45
45
 
46
46
  function APIs(rootpath) {
47
+ if (rootpath == undefined) return;
48
+
47
49
  const router = express.Router();
48
50
  router.use(express.urlencoded({ extended: false }));
49
51
  router.use(express.json());
50
52
 
51
- rootpath = rootpath || join(__dirname, 'routes');
52
-
53
- mapAPIs(rootpath, router);
53
+ setTimeout(function() {
54
+ mapAPIs(rootpath, router);
55
+ });
54
56
 
55
57
  return router;
56
58
  }
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.config = function(callback) {
61
- if (callback instanceof Function) {
62
- loadConfigs = callback;
63
- callback(configloader);
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 = new APIs(`${rootpath}/${api_path}`);
90
+ this.api.value =`${rootpath}/${api_path}`;
81
91
  return this;
82
92
  };
83
93
 
84
94
  this.port = function(port) {
85
- this.port.value = port;
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
- var server = new Server({
117
+ new Server({
103
118
  port: _this.port.value,
104
- api: _this.api.value
119
+ api: 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
@@ -4,12 +4,16 @@ function Server(options) {
4
4
  const server = express();
5
5
  const PORT = options.port || 1300;
6
6
 
7
- if (options.api) {
7
+ if (options.api instanceof Function) {
8
8
  server.use(options.api);
9
9
  }
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;