@sera4/essentia 1.0.10 → 1.0.12

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 CHANGED
@@ -5,4 +5,5 @@ A collection of small modules providing essential utilities for TWS services:
5
5
  * [Sequelize Paginator](paginator/README.md)
6
6
  * [Sequelize HAL Decorator](hal/README.md)
7
7
  * A logging tool
8
- * A small utility to dump the last commit of the project
8
+ * An HTTP response tool for consistenty formatting error responses
9
+ * A small utility to dump the last commit of the project
@@ -0,0 +1,121 @@
1
+ "use strict"
2
+
3
+ const pathHelper = require("path");
4
+ const logger = require("../logger/s4-logger");
5
+
6
+ class HealthCheckError extends Error {
7
+ constructor(m) {
8
+ super(m);
9
+ }
10
+ }
11
+
12
+ /* Health Checking router + controller for use in all of our modules
13
+ * Example use: Place the following code in your routes file
14
+ *
15
+ * const hc = require("@sera4/essentia").healthCheck;
16
+ * const router = require('express').Router();
17
+ *
18
+ * ....
19
+ * app.use(hc.defaultPath(serverConfigs.api_prefix), hc.routes(router));
20
+ *
21
+ */
22
+
23
+ class HealthCheck {
24
+ constructor(appendData = {}) {
25
+ this.defaultResponse = {
26
+ ...
27
+ appendData,
28
+ status: 200,
29
+ errors: {}
30
+ };
31
+
32
+ this.defaultPathRoute = `health_check\(.*\)?`
33
+ this.lastCallID = null;
34
+
35
+ this.healthChecks = [];
36
+ }
37
+
38
+ /* call this with an async function, must support wait capability
39
+ *
40
+ * const dbCheck = async () => {
41
+ * return { name: "db", "result": "success", critical: false };
42
+ * }
43
+ */
44
+ addCheck(fcn) {
45
+ this.healthChecks.push(fcn);
46
+ }
47
+
48
+ defaultPath(prefix = "") {
49
+ return `${prefix}/${this.defaultPathRoute}`
50
+ }
51
+
52
+ routes(router) {
53
+ if (!router) {
54
+ throw new HealthCheckError("missing_express_router");
55
+ }
56
+
57
+ /* definition of all routes, rooted at defaultPath */
58
+ router.get('/', (req, res) => {
59
+ this.checkHealth(req, res)
60
+ });
61
+
62
+ return router
63
+ }
64
+
65
+ /* performs health checks and returns 200 if true and 500 if not */
66
+ async checkHealth(req, res) {
67
+ const checkFormat = pathHelper.extname(req.baseUrl);
68
+
69
+ try {
70
+ const result = await Promise.all(this.healthChecks.map(async healthCallback => {
71
+ return await healthCallback();
72
+ }));
73
+
74
+ //let result = (result.reduce((e) => e.success, 200))
75
+ let callId = (Math.random()*0xFFFFFF<<0).toString(16);
76
+ this.lastCallId = callId;
77
+
78
+ let status = this.defaultResponse.status;
79
+
80
+ result.some(e => {
81
+ if (e.result.match(/fail/)) {
82
+ status = 500;
83
+ return true; // break
84
+ }
85
+ })
86
+
87
+ // set this in the header no matter what
88
+ res.setHeader('check', callId)
89
+
90
+ if (checkFormat === ".json") {
91
+ if (status === 200) {
92
+ res.status(status).json({
93
+ "check": callId,
94
+ "healthy":true,
95
+ "message":"success"
96
+ });
97
+ } else {
98
+ res.status(status).json({
99
+ "check": callId,
100
+ "healthy": false,
101
+ "message": "fail",
102
+ "services": [result]
103
+ });
104
+ }
105
+ } else {
106
+ let status_message;
107
+ if (status === 200) {
108
+ status_message = "success"
109
+ } else {
110
+ status_message = "fail"
111
+ }
112
+
113
+ res.status(status).send(status_message);
114
+ }
115
+ } catch(error) {
116
+ res.status(500).send();
117
+ }
118
+ }
119
+ }
120
+
121
+ module.exports = HealthCheck;
package/index.js CHANGED
@@ -1,9 +1,12 @@
1
+ 'use strict';
2
+
1
3
  module.exports = {
2
4
  paginator : require("./paginator/s4-pagination"),
3
5
  sqlPaginator : require("./paginator/sql-pagination"),
4
6
  logger : require("./logger/s4-logger"),
5
7
  lastCommit : require("./last_commit"),
6
8
  halDecorator : require("./hal"),
7
- formatter : require("./formatter")
9
+ formatter : require("./formatter"),
10
+ healthCheck: require("./health")
8
11
  }
9
12
  //console.log(module.exports.formatter.formatError({ status: 200, error: ["error1"] }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,6 +16,7 @@
16
16
  "license": "UNLICENSED",
17
17
  "devDependencies": {
18
18
  "chai": "^4.1.2",
19
+ "express": "^4.16.4",
19
20
  "mocha": "^5.1.1",
20
21
  "mocha-junit-reporter": "^1.17.0",
21
22
  "node-mocks-http": "^1.5.8",
package/package.tar.gz CHANGED
Binary file