Commit c1e6950b52c7a2dc31a67ff7e5c84071c00ee169

Authored by Adhidarma Hadiwinoto
1 parent 4a7ed5bd6b
Exists in master

Change api-server.router-config.save using config-save

Showing 1 changed file with 27 additions and 29 deletions Inline Diff

api-server/router-config.js
1 const MODULE_NAME = 'KOMODO-SDK.API-SERVER.ROUTER-CONFIG';
2
1 const fs = require('fs'); 3 const fs = require('fs');
2 const express = require('express'); 4 const express = require('express');
3 const bodyParser = require('body-parser'); 5 const bodyParser = require('body-parser');
4 const jsonQuery = require('json-query'); 6 const jsonQuery = require('json-query');
5 const dot = require('dot-object'); 7 const dot = require('dot-object');
6 const copyFile = require('fs-copy-file'); 8 const logger = require('tektrans-logger');
7 const moment = require('moment');
8 9
9 const config = require('../config'); 10 const config = require('../config');
10 const matrix = require('../matrix'); 11 const matrix = require('../matrix');
12 const configSave = require('./config-save');
11 13
12 const router = express.Router(); 14 const router = express.Router();
13 module.exports = router; 15 module.exports = router;
14 16
15 if (!fs.existsSync('config-backup')) fs.mkdirSync('config-backup'); 17 if (!fs.existsSync('config-backup')) fs.mkdirSync('config-backup');
16 18
17 function getJsonConfig(req, res) { 19 function getJsonConfig(req, res) {
18 res.json(config); 20 res.json(config);
19 } 21 }
20 22
21 function getConfigElement(req, res) { 23 function getConfigElement(req, res) {
22 const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim(); 24 const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim();
23 res.json(jsonQuery(key, { data: config }).value); 25 res.json(jsonQuery(key, { data: config }).value);
24 } 26 }
25 27
26 function setConfigElement(req, res) { 28 function setConfigElement(req, res) {
27 if (!req.body || !req.body.key || !req.body.value) { 29 if (!req.body || !req.body.key || !req.body.value) {
28 res.end('INVALID BODY'); 30 res.end('INVALID BODY');
29 return; 31 return;
30 } 32 }
31 33
32 dot.str(req.body.key, req.body.value, config); 34 dot.str(req.body.key, req.body.value, config);
33 matrix.config_is_dirty = true; 35 matrix.config_is_dirty = true;
34 36
35 res.json({ 37 res.json({
36 method: '/config/set', 38 method: '/config/set',
37 key: req.body.key, 39 key: req.body.key,
38 value: req.body.value, 40 value: req.body.value,
39 new_config: config, 41 new_config: config,
40 }); 42 });
41 } 43 }
42 44
43 function delConfigElement(req, res) { 45 function delConfigElement(req, res) {
44 const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim(); 46 const key = ((req && req.params && req.params.key) ? req.params.key : '').replace(/^config\.*/, '').trim();
45 47
46 if (!key) { 48 if (!key) {
47 res.end('INVALID OBJECT KEY'); 49 res.end('INVALID OBJECT KEY');
48 } 50 }
49 51
50 dot.str(key, config); 52 dot.str(key, config);
51 matrix.config_is_dirty = true; 53 matrix.config_is_dirty = true;
52 54
53 res.json({ 55 res.json({
54 method: '/config/del', 56 method: '/config/del',
55 key: req.body.key, 57 key: req.body.key,
56 new_config: config, 58 new_config: config,
57 }); 59 });
58 } 60 }
59 61
60 function saveConfig(req, res) { 62 async function saveConfig(req, res) {
61 copyFile('config.json', `config-backup/config_${moment().format('YYYYMMDD_HHmmss.SS')}.json`, (err) => { 63 const { xid } = res.locals;
62 if (err) { 64 try {
63 res.json({ 65 await configSave(xid);
64 method: '/config/save', 66
65 error: err.toString(), 67 res.json({
66 }); 68 method: '/config/save',
67 return; 69 error: null,
68 }
69
70 fs.writeFile('config.json', JSON.stringify(config, null, 2), (errWriteFile) => {
71 if (errWriteFile) {
72 res.json({
73 method: '/config/save',
74 error: errWriteFile.toString(),
75 });
76
77 return;
78 }
79
80 matrix.config_is_dirty = false;
81
82 res.json({
83 method: '/config/save',
84 error: null,
85 });
86 }); 70 });
87 }); 71 } catch (e) {
72 logger.warn(`${MODULE_NAME} 22E7FCA2: Exception on saving`, {
73 xid,
74 eCode: e.code,
75 eMessage: e.message || e,
76 });
77
78 res.json({
79 method: '/config/save',
80 error: [
81 e.code || 'UNKNOWN',
82 e.message || 'ERROR',
83 ].join(' - '),
84 });
85 }
88 } 86 }
89 87
90 function isDirty(req, res) { 88 function isDirty(req, res) {
91 res.json({ 89 res.json({
92 method: '/config/is-dirty', 90 method: '/config/is-dirty',
93 error: null, 91 error: null,