Compare View
Commits (2)
Changes
Showing 3 changed files Side-by-side Diff
gateway/advice-push-server.js
... | ... | @@ -0,0 +1,70 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const express = require('express'); | |
4 | +const bodyParser = require('body-parser'); | |
5 | + | |
6 | +const pull = require('./pull'); | |
7 | +const config = require('../config'); | |
8 | +const logger = require('../logger'); | |
9 | +const matrix = require('../matrix'); | |
10 | + | |
11 | +if (!config || !config.push_server || !!config.push_server.apikey || !config.push_server.advice_port) return; | |
12 | + | |
13 | +const app = express(); | |
14 | + | |
15 | +let partner = null; | |
16 | + | |
17 | +function setPartner(_partner) { | |
18 | + partner = _partner; | |
19 | + | |
20 | + app.listen(config.push_server.advice_port, function () { | |
21 | + logger.info('Advice server listening', {port: config.push_server.advice_port}); | |
22 | + }); | |
23 | +} | |
24 | + | |
25 | +function isValidApikey(req, res, next) { | |
26 | + if (req.params.apikey === config.push_server.apikey) { | |
27 | + next(); | |
28 | + } | |
29 | + else { | |
30 | + res.end('INVALID_APIKEY'); | |
31 | + } | |
32 | +} | |
33 | + | |
34 | +function adviceHandler(req, res, next) { | |
35 | + | |
36 | + if (!partner) { | |
37 | + logger.warn('PUSH-ADVICE: Undefined partner, skipped'); | |
38 | + res.end('UNDEFINED_PARTNER'); | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + if (!partner.advice) { | |
43 | + logger.warn('PUSH-ADVICE: Partner does not have ADVICE capabilities'); | |
44 | + res.end('UNSUPPORTED'); | |
45 | + return; | |
46 | + } | |
47 | + | |
48 | + let task = null; | |
49 | + | |
50 | + try { | |
51 | + task = JSON.parse(coreMessage); | |
52 | + } | |
53 | + catch(e) { | |
54 | + logger.warn('PUSH-ADVICE: Exception on parsing task to advice', {err: e}); | |
55 | + } | |
56 | + | |
57 | + if (!task) { | |
58 | + res.end('INVALID_TASK'); | |
59 | + return; | |
60 | + } | |
61 | + | |
62 | + task.remote_product = pull.getRemoteProduct(task.product); | |
63 | + partner.advice(task); | |
64 | +} | |
65 | + | |
66 | +app.use(bodyParser.json()); | |
67 | +app.use('/apikey/:apikey', isValidApikey); | |
68 | +app.use('/apikey/:apikey/advice', adviceHandler); | |
69 | + | |
70 | +exports.setPartner = setPartner; |
gateway/pull.js
package.json
1 | 1 | { |
2 | 2 | "name": "komodo-sdk", |
3 | - "version": "1.16.4", | |
3 | + "version": "1.17.0", | |
4 | 4 | "description": "SDK for Komodo", |
5 | 5 | "main": "index.js", |
6 | 6 | "scripts": { |
... | ... | @@ -21,7 +21,7 @@ |
21 | 21 | "dependencies": { |
22 | 22 | "basic-auth": "^2.0.0", |
23 | 23 | "body-parser": "^1.18.2", |
24 | - "express": "^4.16.2", | |
24 | + "express": "^4.16.3", | |
25 | 25 | "express-session": "^1.15.6", |
26 | 26 | "lru-cache": "^4.1.1", |
27 | 27 | "macaddress": "^0.2.8", |