samanbayaka 0.0.12 → 0.0.13
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 +76 -20
- package/commit-hash.mjs +1 -1
- package/helper/logger/pino.mjs +67 -23
- package/helper/utility/error-handler.mjs +7 -7
- package/index.mjs +3 -6
- package/package.json +1 -1
- package/bash/sbk.sh +0 -8
package/README.md
CHANGED
|
@@ -39,10 +39,23 @@ nvm use 22
|
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
#### Environment variables
|
|
42
|
-
To configure all required environment variables,
|
|
42
|
+
To configure all required environment variables, create a Bash file:
|
|
43
|
+
```bash
|
|
44
|
+
sudo nano /etc/logrotate.d/sbk.sh
|
|
45
|
+
```
|
|
46
|
+
Paste this:
|
|
47
|
+
```bash
|
|
48
|
+
##Configurations files path
|
|
49
|
+
export SBK_CONFIG_PATH=/usr/local/etc/<your_folder>
|
|
50
|
+
|
|
51
|
+
##web server port 8760-8769
|
|
52
|
+
export SBK_PORT=8765
|
|
53
|
+
|
|
54
|
+
##Log level allowed values are fatal | error | warn | info | debug | trace
|
|
55
|
+
export SBK_LOG_LEVEL=debug
|
|
56
|
+
```
|
|
57
|
+
Set environment variables in the current Bash session.
|
|
43
58
|
```bash
|
|
44
|
-
pwd
|
|
45
|
-
sudo cp bash/sbk.sh /etc/profile.d/
|
|
46
59
|
source /etc/profile.d/sbk.sh
|
|
47
60
|
```
|
|
48
61
|
Optionally, you may verify the set environment variables.
|
|
@@ -53,14 +66,41 @@ echo $SBK_PORT
|
|
|
53
66
|
Create a directory to store the configuration files, then copy all configuration files from the current project path into this directory and update them as needed. Before copying, ensure that your current working directory is the project directory.
|
|
54
67
|
```bash
|
|
55
68
|
pwd
|
|
56
|
-
sudo mkdir -p /usr/local/etc/<
|
|
57
|
-
sudo cp config/*.* /usr/local/etc/<
|
|
69
|
+
sudo mkdir -p /usr/local/etc/<your_folder>
|
|
70
|
+
sudo cp config/*.* /usr/local/etc/<your_folder>
|
|
71
|
+
```
|
|
72
|
+
#### Log files
|
|
73
|
+
For production, it is recommended to create a new directory named "samanbayaka" under /var/log to store logs.
|
|
74
|
+
```bash
|
|
75
|
+
sudo mkdir -p /var/log/samanbayaka
|
|
76
|
+
sudo chown -R $USER:$(id -gn) /var/log/samanbayaka
|
|
77
|
+
sudo chmod 755 /var/log/samanbayaka
|
|
78
|
+
```
|
|
79
|
+
Empty your log files if they become very large, as follows:
|
|
80
|
+
```bash
|
|
81
|
+
truncate -s 0 /var/log/samanbayaka/sbk-*.log
|
|
82
|
+
```
|
|
83
|
+
#### Setup logrotate (optional)
|
|
84
|
+
Create config file:
|
|
85
|
+
```bash
|
|
86
|
+
sudo nano /etc/logrotate.d/sbk
|
|
87
|
+
```
|
|
88
|
+
Paste this:
|
|
89
|
+
```bash
|
|
90
|
+
/var/log/samanbayaka/sbk-*.log {
|
|
91
|
+
daily
|
|
92
|
+
rotate 14
|
|
93
|
+
compress
|
|
94
|
+
delaycompress
|
|
95
|
+
missingok
|
|
96
|
+
notifempty
|
|
97
|
+
copytruncate
|
|
98
|
+
create 0640 node node
|
|
99
|
+
}
|
|
58
100
|
```
|
|
59
|
-
|
|
60
|
-
|
|
61
101
|
# Usage
|
|
62
|
-
#### Create
|
|
63
|
-
The
|
|
102
|
+
#### Create gateway service
|
|
103
|
+
The Gateway is a critical service that exposes all endpoints as REST APIs. At least one Gateway service must be running to make the REST APIs accessible.
|
|
64
104
|
```bash
|
|
65
105
|
mkdir gateway
|
|
66
106
|
cd gateway
|
|
@@ -69,18 +109,21 @@ pnpm install samanbayaka
|
|
|
69
109
|
touch index.mjs
|
|
70
110
|
```
|
|
71
111
|
|
|
72
|
-
Open
|
|
112
|
+
Open index.mjs and paste the following:
|
|
73
113
|
```bash
|
|
74
114
|
import sbk from "samanbayaka"
|
|
75
115
|
await sbk.loadGatewayService()
|
|
76
116
|
```
|
|
77
|
-
|
|
117
|
+
#### Create feature service
|
|
78
118
|
```bash
|
|
79
|
-
|
|
119
|
+
mkdir <your_service_name>
|
|
120
|
+
cd <your_service_name>
|
|
121
|
+
npm init -y
|
|
122
|
+
pnpm install samanbayaka
|
|
123
|
+
touch index.mjs
|
|
80
124
|
```
|
|
81
125
|
|
|
82
|
-
|
|
83
|
-
#### Create your endpoints as a feature service, such as hello
|
|
126
|
+
A feature service, such as "hello", can be created as follows:
|
|
84
127
|
```bash
|
|
85
128
|
mkdir hello
|
|
86
129
|
cd hello
|
|
@@ -89,7 +132,7 @@ pnpm install samanbayaka
|
|
|
89
132
|
touch index.mjs
|
|
90
133
|
```
|
|
91
134
|
|
|
92
|
-
Open
|
|
135
|
+
Open index.mjs and paste the following:
|
|
93
136
|
```bash
|
|
94
137
|
import sbk from "samanbayaka"
|
|
95
138
|
await sbk.loadFeatureService.mainBus({
|
|
@@ -109,9 +152,9 @@ await sbk.loadFeatureService.mainBus({
|
|
|
109
152
|
cache: {
|
|
110
153
|
|
|
111
154
|
/**
|
|
112
|
-
* These cache entries will be expired after
|
|
155
|
+
* These cache entries will be expired after 30 seconds.
|
|
113
156
|
*/
|
|
114
|
-
ttl:
|
|
157
|
+
ttl: 30
|
|
115
158
|
},
|
|
116
159
|
handler(ctx){
|
|
117
160
|
return "Hello Samanbayaka"
|
|
@@ -123,17 +166,30 @@ await sbk.loadFeatureService.mainBus({
|
|
|
123
166
|
return `Welcome, ${ctx.params.query?.name || "Guest"} - ${ctx.broker.nodeID}`
|
|
124
167
|
}
|
|
125
168
|
},
|
|
126
|
-
createUser
|
|
127
169
|
},
|
|
128
170
|
})
|
|
129
171
|
|
|
130
172
|
```
|
|
131
|
-
|
|
173
|
+
|
|
174
|
+
#### Run the services
|
|
175
|
+
|
|
176
|
+
* development/testing
|
|
132
177
|
```bash
|
|
178
|
+
cd <your_path>/gateway
|
|
179
|
+
node index.mjs
|
|
180
|
+
cd <your_path>/hello
|
|
133
181
|
node index.mjs
|
|
134
182
|
```
|
|
183
|
+
* production
|
|
184
|
+
```bash
|
|
185
|
+
cd <your_path>/gateway
|
|
186
|
+
node index.mjs >> /var/log/samanbayaka/sbk-${HOSTNAME}-$$.log 2>&1
|
|
187
|
+
cd <your_path>/hello
|
|
188
|
+
node index.mjs >> /var/log/samanbayaka/sbk-${HOSTNAME}-$$.log 2>&1
|
|
189
|
+
```
|
|
190
|
+
|
|
135
191
|
# Demo
|
|
136
|
-
You can also run the demo service to better understand how the
|
|
192
|
+
You can also run the demo service to better understand how microservices work in the Moleculer ecosystem and how REST APIs are exposed.
|
|
137
193
|
```bash
|
|
138
194
|
mkdir demo
|
|
139
195
|
cd demo
|
package/commit-hash.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const COMMIT_HASH = '
|
|
1
|
+
export const COMMIT_HASH = '289c98a';
|
package/helper/logger/pino.mjs
CHANGED
|
@@ -1,38 +1,82 @@
|
|
|
1
1
|
import pino from "pino"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
// level: (process.env.SBK_LOG_LEVEL || "info").toLowerCase(),
|
|
5
|
-
// // base: {
|
|
6
|
-
// // service: "gateway",
|
|
7
|
-
// // env: process.env.NODE_ENV || "prod"
|
|
8
|
-
// // }
|
|
9
|
-
// });
|
|
3
|
+
import { serviceDtls } from '#hFil/esm-loading.mjs'
|
|
10
4
|
|
|
11
5
|
|
|
6
|
+
const pinoOptions = {
|
|
7
|
+
level: (process.env.SBK_LOG_LEVEL || "info").toLowerCase(),
|
|
12
8
|
|
|
9
|
+
/**
|
|
10
|
+
* ISO timestamps (better for Loki/Grafana)
|
|
11
|
+
*/
|
|
12
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* base fields = Loki labels candidates
|
|
16
|
+
*/
|
|
17
|
+
base: null,
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Rename default fields (optional but cleaner for Loki)
|
|
21
|
+
* @type {Object}
|
|
22
|
+
*/
|
|
23
|
+
// formatters: {
|
|
24
|
+
// level(label) {
|
|
25
|
+
// return { level: label } // instead of numeric
|
|
26
|
+
// }
|
|
27
|
+
// },
|
|
28
|
+
|
|
29
|
+
serializers: {
|
|
30
|
+
err: pino.stdSerializers.err
|
|
17
31
|
},
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
|
|
33
|
+
// messageKey: "message",
|
|
34
|
+
nestedKey: 'data',
|
|
35
|
+
// msgPrefix: " ❖❖ SBK ❖❖ ",
|
|
36
|
+
hooks: {
|
|
37
|
+
logMethod(args, method, level) {
|
|
38
|
+
let mergedObj = {}
|
|
39
|
+
let messages = []
|
|
40
|
+
|
|
41
|
+
for (const arg of args) {
|
|
42
|
+
if (typeof arg === 'string') {
|
|
43
|
+
messages.push(arg)
|
|
44
|
+
} else if (arg && typeof arg === 'object') {
|
|
45
|
+
mergedObj = { ...mergedObj, ...arg }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Concatinate all string seperte with white space
|
|
51
|
+
*/
|
|
52
|
+
// if( messages.length > 0 ) messages.unshift(" ❖❖ SBK ❖❖ ")
|
|
53
|
+
const finalMsg = messages.join(' ')
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Rebuild args into standard Pino format
|
|
58
|
+
*/
|
|
59
|
+
if (finalMsg && Object.keys(mergedObj).length > 0) {
|
|
60
|
+
args = [{...mergedObj, ...{tag: "SBK", message: finalMsg}}]
|
|
61
|
+
} else if (finalMsg) {
|
|
62
|
+
args = [{tag: "MOL", message: finalMsg}]
|
|
63
|
+
} else {
|
|
64
|
+
args = [{...mergedObj, ...{tag: "SBK"}}]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
method.apply(this, args)
|
|
28
68
|
}
|
|
29
|
-
}
|
|
30
|
-
|
|
69
|
+
},
|
|
70
|
+
}
|
|
31
71
|
|
|
32
72
|
|
|
33
73
|
export default Object.freeze({
|
|
34
74
|
type: "Pino",
|
|
35
|
-
options:
|
|
75
|
+
options: {
|
|
76
|
+
pino: {
|
|
77
|
+
options: pinoOptions
|
|
78
|
+
}
|
|
79
|
+
}
|
|
36
80
|
})
|
|
37
81
|
|
|
38
82
|
|
|
@@ -158,18 +158,18 @@ export const validateServiceName = (name, pkgName) => {
|
|
|
158
158
|
*/
|
|
159
159
|
export const shutdownHdl = async (broker, signal, err=null) => {
|
|
160
160
|
if(err){
|
|
161
|
-
broker.logger.error(
|
|
161
|
+
broker.logger.error({err}, `${signal}: `)
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
broker.logger.info(
|
|
164
|
+
broker.logger.info({message: `Received ${signal}, Stopping broker...`})
|
|
165
165
|
|
|
166
166
|
try {
|
|
167
167
|
await broker.stop()
|
|
168
|
-
broker.logger.info(
|
|
168
|
+
broker.logger.info({message: "Broker stopped gracefully."})
|
|
169
169
|
|
|
170
170
|
process.exit(0)
|
|
171
171
|
} catch (err) {
|
|
172
|
-
broker.logger.error(
|
|
172
|
+
broker.logger.error({err}, "Error during broker shutdown:")
|
|
173
173
|
process.exit(1)
|
|
174
174
|
}
|
|
175
175
|
}
|
|
@@ -208,10 +208,10 @@ export const SbkErrorMiddleware = {
|
|
|
208
208
|
const timestamp = new Date().toISOString()
|
|
209
209
|
|
|
210
210
|
if(err.code){
|
|
211
|
-
ctx.broker.logger.error(
|
|
211
|
+
ctx.broker.logger.error({traceId, type, instance, detail, stack: ""})
|
|
212
212
|
}
|
|
213
213
|
else{
|
|
214
|
-
ctx.broker.logger.error(
|
|
214
|
+
ctx.broker.logger.error({traceId, type, instance, detail, stack})
|
|
215
215
|
}
|
|
216
216
|
|
|
217
217
|
|
|
@@ -269,7 +269,7 @@ const sbkValidateObj = async() => {
|
|
|
269
269
|
})
|
|
270
270
|
}
|
|
271
271
|
catch(err){
|
|
272
|
-
console.error("\x1b[31m%s\x1b[0m", `SBKERR
|
|
272
|
+
console.error("\x1b[31m%s\x1b[0m", ` ❖❖ SBKERR ❖❖ ${err.message}`)
|
|
273
273
|
process.exit(1)
|
|
274
274
|
}
|
|
275
275
|
}
|
package/index.mjs
CHANGED
|
@@ -39,20 +39,17 @@ const isReplEnabled = (
|
|
|
39
39
|
* @type {ServiceBroker}
|
|
40
40
|
*/
|
|
41
41
|
const broker = new ServiceBroker({
|
|
42
|
+
...BROKER_CONFIG,
|
|
42
43
|
...{
|
|
43
|
-
|
|
44
|
-
namespace: initSbk.namespaceTxt
|
|
45
|
-
},
|
|
46
|
-
...{
|
|
44
|
+
namespace: initSbk.namespaceTxt,
|
|
47
45
|
nodeID: serviceDtls.id,
|
|
48
46
|
transporter: NATS_CONFIG,
|
|
49
47
|
validator: new AjvValidator(),
|
|
50
|
-
logger:
|
|
48
|
+
logger: pinoLogger,
|
|
51
49
|
},
|
|
52
50
|
middlewares: [SbkErrorMiddleware],
|
|
53
51
|
})
|
|
54
52
|
|
|
55
|
-
|
|
56
53
|
/**
|
|
57
54
|
* Listen for force stop signals
|
|
58
55
|
*/
|
package/package.json
CHANGED
package/bash/sbk.sh
DELETED