Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 2 changed files Inline Diff

gateway/advice-push-server.js
1 "use strict"; 1 "use strict";
2 2
3 const express = require('express'); 3 const express = require('express');
4 const bodyParser = require('body-parser'); 4 const bodyParser = require('body-parser');
5 5
6 const pull = require('./pull'); 6 const pull = require('./pull');
7 const config = require('../config'); 7 const config = require('../config');
8 const logger = require('../logger'); 8 const logger = require('../logger');
9 const matrix = require('../matrix'); 9 const matrix = require('../matrix');
10 10
11 if (!config || !config.push_server || !!config.push_server.apikey || !config.push_server.advice || !config.push_server.advice.url || !config.push_server.advice.port) return; 11 if (!config || !config.push_server || !!config.push_server.apikey || !config.push_server.advice || !config.push_server.advice.url || !config.push_server.advice.port) return;
12 12
13 const app = express(); 13 const app = express();
14 14
15 let partner = null; 15 let partner = null;
16 16
17 function setPartner(_partner) { 17 function setPartner(_partner) {
18 partner = _partner; 18 partner = _partner;
19 19
20 app.listen(config.push_server.advice.port, function () { 20 app.listen(config.push_server.advice.port, function () {
21 logger.info('Advice server listening', {port: config.push_server.advice.port}); 21 logger.info('Advice server listening', {port: config.push_server.advice.port});
22 }); 22 });
23 } 23 }
24 24
25 function isValidApikey(req, res, next) { 25 function isValidApikey(req, res, next) {
26 if (req.params.apikey === config.push_server.apikey) { 26 if (req.params.apikey === config.push_server.apikey) {
27 next(); 27 next();
28 } 28 }
29 else { 29 else {
30 res.end('INVALID_APIKEY'); 30 res.end('INVALID_APIKEY');
31 } 31 }
32 } 32 }
33 33
34 function adviceHandler(req, res, next) { 34 function adviceHandler(req, res, next) {
35 35
36 if (!partner) { 36 if (!partner) {
37 logger.warn('PUSH-ADVICE: Undefined partner, skipped'); 37 logger.warn('PUSH-ADVICE: Undefined partner, skipped');
38 res.end('UNDEFINED_PARTNER'); 38 res.end('UNDEFINED_PARTNER');
39 return; 39 return;
40 } 40 }
41 41
42 if (!partner.advice) { 42 if (!partner.advice) {
43 logger.warn('PUSH-ADVICE: Partner does not have ADVICE capabilities'); 43 logger.warn('PUSH-ADVICE: Partner does not have ADVICE capabilities');
44 res.end('UNSUPPORTED'); 44 res.end('UNSUPPORTED');
45 return; 45 return;
46 } 46 }
47 47
48 let task = null; 48 let task = null;
49 49
50 try { 50 try {
51 task = JSON.parse(coreMessage); 51 task = JSON.parse(coreMessage);
52 } 52 }
53 catch(e) { 53 catch(e) {
54 logger.warn('PUSH-ADVICE: Exception on parsing task to advice', {err: e}); 54 logger.warn('PUSH-ADVICE: Exception on parsing task to advice', {err: e});
55 } 55 }
56 56
57 if (!task) { 57 if (!task) {
58 logger.warn('PUSH-ADVICE: Invalid task');
58 logger.warn('PUSH-ADVICE: Invalid task'); 59 res.end('INVALID_TASK');
59 res.end('INVALID_TASK'); 60 return;
60 return; 61 }
61 } 62
63 logger.verbose('PUSH-ADVICE: Got advice push', {task: task});
64
62 65 task.remote_product = pull.getRemoteProduct(task.product);
63 logger.verbose('PUSH-ADVICE: Got advice push', {task: task}); 66 partner.advice(task);
64 67 }
65 task.remote_product = pull.getRemoteProduct(task.product); 68
66 partner.advice(task); 69 app.use(bodyParser.json());
67 } 70 app.use('/apikey/:apikey', isValidApikey);
68 71 app.use('/apikey/:apikey/advice', adviceHandler);
69 app.use(bodyParser.json()); 72
70 app.use('/apikey/:apikey', isValidApikey); 73 exports.setPartner = setPartner;
71 app.use('/apikey/:apikey/advice', adviceHandler); 74
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.17.1", 3 "version": "1.17.2",
4 "description": "SDK for Komodo", 4 "description": "SDK for Komodo",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha", 7 "test": "mocha",
8 "postversion": "git push && git push --tags" 8 "postversion": "git push && git push --tags"
9 }, 9 },
10 "repository": { 10 "repository": {
11 "type": "git", 11 "type": "git",
12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" 12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git"
13 }, 13 },
14 "keywords": [ 14 "keywords": [
15 "ppob", 15 "ppob",
16 "payment", 16 "payment",
17 "komodo" 17 "komodo"
18 ], 18 ],
19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", 19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
20 "license": "ISC", 20 "license": "ISC",
21 "dependencies": { 21 "dependencies": {
22 "basic-auth": "^2.0.0", 22 "basic-auth": "^2.0.0",
23 "body-parser": "^1.18.2", 23 "body-parser": "^1.18.2",
24 "express": "^4.16.3", 24 "express": "^4.16.3",
25 "express-session": "^1.15.6", 25 "express-session": "^1.15.6",
26 "lru-cache": "^4.1.1", 26 "lru-cache": "^4.1.1",
27 "macaddress": "^0.2.8", 27 "macaddress": "^0.2.8",
28 "moment": "^2.19.1", 28 "moment": "^2.19.1",
29 "node-machine-id": "^1.1.10", 29 "node-machine-id": "^1.1.10",
30 "node-natural-sort": "^0.8.6", 30 "node-natural-sort": "^0.8.6",
31 "numeral": "^2.0.6", 31 "numeral": "^2.0.6",
32 "nunjucks": "^3.0.1", 32 "nunjucks": "^3.0.1",
33 "redis": "^2.8.0", 33 "redis": "^2.8.0",
34 "request": "^2.81.0", 34 "request": "^2.81.0",
35 "sha1": "^1.1.1", 35 "sha1": "^1.1.1",
36 "simple-git": "^1.80.1", 36 "simple-git": "^1.80.1",
37 "strftime": "^0.10.0", 37 "strftime": "^0.10.0",
38 "uniqid": "^4.1.1", 38 "uniqid": "^4.1.1",
39 "uuid": "^3.1.0", 39 "uuid": "^3.1.0",
40 "winston": "^2.3.1", 40 "winston": "^2.3.1",
41 "winston-circular-buffer": "^1.0.0", 41 "winston-circular-buffer": "^1.0.0",
42 "winston-daily-rotate-file": "^1.4.6" 42 "winston-daily-rotate-file": "^1.4.6"
43 } 43 }
44 } 44 }
45 45