Compare View

switch
from
...
to
 
Commits (18)

Changes

Showing 6 changed files Side-by-side Diff

... ... @@ -11,7 +11,9 @@ const express = require('express');
11 11 const config = require('../config');
12 12 const logger = require('../logger');
13 13 const matrix = require('../matrix');
  14 +
14 15 const routerConfig = require('./router-config');
  16 +const routerMatrix = require('./router-matrix');
15 17  
16 18 const app = express();
17 19  
... ... @@ -39,3 +41,4 @@ isConfigured() && app.listen(config.apiserver.port, function () {
39 41  
40 42 app.use('/apikey/:apikey', needValidApikey);
41 43 app.use('/apikey/:apikey/config', routerConfig);
  44 +app.use('/apikey/:apikey/matrix', routerMatrix);
api-server/router-config.js
1 1 "use strict";
2 2  
3 3 const express = require('express');
  4 +const bodyParser = require('body-parser');
4 5 const jsonQuery = require('json-query');
  6 +const dot = require('dot-object');
5 7  
6 8 const config = require('../config');
7 9 const logger = require('../logger');
... ... @@ -15,15 +17,55 @@ function getJsonConfig(req, res, next) {
15 17 }
16 18  
17 19 function getConfigElement(req, res, next) {
18   - const key = (req && req.params && req.params.key) ? req.params.key : 'config';
  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 40  
20   - console.log('KEY: ' + key);
  41 +function delConfigElement(req, res, next) {
  42 + const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim();
21 43  
22   - res.json(jsonQuery(key, {config: config}));
  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 61  
25 62 router.get('/', getJsonConfig);
26 63 router.post('/', getJsonConfig);
27 64  
28   -router.use('/get', getConfigElement);
29   -router.use('/get/:key', getConfigElement);
  65 +router.get('/get', getConfigElement);
  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);
api-server/router-matrix.js
... ... @@ -0,0 +1,13 @@
  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);
api-server/router-services.js
... ... @@ -0,0 +1,51 @@
  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);
... ... @@ -0,0 +1,32 @@
  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 1 {
2 2 "name": "komodo-sdk",
3   - "version": "1.24.4",
  3 + "version": "1.25.0",
4 4 "description": "SDK for Komodo",
5 5 "main": "index.js",
6 6 "scripts": {
... ... @@ -21,6 +21,7 @@
21 21 "dependencies": {
22 22 "basic-auth": "^2.0.0",
23 23 "body-parser": "^1.18.2",
  24 + "dot-object": "^1.7.0",
24 25 "express": "^4.16.3",
25 26 "express-session": "^1.15.6",
26 27 "json-query": "^2.2.2",