Commit e448936d34b140aa2658c77d2daa27566edd2ce2
1 parent
cece8a321a
Exists in
master
Penggunaan parameter partner menggantikan from dan to
Showing 1 changed file with 30 additions and 7 deletions Inline Diff
lib/http-listener.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 | const ipfilter = require('express-ipfilter').IpFilter | 5 | const ipfilter = require('express-ipfilter').IpFilter |
6 | 6 | ||
7 | const config = require('komodo-sdk/config'); | 7 | const config = require('komodo-sdk/config'); |
8 | const logger = require('komodo-sdk/logger'); | 8 | const logger = require('komodo-sdk/logger'); |
9 | 9 | ||
10 | const commandHandler = require('./command-handler'); | 10 | const commandHandler = require('./command-handler'); |
11 | const messagesArchive = require('./messages-archive'); | 11 | const messagesArchive = require('./messages-archive'); |
12 | 12 | ||
13 | const app = express(); | 13 | const app = express(); |
14 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; | 14 | const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; |
15 | 15 | ||
16 | /** | ||
17 | * Handler utama. | ||
18 | * | ||
19 | * @param {object} req - Express request object | ||
20 | * @param {object} req.query - Express query string object | ||
21 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) | ||
22 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner | ||
23 | * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw | ||
24 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner | ||
25 | * @param {string} req.query.msg - Isi pesan | ||
26 | * @param {string} req.query.origin - Nama origin | ||
27 | * @param {string} [req.query.origin_label] - Nama origin untuk ditulis di histori pesan | ||
28 | * @param {string} [req.query.do_not_forward_to_core] - Apakah teruskan pesan ke CORE | ||
29 | * @param {string} [req.query.is_outgoing] - Apakah pesan keluar | ||
30 | * @param {object} res - Express response object | ||
31 | */ | ||
16 | function mainHandler(req, res) { | 32 | function mainHandler(req, res) { |
17 | |||
18 | if (!req.body) req.body = {}; | 33 | if (!req.body) req.body = {}; |
19 | 34 | ||
20 | if (!req.query.from && !req.body.from) { | 35 | if ( |
21 | res.end('ERROR. Undefined parameter: from'); | 36 | ( !req.body.partner || !req.query.partner ) |
37 | && | ||
38 | ( | ||
39 | ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) | ||
40 | || | ||
41 | ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) | ||
42 | ) | ||
43 | ) { | ||
44 | res.end('ERROR. Undefined parameter: partner or from or to'); | ||
22 | return; | 45 | return; |
23 | } | 46 | } |
24 | 47 | ||
25 | if (!req.query.msg && !req.body.msg) { | 48 | if (!req.query.msg && !req.body.msg) { |
26 | res.end('ERROR. Undefined parameter: msg'); | 49 | res.end('ERROR. Undefined parameter: msg'); |
27 | return; | 50 | return; |
28 | } | 51 | } |
29 | 52 | ||
30 | messagesArchive.insert( | 53 | messagesArchive.insert( |
31 | { | 54 | { |
32 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, | 55 | origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, |
33 | origin_transport: req.body.origin_transport || req.query.origin_transport, | 56 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
34 | partner: req.body.from_raw || req.query.from_raw || req.body.from || req.query.from, | 57 | partner: req.body.partner_raw || req.query.partner_raw || req.body.from_raw || req.query.from_raw || req.body.from || req.query.from || req.body.to || req.query.to || req.body.partner || req.query.partner, |
35 | msg: req.body.msg || req.query.msg, | 58 | msg: req.body.msg || req.query.msg, |
36 | }, | 59 | }, |
37 | messagesArchive.DIRECTION_INCOMING | 60 | ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING |
38 | ); | 61 | ); |
39 | 62 | ||
40 | if (req.body.do_not_forward_to_core || req.query.do_not_forward_to_core) { | 63 | if (req.body.do_not_forward_to_core || req.query.do_not_forward_to_core || req.body.is_outgoing || req.query.is_outgoing) { |
41 | logger.verbose('Ignoring message', { from: req.body.from || req.query.from, msg: req.body.msg || req.query.msg, do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core }); | 64 | logger.verbose('Ignoring message', { from: req.body.from || req.query.from, msg: req.body.msg || req.query.msg, do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core }); |
42 | res.end('OK'); | 65 | res.end('OK'); |
43 | return; | 66 | return; |
44 | } | 67 | } |
45 | 68 | ||
46 | if (!req.query.report_port && !req.body.report_port) { | 69 | if (!req.query.report_port && !req.body.report_port) { |
47 | res.end('ERROR. Undefined parameter: report_port'); | 70 | res.end('ERROR. Undefined parameter: report_port'); |
48 | return; | 71 | return; |
49 | } | 72 | } |
50 | 73 | ||
51 | const params = { | 74 | const params = { |
52 | origin: req.body.origin || req.query.origin || 'MESSAGING', | 75 | origin: req.body.origin || req.query.origin || 'MESSAGING', |
53 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, | 76 | report_ip: req.body.report_ip || req.query.report_ip || req.ip, |
54 | report_port: req.body.report_port || req.query.report_port, | 77 | report_port: req.body.report_port || req.query.report_port, |
55 | from: req.body.from || req.query.from, | 78 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, |
56 | msg: req.body.msg || req.query.msg | 79 | msg: req.body.msg || req.query.msg |
57 | } | 80 | } |
58 | 81 | ||
59 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { | 82 | commandHandler(req.body.msg || req.query.msg, params, function(err, coreResponseObject, responseParams) { |
60 | if (err) { | 83 | if (err) { |
61 | res.end('ERROR. ' + err); | 84 | res.end('ERROR. ' + err); |
62 | } | 85 | } |
63 | else if (coreResponseObject) { | 86 | else if (coreResponseObject) { |
64 | res.json(coreResponseObject); | 87 | res.json(coreResponseObject); |
65 | } | 88 | } |
66 | else { | 89 | else { |
67 | res.end(responseParams.body); | 90 | res.end(responseParams.body); |
68 | } | 91 | } |
69 | }) | 92 | }) |
70 | } | 93 | } |
71 | 94 | ||
72 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); | 95 | config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); |
73 | 96 | ||
74 | app.get('/', mainHandler); | 97 | app.get('/', mainHandler); |
75 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); | 98 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); |
76 | 99 | ||
77 | app.listen(port, function() { | 100 | app.listen(port, function() { |
78 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); | 101 | logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); |