soajs.controller 4.2.16 → 4.2.18

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/.travis.yml CHANGED
@@ -58,6 +58,12 @@ jobs:
58
58
  - docker push soajsorg/gateway
59
59
  - docker push soajsorg/gateway:${PACKAGE_VERSION}
60
60
  - docker push soajsorg/gateway:${MAJOR_VERSION}.x
61
+ before_deploy:
62
+ - yes | gem update --system --force
63
+ - gem install bundler
64
+ - gem install faraday-net_http -v '3.3.0'
65
+ - gem install uri
66
+ - gem install logger
61
67
  deploy:
62
68
  on:
63
69
  all_branches: true
@@ -0,0 +1,83 @@
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
+ if (uracObj) {
37
+ userId = uracObj._id;
38
+ }
39
+ }
40
+
41
+ if (userId) {
42
+ req.soajs.awareness.getHost(item.name, item.version, (host) => {
43
+ if (host) {
44
+ item.host = host;
45
+ item.port = req.soajs.registry.services[item.name].port;
46
+
47
+ let options = {
48
+ method: "post",
49
+ uri: "http://" + item.host + ":" + item.port + item.api,
50
+ json: true
51
+ };
52
+ if (req.headers && req.headers.soajsinjectobj) {
53
+ options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
54
+ }
55
+ request(options, (error, response, body) => {
56
+ if (error) {
57
+ req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
58
+ req.soajs.log.error("lastSeen " + error.message);
59
+ } else if (!body.result) {
60
+ let errorMsg = "";
61
+ if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
62
+ body.errors.details.forEach((detail) => {
63
+ if (errorMsg === "") {
64
+ errorMsg += detail.message;
65
+ }
66
+ else {
67
+ errorMsg += " - " + detail.message;
68
+ }
69
+ });
70
+ }
71
+ req.soajs.log.error("lastSeen", errorMsg);
72
+ }
73
+ });
74
+ } else {
75
+ req.soajs.log.debug("lastSeen failed with no host for [" + item.name + "@" + item.version + " " + item.api + "]");
76
+ }
77
+ });
78
+ } else {
79
+ req.soajs.log.debug("lastSeen failed with no userId for [" + item.name + "@" + item.version + " " + item.api + "]");
80
+ }
81
+ }
82
+ };
83
+ };
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.16",
4
+ "version": "4.2.18",
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
  }
@@ -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);
@@ -34,6 +34,7 @@ describe("Starting Gateway Unit test", () => {
34
34
  require("./mw/response/index.js");
35
35
  require("./mw/soajs/index.js");
36
36
  require("./mw/traffic/index.js");
37
+ require("./mw/lastSeen/index.js");
37
38
 
38
39
  require("./utilities/utils.js");
39
40
 
@@ -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
+ });