Compare View
Commits (18)
Changes
Showing 6 changed files Inline Diff
api-server/index.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | /** | 3 | /** |
4 | * API Server | 4 | * API Server |
5 | * | 5 | * |
6 | * @todo make it work | 6 | * @todo make it work |
7 | */ | 7 | */ |
8 | 8 | ||
9 | const express = require('express'); | 9 | const express = require('express'); |
10 | 10 | ||
11 | const config = require('../config'); | 11 | const config = require('../config'); |
12 | const logger = require('../logger'); | 12 | const logger = require('../logger'); |
13 | const matrix = require('../matrix'); | 13 | const matrix = require('../matrix'); |
14 | |||
14 | 15 | const routerConfig = require('./router-config'); | |
16 | const routerMatrix = require('./router-matrix'); | ||
15 | const routerConfig = require('./router-config'); | 17 | |
16 | const routerMatrix = require('./router-matrix'); | 18 | const app = express(); |
17 | 19 | ||
18 | const app = express(); | 20 | function isConfigured() { |
19 | 21 | return Boolean(config && config.apiserver && config.apiserver.apikey && config.apiserver.port); | |
20 | function isConfigured() { | 22 | } |
21 | return Boolean(config && config.apiserver && config.apiserver.apikey && config.apiserver.port); | 23 | |
22 | } | 24 | function isValidApikey(apikey) { |
23 | 25 | return isConfigured() && (config.apiserver.apikey === apikey); | |
24 | function isValidApikey(apikey) { | 26 | } |
25 | return isConfigured() && (config.apiserver.apikey === apikey); | 27 | |
26 | } | 28 | function needValidApikey(req, res, next) { |
27 | 29 | if (isValidApikey(req.params.apikey)) { | |
28 | function needValidApikey(req, res, next) { | 30 | next(); |
29 | if (isValidApikey(req.params.apikey)) { | 31 | } |
30 | next(); | 32 | else { |
31 | } | 33 | res.end('INVALID_APIKEY'); |
32 | else { | 34 | } |
33 | res.end('INVALID_APIKEY'); | 35 | } |
34 | } | 36 | |
35 | } | 37 | isConfigured() && app.listen(config.apiserver.port, function () { |
36 | 38 | logger.info('API-SERVER listening', {port: config.apiserver.port}); | |
37 | isConfigured() && app.listen(config.apiserver.port, function () { | 39 | }); |
38 | logger.info('API-SERVER listening', {port: config.apiserver.port}); | 40 | |
39 | }); | 41 | |
40 | 42 | app.use('/apikey/:apikey', needValidApikey); | |
41 | 43 | app.use('/apikey/:apikey/config', routerConfig); | |
44 | app.use('/apikey/:apikey/matrix', routerMatrix); | ||
42 | app.use('/apikey/:apikey', needValidApikey); | 45 |
api-server/router-config.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 jsonQuery = require('json-query'); |
6 | const dot = require('dot-object'); | ||
5 | const jsonQuery = require('json-query'); | 7 | |
6 | const dot = require('dot-object'); | 8 | const config = require('../config'); |
7 | 9 | const logger = require('../logger'); | |
8 | const config = require('../config'); | 10 | const matrix = require('../matrix'); |
9 | const logger = require('../logger'); | 11 | |
10 | const matrix = require('../matrix'); | 12 | const router = express.Router(); |
11 | 13 | module.exports = router; | |
12 | const router = express.Router(); | 14 | |
13 | module.exports = router; | 15 | function getJsonConfig(req, res, next) { |
14 | 16 | res.json(config); | |
15 | function getJsonConfig(req, res, next) { | 17 | } |
16 | res.json(config); | 18 | |
17 | } | 19 | function getConfigElement(req, res, next) { |
18 | 20 | const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim(); | |
21 | res.json(jsonQuery(key, {data: config}).value); | ||
22 | } | ||
23 | |||
24 | function setConfigElement(req, res, next) { | ||
25 | if (!req.body || !req.body.key || !req.body.value) { | ||
26 | res.end('INVALID BODY'); | ||
27 | return; | ||
28 | } | ||
29 | |||
30 | dot.str(req.body.key, req.body.value, config); | ||
31 | matrix.config_is_dirty = true; | ||
32 | |||
33 | res.json({ | ||
34 | method: '/config/set', | ||
35 | key: req.body.key, | ||
36 | value: req.body.value, | ||
37 | new_config: config | ||
38 | }); | ||
39 | } | ||
19 | function getConfigElement(req, res, next) { | 40 | |
20 | const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim(); | 41 | function delConfigElement(req, res, next) { |
42 | const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim(); | ||
21 | res.json(jsonQuery(key, {data: config}).value); | 43 | |
22 | } | 44 | if (!key) { |
45 | res.end('INVALID OBJECT KEY') | ||
46 | } | ||
47 | |||
48 | dot.str(key, config); | ||
49 | matrix.config_is_dirty = true; | ||
50 | |||
51 | res.json({ | ||
52 | method: '/config/del', | ||
53 | key: req.body.key, | ||
54 | new_config: config | ||
55 | }); | ||
56 | } | ||
57 | |||
58 | function saveConfig(req, res, next) { | ||
59 | res.end('NOT YET IMPLEMENTED'); | ||
23 | 60 | } | |
24 | function setConfigElement(req, res, next) { | 61 | |
25 | if (!req.body || !req.body.key || !req.body.value) { | 62 | router.get('/', getJsonConfig); |
26 | res.end('INVALID BODY'); | 63 | router.post('/', getJsonConfig); |
27 | return; | 64 | |
28 | } | 65 | router.get('/get', getConfigElement); |
29 | 66 | router.get('/get/:key', getConfigElement); | |
67 | |||
68 | router.post('/set/:key', bodyParser.json(), setConfigElement); | ||
69 | |||
70 | router.get('/del/:key', delConfigElement); | ||
71 | router.get('/save', saveConfig); | ||
30 | dot.str(req.body.key, req.body.value, config); | 72 |
api-server/router-matrix.js
File was created | 1 | const express = require('express'); | |
2 | const bodyParser = require('body-parser'); | ||
3 | |||
4 | const matrix = require('../matrix'); | ||
5 | |||
6 | const router = express.Router(); | ||
7 | module.exports = router; | ||
8 | |||
9 | function getJsonMatrix(req, res, next) { | ||
10 | res.json(matrix); | ||
11 | } | ||
12 | |||
13 | router.get('/', getJsonMatrix); | ||
1 | const express = require('express'); | 14 |
api-server/router-services.js
File was created | 1 | "use strict"; | |
2 | |||
3 | const express = require('express'); | ||
4 | |||
5 | const config = require('../config'); | ||
6 | const logger = require('../logger'); | ||
7 | const matrix = require('../matrix'); | ||
8 | |||
9 | const router = express.Router(); | ||
10 | module.exports = router; | ||
11 | |||
12 | function isPause(req, res, next) { | ||
13 | res.json({ | ||
14 | method: '/services/is-pause', | ||
15 | error: null, | ||
16 | result: { isPause: Boolean(matrix.pause) } | ||
17 | }); | ||
18 | } | ||
19 | |||
20 | function pause(req, res, next) { | ||
21 | matrix.pause = true; | ||
22 | res.json({ | ||
23 | method: '/services/pause', | ||
24 | error: null, | ||
25 | result: { isPause: Boolean(matrix.pause) } | ||
26 | }); | ||
27 | } | ||
28 | |||
29 | function resume(req, res, next) { | ||
30 | matrix.pause = false; | ||
31 | res.json({ | ||
32 | method: '/services/resume', | ||
33 | error: null, | ||
34 | result: { isPause: Boolean(matrix.pause) } | ||
35 | }); | ||
36 | } | ||
37 | |||
38 | function terminate(req, res, next) { | ||
39 | res.json({ | ||
40 | method: '/services/terminate', | ||
41 | error: null, | ||
42 | result: { | ||
43 | message: 'Going to restart in ' + delay + 'ms' | ||
44 | } | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | |||
49 | router.get('/is-pause', isPause); | ||
50 | router.get('/pause', pause); | ||
51 | router.get('/resume', resume); | ||
1 | "use strict"; | 52 |
extract-from-msg.js
File was created | 1 | "use strict"; | |
2 | |||
3 | function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) { | ||
4 | if (!msg || typeof msg !== 'string') { | ||
5 | return; | ||
6 | } | ||
7 | |||
8 | let pattern; | ||
9 | let match_idx; | ||
10 | |||
11 | if (custom_rule && custom_rule.pattern) { | ||
12 | pattern = custom_rule.pattern; | ||
13 | match_idx = custom_rule.match_idx; | ||
14 | } | ||
15 | else { | ||
16 | pattern = default_pattern; | ||
17 | match_idx = default_match_idx; | ||
18 | } | ||
19 | |||
20 | const re = new RegExp(pattern); | ||
21 | const matches = msg.match(re); | ||
22 | |||
23 | if (!matches) return; | ||
24 | |||
25 | if (match_idx < matches.length) { | ||
26 | return matches[match_idx] || null; | ||
27 | } else { | ||
28 | return; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | module.exports = extractFromMessage; | ||
1 | "use strict"; | 33 |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.24.4", | 3 | "version": "1.25.0", |
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 | "dot-object": "^1.7.0", | ||
24 | "dot-object": "^1.7.0", | 25 | "express": "^4.16.3", |
25 | "express": "^4.16.3", | 26 | "express-session": "^1.15.6", |
26 | "express-session": "^1.15.6", | 27 | "json-query": "^2.2.2", |
27 | "json-query": "^2.2.2", | 28 | "lru-cache": "^4.1.1", |
28 | "lru-cache": "^4.1.1", | 29 | "macaddress": "^0.2.8", |
29 | "macaddress": "^0.2.8", | 30 | "moment": "^2.19.1", |
30 | "moment": "^2.19.1", | 31 | "node-machine-id": "^1.1.10", |
31 | "node-machine-id": "^1.1.10", | 32 | "node-natural-sort": "^0.8.6", |
32 | "node-natural-sort": "^0.8.6", | 33 | "numeral": "^2.0.6", |
33 | "numeral": "^2.0.6", | 34 | "nunjucks": "^3.0.1", |
34 | "nunjucks": "^3.0.1", | 35 | "redis": "^2.8.0", |
35 | "redis": "^2.8.0", | 36 | "request": "^2.81.0", |
36 | "request": "^2.81.0", | 37 | "sha1": "^1.1.1", |
37 | "sha1": "^1.1.1", | 38 | "simple-git": "^1.80.1", |
38 | "simple-git": "^1.80.1", | 39 | "strftime": "^0.10.0", |
39 | "strftime": "^0.10.0", | 40 | "uniqid": "^4.1.1", |
40 | "uniqid": "^4.1.1", | 41 | "uuid": "^3.1.0", |
41 | "uuid": "^3.1.0", | 42 | "winston": "^2.3.1", |
42 | "winston": "^2.3.1", | 43 | "winston-circular-buffer": "^1.0.0", |
43 | "winston-circular-buffer": "^1.0.0", | 44 | "winston-daily-rotate-file": "^1.4.6" |
44 | "winston-daily-rotate-file": "^1.4.6" | 45 | } |
45 | } | 46 | } |
46 | } | 47 |