Commit 8fa0b35c5dbe9f23b57558a31d39da08b6a3699a
1 parent
1299ad85d9
Exists in
master
advice ready to test
Showing 3 changed files with 72 additions and 1 deletions 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