Commit 56758af4445b6300ccb363d1ab857bdf6918cfe0
1 parent
f8b177906d
Exists in
master
http-command-server route-sms
Showing 4 changed files with 66 additions and 0 deletions Side-by-side Diff
index.js
lib/http-command-server/index.js
... | ... | @@ -0,0 +1,36 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const express = require('express'); | |
4 | + | |
5 | +const config = require('komodo-sdk/config'); | |
6 | +const logger = require('komodo-sdk/logger'); | |
7 | + | |
8 | +const routerSMS = require('./router-sms'); | |
9 | + | |
10 | +const app = express(); | |
11 | + | |
12 | +function middlewareCustomLog(req, res, next) { | |
13 | + logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url }); | |
14 | + next(); | |
15 | +} | |
16 | + | |
17 | +function middlewareCheckApikey(req, res, next) { | |
18 | + if (!req.query.apikey || (req.query.apikey !== config.http_command_server.apikey)) { | |
19 | + res.json({ | |
20 | + status: 'NOT-OK', | |
21 | + error: 'INVALID_APIKEY', | |
22 | + message: 'Invalid apikey', | |
23 | + }); | |
24 | + } else { | |
25 | + next(); | |
26 | + } | |
27 | +} | |
28 | + | |
29 | +app.use(middlewareCustomLog); | |
30 | +app.use(middlewareCheckApikey); | |
31 | +app.use('/sms', routerSMS); | |
32 | + | |
33 | + | |
34 | +app.listen(config.http_command_server.listen_port, () => { | |
35 | + logger.info(`HTTP command server listeing on port ${config.http_command_server.listen_port}`); | |
36 | +}); |
lib/http-command-server/router-sms.js
... | ... | @@ -0,0 +1,28 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const express = require('express'); | |
4 | + | |
5 | +const modem = require('../modem'); | |
6 | + | |
7 | +const router = express.Router(); | |
8 | +module.exports = router; | |
9 | + | |
10 | +function handlerIndex(req, res) { | |
11 | + if (!req.query || !req.query.number || !req.query.msg) { | |
12 | + res.json({ | |
13 | + status: 'NOT-OK', | |
14 | + error: 'INVALID-PARAMETER', | |
15 | + message: 'Invalid parameter. Missing number or msg parameter.', | |
16 | + }); | |
17 | + return; | |
18 | + } | |
19 | + | |
20 | + res.json({ | |
21 | + status: 'OK', | |
22 | + error: false, | |
23 | + message: 'Message queued.', | |
24 | + }); | |
25 | + modem.sendSMS(req.query.number, req.query.msg); | |
26 | +} | |
27 | + | |
28 | +router.get('/', handlerIndex); |
package.json
... | ... | @@ -27,6 +27,7 @@ |
27 | 27 | "dependencies": { |
28 | 28 | "@serialport/parser-delimiter": "^2.0.2", |
29 | 29 | "@serialport/parser-readline": "^2.0.2", |
30 | + "express": "^4.17.1", | |
30 | 31 | "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", |
31 | 32 | "locks": "^0.2.2", |
32 | 33 | "moment": "^2.24.0", |