binhend 1.0.1 → 1.0.3

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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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
@@ -44,12 +44,12 @@ 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
53
  setTimeout(function() {
54
54
  mapAPIs(rootpath, router);
55
55
  });
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
- if (loadConfigs instanceof Function) {
36
- var loader = new ConfigLoader(config);
37
- loadConfigs(loader);
38
- loader.done(resolve);
39
- }
40
- else resolve(config);
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
- module(configloader);
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) {
@@ -116,7 +136,7 @@ function Binh() {
116
136
  configloader.done(function(configs) {
117
137
  new Server({
118
138
  port: _this.port.value,
119
- api: new APIs(_this.api.value),
139
+ api: APIs(_this.api.value),
120
140
  callback: callback,
121
141
  configs: configs
122
142
  });
package/src/server.js CHANGED
@@ -4,7 +4,7 @@ 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