samanbayaka 0.0.25 → 0.0.27
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/README.md +40 -23
- package/commit-hash.mjs +1 -1
- package/config/nats.yml +1 -2
- package/config/sysconfigs.yml +24 -0
- package/helper/file/esm-loading.mjs +24 -227
- package/helper/mol-built-in/AjvValidator.mjs +1 -14
- package/helper/mol-built-in/CustomLogger.mjs +13 -8
- package/helper/mol-built-in/HybridCacher.mjs +2 -2
- package/helper/utility/config-handler.mjs +45 -9
- package/helper/utility/error-handler.mjs +61 -47
- package/helper/utility/global-configs-validator.mjs +16 -5
- package/helper/utility/openapi-to-mol-params.mjs +71 -0
- package/helper/utility/telemetry.mjs +2 -6
- package/index.mjs +210 -163
- package/package.json +2 -1
- package/public/config-manager.html +0 -2
- package/public/scalar/index.html +27 -0
- package/services/about/index.mjs +2 -1
- package/services/gateway/index.mjs +43 -25
- package/services/kafkajs/index.mjs +3 -3
- package/services/system/config-manager.mjs +460 -0
- package/services/system/generate-openapi-docs.mjs +55 -0
- package/services/system/index.mjs +65 -155
- package/services/system/{scalar.mjs → scalar-ui.mjs} +3 -4
- package/services/system/system-manager.mjs +152 -0
- package/services/system/config/server.mjs +0 -36
- package/services/system/sw-stats-mw.mjs +0 -12
package/README.md
CHANGED
|
@@ -427,18 +427,32 @@ The `interMessageDelayMs` option introduces a delay between two consecutive mess
|
|
|
427
427
|
### Run the services
|
|
428
428
|
|
|
429
429
|
* development/testing
|
|
430
|
+
|
|
430
431
|
```bash
|
|
431
432
|
cd <your_path>/gateway
|
|
432
|
-
node index.mjs
|
|
433
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' node index.mjs
|
|
433
434
|
cd <your_path>/hello
|
|
434
|
-
node index.mjs
|
|
435
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' node index.mjs
|
|
435
436
|
```
|
|
437
|
+
or
|
|
438
|
+
```bash
|
|
439
|
+
rm -rf node_modules/samanbayaka/ &&cp -r ../../samanbayaka/. node_modules/samanbayaka/ && SBK_CONFIG_CRED='sbk sbk@123 12379' node index.mjs
|
|
440
|
+
|
|
441
|
+
pm2 stop "$(basename "$PWD")" && rm -rf node_modules/samanbayaka/ &&cp -r ../../samanbayaka/. node_modules/samanbayaka/ && SBK_CONFIG_CRED='sbk sbk@123 12379' pm2 start index.mjs --name "$(basename "$PWD")" --stop-exit-codes 1 --output /var/log/samanbayaka/sbk-out-${HOSTNAME}-$$.log --error /var/log/samanbayaka/sbk-err-${HOSTNAME}-$$.log
|
|
442
|
+
```
|
|
436
443
|
* production
|
|
437
444
|
```bash
|
|
438
445
|
cd <your_path>/gateway
|
|
439
|
-
node index.mjs >> /var/log/samanbayaka/sbk-${HOSTNAME}-$$.log 2>&1
|
|
446
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' node index.mjs >> /var/log/samanbayaka/sbk-${HOSTNAME}-$$.log 2>&1
|
|
447
|
+
cd <your_path>/hello
|
|
448
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' node index.mjs >> /var/log/samanbayaka/sbk-${HOSTNAME}-$$.log 2>&1
|
|
449
|
+
```
|
|
450
|
+
or
|
|
451
|
+
```bash
|
|
452
|
+
cd <your_path>/gateway
|
|
453
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' pm2 start index.mjs --name "$(basename "$PWD")" --stop-exit-codes 1 --output /var/log/samanbayaka/sbk-out-${HOSTNAME}-$$.log --error /var/log/samanbayaka/sbk-err-${HOSTNAME}-$$.log
|
|
440
454
|
cd <your_path>/hello
|
|
441
|
-
|
|
455
|
+
SBK_CONFIG_CRED='<your_etcs_user> <your_etcd_pass> <your_etcd_port>' pm2 start index.mjs --name "$(basename "$PWD")" --stop-exit-codes 1 --output /var/log/samanbayaka/sbk-out-${HOSTNAME}-$$.log --error /var/log/samanbayaka/sbk-err-${HOSTNAME}-$$.log
|
|
442
456
|
```
|
|
443
457
|
|
|
444
458
|
## Demo
|
|
@@ -470,7 +484,7 @@ cd etcd
|
|
|
470
484
|
touch docker-compose.yml
|
|
471
485
|
export ETCD_ROOT_PW=<your_root_pass>
|
|
472
486
|
export ETCD_CONFIG_ADMIN_PW=<your_config_admin_pass>
|
|
473
|
-
mkdir -p /usr/local/etc/<your_project_name>
|
|
487
|
+
sudo mkdir -p /usr/local/etc/<your_project_name>
|
|
474
488
|
```
|
|
475
489
|
|
|
476
490
|
Open `docker-compose.yml` and paste the following:
|
|
@@ -561,6 +575,7 @@ services:
|
|
|
561
575
|
|
|
562
576
|
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} role add default
|
|
563
577
|
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} role grant-permission --prefix=true default read /config/sbk/global/
|
|
578
|
+
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} role grant-permission --prefix=true default read /config/sbk/members/
|
|
564
579
|
|
|
565
580
|
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} role add edge
|
|
566
581
|
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} role grant-permission --prefix=true edge read /config/sbk/edge/
|
|
@@ -605,12 +620,21 @@ services:
|
|
|
605
620
|
etcdctl --endpoints=http://etcd:2379 --user=root:${ETCD_ROOT_PW} user grant-role msg msg
|
|
606
621
|
|
|
607
622
|
# ---------------------------------------------------------
|
|
623
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/sys/yaml/SYS_CONFIGS "$(cat /etcd-data/sysconfig.yml)" || true
|
|
624
|
+
|
|
625
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/REVISION "" || true
|
|
626
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/APP_ENV development || true
|
|
627
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/LOG_LEVEL warn || true
|
|
628
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/MAX_L1_TTL 120 || true
|
|
629
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/MAX_L2_TTL 600 || true
|
|
630
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/TELEMETRY false || true
|
|
608
631
|
|
|
609
632
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/broker/yaml/nats "$(cat /etcd-data/nats.yml)" || true
|
|
610
633
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/catcher/yaml/redis "$(cat /etcd-data/redis.yml)" || true
|
|
611
634
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/telemetry/yaml/openobserve "$(cat /etcd-data/openobserve.yml)" || true
|
|
612
635
|
|
|
613
636
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/edge/yaml/auth "$(cat /etcd-data/auth.yml)" || true
|
|
637
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/edge/asset/path public || true
|
|
614
638
|
|
|
615
639
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/bridge/yaml/kafka "$(cat /etcd-data/kafka.yml)" || true
|
|
616
640
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/bridge/yaml/mqtt "$(cat /etcd-data/mqtt.yml)" || true
|
|
@@ -629,26 +653,19 @@ services:
|
|
|
629
653
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/dbs/pg/yaml/route-otp "$(cat /etcd-data/route-otp.yml)" || true
|
|
630
654
|
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/dbs/pg/yaml/outlook "$(cat /etcd-data/outlook.yml)" || true
|
|
631
655
|
|
|
632
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
633
|
-
|
|
634
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
635
|
-
|
|
656
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/demo true || true
|
|
657
|
+
|
|
658
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/system true || true
|
|
659
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/system/envs/LOG_LEVEL error || true
|
|
660
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/system/envs/MAX_L1_TTL 0 || true
|
|
661
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/system/envs/MAX_L2_TTL 0 || true
|
|
636
662
|
|
|
637
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
638
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
639
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/MAX_L1_TTL 120 || true
|
|
640
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/envs/MAX_L2_TTL 600 || true
|
|
663
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/gateway true || true
|
|
664
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/gateway/envs/PORT 8765 || true
|
|
641
665
|
|
|
642
|
-
|
|
643
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
644
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/
|
|
645
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/system/envs/MAX_L1_TTL 0 || true
|
|
646
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/system/envs/MAX_L2_TTL 0 || true
|
|
647
|
-
|
|
648
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/gateway add || true
|
|
649
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/test add || true
|
|
650
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/sms add || true
|
|
651
|
-
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/global/members/email add || true
|
|
666
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/test true || true
|
|
667
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/sms add || true
|
|
668
|
+
etcdctl --endpoints=http://etcd:2379 --user=configadmin:${ETCD_CONFIG_ADMIN_PW} put /config/sbk/members/email add || true
|
|
652
669
|
|
|
653
670
|
#etcdctl --endpoints=http://etcd:2379 --user=<your_user>:<your_pass> get /config/sbk/ --prefix
|
|
654
671
|
#etcdctl --endpoints=http://etcd:2379 --user=<your_user>:<your_pass> get /config/sbk/global/app_env
|
package/commit-hash.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const COMMIT_HASH = '
|
|
1
|
+
export const COMMIT_HASH = '64e1dcc';
|
package/config/nats.yml
CHANGED
|
@@ -10,7 +10,6 @@ retryPolicy:
|
|
|
10
10
|
delay: 200
|
|
11
11
|
maxDelay: 3000
|
|
12
12
|
factor: 2
|
|
13
|
-
check: null # err => err && !!err.retryable
|
|
14
13
|
|
|
15
14
|
contextParamsCloning: false
|
|
16
15
|
|
|
@@ -89,7 +88,7 @@ replOptions:
|
|
|
89
88
|
metadata:
|
|
90
89
|
# region: eu-west1
|
|
91
90
|
|
|
92
|
-
logger:
|
|
91
|
+
logger: CustomLogger
|
|
93
92
|
logLevel: error
|
|
94
93
|
|
|
95
94
|
transporter:
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Expose the system API on the specified port
|
|
2
|
+
port: <your_sys_port>
|
|
3
|
+
|
|
4
|
+
# Project description
|
|
5
|
+
projDescription: "{#var#} is a microservices-based project built with moleculer and moleculer-web, designed for scalable, high-performance service communication and API management."
|
|
6
|
+
|
|
7
|
+
# Configuration brodcast interval in miliseconds
|
|
8
|
+
broadcastInterval: 600000 # 10 minitues
|
|
9
|
+
|
|
10
|
+
# The broadcast interval increases by the specified factor after each broadcast,
|
|
11
|
+
# i.e., interval = factor × interval.
|
|
12
|
+
factor: 1.1
|
|
13
|
+
|
|
14
|
+
# Configuration broadcast maximum duration in miliseconds
|
|
15
|
+
maxBroadcastDuration: 86400000 # 24 hours
|
|
16
|
+
|
|
17
|
+
# Expose the API to the domain
|
|
18
|
+
domain: sbk.wbsedcl.in
|
|
19
|
+
|
|
20
|
+
# Directory Path for API Assets
|
|
21
|
+
assetPath: public
|
|
22
|
+
|
|
23
|
+
# URL of Redpanda web client
|
|
24
|
+
redpandaWebUiPath: http://redpanda.web.ui:9080
|
|
@@ -7,21 +7,12 @@ import os from "os"
|
|
|
7
7
|
import { createRequire } from 'module'
|
|
8
8
|
|
|
9
9
|
import chokidar from "chokidar"
|
|
10
|
-
import { Etcd3 } from "etcd3"
|
|
11
|
-
import YAML from 'yaml'
|
|
12
10
|
|
|
13
11
|
const __filename = fileURLToPath(import.meta.url)
|
|
14
12
|
const __dirname = path.dirname(__filename)
|
|
15
13
|
const require = createRequire(import.meta.url)
|
|
16
14
|
|
|
17
15
|
|
|
18
|
-
/**
|
|
19
|
-
* Configuration path
|
|
20
|
-
* @type {string}
|
|
21
|
-
*/
|
|
22
|
-
const CONFIG_PATH = process.env.SBK_CONFIG_PATH // Environment variable of configuration path
|
|
23
|
-
|
|
24
|
-
|
|
25
16
|
/**
|
|
26
17
|
* Project absolute path
|
|
27
18
|
*/
|
|
@@ -38,97 +29,20 @@ export const DEMO_SERVICES_DIR = path.join(ABSOLUTE_PATH, 'services', 'demo')
|
|
|
38
29
|
/**
|
|
39
30
|
* Feature services absolute path
|
|
40
31
|
*/
|
|
41
|
-
const SERVICES_DIR = __dirname
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* User credentials for accessing and reading configurations from
|
|
46
|
-
* the configuration server.
|
|
47
|
-
* @type {string}
|
|
48
|
-
*/
|
|
49
|
-
const [configUser, ConfigPass, configPort] = (process.env.SBK_CONFIG_CRED || "ur:pa:2379").split(":")
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Configuration Reader Client Options
|
|
54
|
-
* @type {object}
|
|
55
|
-
*/
|
|
56
|
-
const client = new Etcd3({
|
|
57
|
-
hosts: `http://etcd:${configPort}`,
|
|
58
|
-
auth: {
|
|
59
|
-
username: configUser,
|
|
60
|
-
password: ConfigPass
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Getting configurations through dynamic loading of ESM modules
|
|
67
|
-
* ftom the path defined in environment variable
|
|
68
|
-
* @param {string}
|
|
69
|
-
* @return {object}
|
|
70
|
-
*/
|
|
71
|
-
export const getConfig = async (path) => {
|
|
72
|
-
try {
|
|
73
|
-
const yamlText = await client.get(`/config${path}`)
|
|
74
|
-
if ( path.startsWith("/yaml/") ) {
|
|
75
|
-
return Object.freeze( YAML.parse(yamlText?.toString()) )
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
return yamlText?.toString()
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
} catch (err) {
|
|
82
|
-
throw new Error(`Unable to read configuration : ${err.message}`)
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
export const watchConfig = async (path, callback) => {
|
|
88
|
-
const watcher = await client
|
|
89
|
-
.watch()
|
|
90
|
-
.prefix(`/config${path}`)
|
|
91
|
-
.create()
|
|
92
|
-
|
|
93
|
-
watcher.on("put", event => {
|
|
94
|
-
const evVal = path.startsWith("/yaml/")
|
|
95
|
-
? Object.freeze( YAML.parse(event.value.toString()) )
|
|
96
|
-
: event.value?.toString()
|
|
97
|
-
callback(
|
|
98
|
-
"put",
|
|
99
|
-
event.key?.toString(),
|
|
100
|
-
evVal
|
|
101
|
-
)
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
watcher.on("delete", event => {
|
|
105
|
-
callback(
|
|
106
|
-
"delete",
|
|
107
|
-
event.key.toString(),
|
|
108
|
-
null
|
|
109
|
-
)
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Getting server configuration.
|
|
118
|
-
* @type {string}
|
|
119
|
-
*/
|
|
120
|
-
const CONFIG = {assetPath: "public"} // await getConfig('server')
|
|
32
|
+
const SERVICES_DIR = /node_modules/.test(__dirname)
|
|
33
|
+
? __dirname.split('node_modules')[0]
|
|
34
|
+
: __dirname.replace("/helper/file","")
|
|
121
35
|
|
|
122
36
|
|
|
123
37
|
/**
|
|
124
38
|
* Assets path
|
|
125
39
|
*/
|
|
126
|
-
export const
|
|
127
|
-
|
|
40
|
+
export const absoluteAssetPath = (dirName) => {
|
|
41
|
+
return path
|
|
128
42
|
.join(
|
|
129
43
|
ABSOLUTE_PATH,
|
|
130
|
-
|
|
131
|
-
)
|
|
44
|
+
dirName,
|
|
45
|
+
)
|
|
132
46
|
}
|
|
133
47
|
|
|
134
48
|
|
|
@@ -176,7 +90,7 @@ const pkgMain = readPkg(ABSOLUTE_PATH)
|
|
|
176
90
|
* Create and object of name and version
|
|
177
91
|
* @type {Object}
|
|
178
92
|
*/
|
|
179
|
-
export const
|
|
93
|
+
export const pkgInfo = {
|
|
180
94
|
name: pkgMain.name.split('-')[0]
|
|
181
95
|
.split(" ")
|
|
182
96
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
@@ -186,139 +100,22 @@ export const pkgDtls = {
|
|
|
186
100
|
}
|
|
187
101
|
|
|
188
102
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
// * @param {RegExp} regx to filter only *.mjs files from service path
|
|
206
|
-
// * @return {void}
|
|
207
|
-
// */
|
|
208
|
-
// export const loadServices = async (broker) => {
|
|
209
|
-
// /**
|
|
210
|
-
// * Derive the service directory from the server configuration.
|
|
211
|
-
// * @type {string}
|
|
212
|
-
// */
|
|
213
|
-
// const SERVICES_DIR = path
|
|
214
|
-
// .join(
|
|
215
|
-
// ABSOLUTE_PATH,
|
|
216
|
-
// CONFIG.servicesDir,
|
|
217
|
-
// serviceName
|
|
218
|
-
// )
|
|
219
|
-
|
|
220
|
-
// const filePath = path.join(SERVICES_DIR, CONFIG.serviceMain)
|
|
221
|
-
|
|
222
|
-
// /**
|
|
223
|
-
// * Dynamic import with cache busting
|
|
224
|
-
// * @type {object}
|
|
225
|
-
// */
|
|
226
|
-
// const module = await import(`${pathToFileURL(filePath)}?t=${Date.now()}`)
|
|
227
|
-
// const schema = module.default || module
|
|
228
|
-
|
|
229
|
-
// if (!schema || typeof schema !== "object" || !schema.name || schema.name != serviceName) {
|
|
230
|
-
// const regex = new RegExp(`(\\/${CONFIG.servicesDir}\\/.*)$`)
|
|
231
|
-
// const rfPath = pathToFileURL(filePath).href.match(regex)[1]
|
|
232
|
-
// await broker.stop()
|
|
233
|
-
// broker.logger.fatal('SBKERR ❖', `Service start failed: invalid service name or schema in ${rfPath}`)
|
|
234
|
-
// process.exit(0)
|
|
235
|
-
// }
|
|
236
|
-
|
|
237
|
-
// broker.createService(schema)
|
|
238
|
-
// broker.logger.info('SBKMSG ❖', `Hot-realod: ${isHotReloadEnabled ? "ON" : "OFF"}`)
|
|
239
|
-
|
|
240
|
-
// const watcher = chokidar.watch(SERVICES_DIR, {
|
|
241
|
-
// ignoreInitial: true,
|
|
242
|
-
// awaitWriteFinish: {
|
|
243
|
-
// stabilityThreshold: 200, // wait 200ms after last change
|
|
244
|
-
// pollInterval: 50
|
|
245
|
-
// },
|
|
246
|
-
// ignored: [
|
|
247
|
-
// /(^|[\/\\])\../,
|
|
248
|
-
// /~$/,
|
|
249
|
-
// /\.swp$/,
|
|
250
|
-
// /\.tmp$/
|
|
251
|
-
// ]
|
|
252
|
-
// })
|
|
253
|
-
|
|
254
|
-
// const handleWatchFile = async(wfPath, isUnlink) => {
|
|
255
|
-
// const regex = new RegExp(`(\\/${CONFIG.servicesDir}\\/.*)$`)
|
|
256
|
-
// const rfPath = pathToFileURL(wfPath).href.match(regex)[1]
|
|
257
|
-
// /**
|
|
258
|
-
// * Hot Reloading of moleculer services from service path
|
|
259
|
-
// */
|
|
260
|
-
// if(isHotReloadEnabled){
|
|
261
|
-
// if(path.basename(filePath) === path.basename(wfPath)){
|
|
262
|
-
// const oldServiceFullName = (broker.services.find(s => s.name === serviceName))?.fullName
|
|
263
|
-
// const oldService = broker.getLocalService(oldServiceFullName)
|
|
264
|
-
|
|
265
|
-
// if (!isUnlink) {
|
|
266
|
-
// try {
|
|
267
|
-
// /**
|
|
268
|
-
// * Dynamic import with cache busting
|
|
269
|
-
// */
|
|
270
|
-
// const serviceModule = await import(`${pathToFileURL(filePath)}?t=${Date.now()}`)
|
|
271
|
-
// const schema = serviceModule.default || serviceModule
|
|
272
|
-
|
|
273
|
-
// if (!schema || typeof schema !== "object" || !schema.name || schema.name != serviceName) {
|
|
274
|
-
// broker.logger.error('SBKERR ❖', `Hot-reloading terminated due to an invalid service name or schema for ${filePath}`)
|
|
275
|
-
// return
|
|
276
|
-
// }
|
|
277
|
-
|
|
278
|
-
// /**
|
|
279
|
-
// * Remove the already loaded service if it exists.
|
|
280
|
-
// */
|
|
281
|
-
// if (oldService) {
|
|
282
|
-
// await broker.destroyService(oldService)
|
|
283
|
-
// }
|
|
284
|
-
|
|
285
|
-
// /**
|
|
286
|
-
// * Create new service
|
|
287
|
-
// */
|
|
288
|
-
// broker.createService(schema)
|
|
289
|
-
// const newServiceFullName = schema.version === undefined
|
|
290
|
-
// ? schema.name
|
|
291
|
-
// : `${schema.version}.${schema.name}`
|
|
292
|
-
// broker.logger.info('SBKMSG ❖', `Service '${newServiceFullName}' reloaded.`)
|
|
293
|
-
// } catch (err) {
|
|
294
|
-
// broker.logger.error('SBKERR ❖', `Hot-reloading of service '${newServiceFullName}' failed..`, err)
|
|
295
|
-
// }
|
|
296
|
-
// } else if (event === "unlink") {
|
|
297
|
-
// /**
|
|
298
|
-
// * Remove the already loaded service if it exists.
|
|
299
|
-
// */
|
|
300
|
-
// if (oldService) {
|
|
301
|
-
// await broker.destroyService(oldService)
|
|
302
|
-
// broker.logger.info('SBKMSG ❖', `Service '${newServiceFullName}' removed.`)
|
|
303
|
-
// }
|
|
304
|
-
// }
|
|
305
|
-
// }
|
|
306
|
-
// else {
|
|
307
|
-
// broker.logger.warn('SBKWRN ❖',`Changes detected in the file "${rfPath}". Restart required to apply updates.`)
|
|
308
|
-
// }
|
|
309
|
-
|
|
310
|
-
// }
|
|
311
|
-
// else {
|
|
312
|
-
// broker.logger.warn('SBKWRN ❖',`Changes detected in the file "${rfPath}". Restart required to apply updates.`)
|
|
313
|
-
// }
|
|
314
|
-
// }
|
|
315
|
-
|
|
316
|
-
// watcher.on("add", (fp) => handleWatchFile(fp, false))
|
|
317
|
-
// watcher.on("change", (fp) => handleWatchFile(fp, false))
|
|
318
|
-
// watcher.on("unlink", (fp) => handleWatchFile(fp, true))
|
|
319
|
-
// }
|
|
320
|
-
|
|
321
|
-
|
|
103
|
+
/**
|
|
104
|
+
* File chage watcher in the service directory
|
|
105
|
+
*/
|
|
106
|
+
export const dirWatcher = chokidar.watch(SERVICES_DIR, {
|
|
107
|
+
ignoreInitial: true,
|
|
108
|
+
awaitWriteFinish: {
|
|
109
|
+
stabilityThreshold: 2000, // wait 2000ms after last change
|
|
110
|
+
pollInterval: 50
|
|
111
|
+
},
|
|
112
|
+
ignored: [
|
|
113
|
+
/(^|[\/\\])\../,
|
|
114
|
+
/~$/,
|
|
115
|
+
/\.swp$/,
|
|
116
|
+
/\.tmp$/
|
|
117
|
+
]
|
|
118
|
+
})
|
|
322
119
|
|
|
323
120
|
|
|
324
121
|
|
|
@@ -65,12 +65,17 @@ const pinoOptions = {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
|
|
68
|
-
export default {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
options:
|
|
68
|
+
export default (opts) => {
|
|
69
|
+
const { logger, logLevel } = opts
|
|
70
|
+
return logger == "CustomLogger"
|
|
71
|
+
? {
|
|
72
|
+
type: "Pino",
|
|
73
|
+
options: {
|
|
74
|
+
level: (logLevel || "info").toLowerCase(),
|
|
75
|
+
pino: {
|
|
76
|
+
options: pinoOptions
|
|
77
|
+
}
|
|
78
|
+
}
|
|
74
79
|
}
|
|
75
|
-
|
|
76
|
-
}
|
|
80
|
+
: "Console"
|
|
81
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Cachers } from "moleculer"
|
|
2
2
|
|
|
3
|
-
const MAX_L1_TTL =
|
|
4
|
-
const MAX_L2_TTL =
|
|
3
|
+
const MAX_L1_TTL = 600 // 10 minutes in seconds
|
|
4
|
+
const MAX_L2_TTL = 86400 // 24 hours in seconds
|
|
5
5
|
|
|
6
6
|
export default class HybridCacher extends Cachers.Base {
|
|
7
7
|
constructor(opts = {}) {
|
|
@@ -15,7 +15,7 @@ export const etcdCred = errHdl.validateConfigCred(process.env.SBK_CONFIG_CRED)
|
|
|
15
15
|
* Configuration Reader Client Options
|
|
16
16
|
* @type {object}
|
|
17
17
|
*/
|
|
18
|
-
const client = new Etcd3({
|
|
18
|
+
export const client = new Etcd3({
|
|
19
19
|
hosts: `http://etcd:${etcdCred[2]}`,
|
|
20
20
|
auth: {
|
|
21
21
|
username: etcdCred[0],
|
|
@@ -24,19 +24,18 @@ const client = new Etcd3({
|
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Getting configurations
|
|
28
|
-
* ftom the path defined in environment variable
|
|
27
|
+
* Getting configurations against a Etcd key
|
|
29
28
|
* @param {string}
|
|
30
|
-
* @return {object}
|
|
29
|
+
* @return {object | string}
|
|
31
30
|
*/
|
|
32
|
-
export const getConfigs = async (path) => {
|
|
31
|
+
export const getConfigs = async (path, isYaml = false) => {
|
|
33
32
|
try {
|
|
34
|
-
const yamlText = await client.get(path)
|
|
33
|
+
const yamlText = (await client.get(path))?.toString()
|
|
35
34
|
if ( path.split('/').includes('yaml') ) {
|
|
36
|
-
return Object.freeze( YAML.parse(yamlText
|
|
35
|
+
return isYaml ? yamlText : Object.freeze( YAML.parse( yamlText ) )
|
|
37
36
|
}
|
|
38
37
|
else {
|
|
39
|
-
return yamlText
|
|
38
|
+
return yamlText
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
} catch (err) {
|
|
@@ -44,7 +43,12 @@ export const getConfigs = async (path) => {
|
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Getting all keys having prefix path
|
|
48
|
+
* @param {string} path
|
|
49
|
+
* @param {Boolean} fullPath
|
|
50
|
+
* @return {array}
|
|
51
|
+
*/
|
|
48
52
|
export const getConfigKeys = async (path, fullPath = false) => {
|
|
49
53
|
try {
|
|
50
54
|
const entries = await client.getAll()
|
|
@@ -60,6 +64,35 @@ export const getConfigKeys = async (path, fullPath = false) => {
|
|
|
60
64
|
}
|
|
61
65
|
}
|
|
62
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Create Etcd key with value
|
|
69
|
+
* @param {string} path
|
|
70
|
+
* @param {string} data
|
|
71
|
+
*/
|
|
72
|
+
export const putConfig = async(path, data) => {
|
|
73
|
+
await client.put(path)
|
|
74
|
+
.value(data)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Delete Etcd key
|
|
79
|
+
* @param {string} path
|
|
80
|
+
* @return {string}
|
|
81
|
+
*/
|
|
82
|
+
export const delConfig = async(path) => {
|
|
83
|
+
const deletedData = await client
|
|
84
|
+
.delete()
|
|
85
|
+
.key(path)
|
|
86
|
+
.getPrevious()
|
|
87
|
+
|
|
88
|
+
return deletedData.map(item => ({
|
|
89
|
+
...item,
|
|
90
|
+
key: Buffer.from(item.key).toString("utf8"),
|
|
91
|
+
value: Buffer.from(item.value).toString("utf8"),
|
|
92
|
+
}))
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
63
96
|
|
|
64
97
|
export const getConfigAll = async (path, fullPath = false) => {
|
|
65
98
|
try {
|
|
@@ -107,6 +140,7 @@ export const watchConfigs = async (path, callback) => {
|
|
|
107
140
|
null
|
|
108
141
|
)
|
|
109
142
|
})
|
|
143
|
+
return watcher
|
|
110
144
|
}
|
|
111
145
|
catch (err) {
|
|
112
146
|
throw new Error(`Unable to watck configuration: ${err.message}`)
|
|
@@ -114,3 +148,5 @@ export const watchConfigs = async (path, callback) => {
|
|
|
114
148
|
|
|
115
149
|
}
|
|
116
150
|
|
|
151
|
+
|
|
152
|
+
|