soajs.controller 4.2.16 → 4.2.17
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/mw/lastSeen/index.js +81 -0
- package/package.json +2 -1
- package/server/controller.js +3 -0
- package/test/unit/index.js +1 -0
- package/test/unit/mw/lastSeen/index.js +67 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright SOAJS All Rights Reserved.
|
|
6
|
+
*
|
|
7
|
+
* Use of this source code is governed by an Apache license that can be
|
|
8
|
+
* found in the LICENSE file at the root of this repository
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const request = require("request");
|
|
12
|
+
const get = (p, o) => p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param configuration
|
|
17
|
+
* @returns {Function}
|
|
18
|
+
*/
|
|
19
|
+
module.exports = () => {
|
|
20
|
+
|
|
21
|
+
return (req, res, next) => {
|
|
22
|
+
next();
|
|
23
|
+
|
|
24
|
+
let lastSeen = get(["soajs", "registry", "custom", "gateway", "value", "lastSeen"], req);
|
|
25
|
+
if (lastSeen && lastSeen.active) {
|
|
26
|
+
|
|
27
|
+
const item = {
|
|
28
|
+
"name": lastSeen.serviceName || "urac",
|
|
29
|
+
"version": lastSeen.serviceVersion || "3",
|
|
30
|
+
"api": lastSeen.api || "/user/last/seen"
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
let userId = null;
|
|
34
|
+
if (req.soajs.uracDriver) {
|
|
35
|
+
let uracObj = req.soajs.uracDriver.getProfile();
|
|
36
|
+
userId = uracObj._id;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (userId) {
|
|
40
|
+
req.soajs.awareness.getHost(item.name, item.version, (host) => {
|
|
41
|
+
if (host) {
|
|
42
|
+
item.host = host;
|
|
43
|
+
item.port = req.soajs.registry.services[item.name].port;
|
|
44
|
+
|
|
45
|
+
let options = {
|
|
46
|
+
method: "post",
|
|
47
|
+
uri: "http://" + item.host + ":" + item.port + item.api,
|
|
48
|
+
json: true
|
|
49
|
+
};
|
|
50
|
+
if (req.headers && req.headers.soajsinjectobj) {
|
|
51
|
+
options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
|
|
52
|
+
}
|
|
53
|
+
request(options, (error, response, body) => {
|
|
54
|
+
if (error) {
|
|
55
|
+
req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
56
|
+
req.soajs.log.error("lastSeen " + error.message);
|
|
57
|
+
} else if (!body.result) {
|
|
58
|
+
let errorMsg = "";
|
|
59
|
+
if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
|
|
60
|
+
body.errors.details.forEach((detail) => {
|
|
61
|
+
if (errorMsg === "") {
|
|
62
|
+
errorMsg += detail.message;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
errorMsg += " - " + detail.message;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
req.soajs.log.error("lastSeen", errorMsg);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
req.soajs.log.debug("lastSeen failed with no host for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
req.soajs.log.debug("lastSeen failed with no userId for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soajs.controller",
|
|
3
3
|
"description": "soajs multi tenant API gateway",
|
|
4
|
-
"version": "4.2.
|
|
4
|
+
"version": "4.2.17",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "soajs team",
|
|
7
7
|
"email": "team@soajs.org"
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"grunt-jsdoc": "2.4.1",
|
|
51
51
|
"grunt-mocha-test": "0.13.3",
|
|
52
52
|
"mocha": "10.7.3",
|
|
53
|
+
"nock": "13.5.6",
|
|
53
54
|
"sinon": "19.0.2"
|
|
54
55
|
}
|
|
55
56
|
}
|
package/server/controller.js
CHANGED
|
@@ -37,6 +37,7 @@ const keyACL_mw = require("../mw/keyACL/index");
|
|
|
37
37
|
const gotoService_mw = require("../mw/gotoService/index");
|
|
38
38
|
const mt_mw = require("../mw/mt/index");
|
|
39
39
|
const traffic_mw = require("../mw/traffic/index");
|
|
40
|
+
const lastSeen_mw = require("../mw/lastSeen/index");
|
|
40
41
|
|
|
41
42
|
const utils = require("../utilities/utils");
|
|
42
43
|
|
|
@@ -172,6 +173,8 @@ Controller.prototype.init = function (callback) {
|
|
|
172
173
|
"gatewayDB": registry.coreDB.gateway || registry.coreDB.provision
|
|
173
174
|
}));
|
|
174
175
|
|
|
176
|
+
app.use(lastSeen_mw());
|
|
177
|
+
|
|
175
178
|
app.use((req, res, next) => {
|
|
176
179
|
setImmediate(() => {
|
|
177
180
|
req.soajs.controller.gotoservice(req, res, next);
|
package/test/unit/index.js
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const helper = require("../../../helper.js");
|
|
4
|
+
const mw = helper.requireModule('./mw/lastSeen/index');
|
|
5
|
+
const assert = require('assert');
|
|
6
|
+
const nock = require('nock')
|
|
7
|
+
|
|
8
|
+
describe("Unit test for: mw - lastSeen", () => {
|
|
9
|
+
|
|
10
|
+
it("success - default from environment", (done) => {
|
|
11
|
+
let res = {};
|
|
12
|
+
let req = {
|
|
13
|
+
getClientIP: () => {
|
|
14
|
+
return "127.0.0.1"
|
|
15
|
+
},
|
|
16
|
+
soajs: {
|
|
17
|
+
log: {
|
|
18
|
+
debug: (a) => {
|
|
19
|
+
console.log(a);
|
|
20
|
+
},
|
|
21
|
+
error: (a) => {
|
|
22
|
+
console.log(a);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
uracDriver: {
|
|
26
|
+
getProfile: (name, version, cb) => {
|
|
27
|
+
return { _id: "1111111111" };
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
awareness: {
|
|
31
|
+
getHost: (name, version, cb) => {
|
|
32
|
+
return cb("urac.fake");
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
registry: {
|
|
36
|
+
services: {
|
|
37
|
+
urac: {
|
|
38
|
+
port: 4001
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
custom: {
|
|
42
|
+
gateway: {
|
|
43
|
+
value: {
|
|
44
|
+
lastSeen: {
|
|
45
|
+
active: true
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const scope = nock('http://urac.fake:4001')
|
|
54
|
+
.post('/user/last/seen')
|
|
55
|
+
.reply(200, {
|
|
56
|
+
result: true,
|
|
57
|
+
data: true
|
|
58
|
+
})
|
|
59
|
+
let functionMw = mw({});
|
|
60
|
+
functionMw(req, res, (error) => {
|
|
61
|
+
assert.ifError(error);
|
|
62
|
+
// nock.cleanAll()
|
|
63
|
+
done();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
});
|