UNPKG

4.81 kBJavaScriptView Raw
1var express = require('express');
2var router = express.Router();
3var common = require('./common');
4
5// runs on all routes and checks password if one is setup
6router.all('/config/*', common.checkLogin, function (req, res, next){
7 next();
8});
9
10// Add a new connection config
11router.post('/config/add_config', function (req, res, next){
12 var nconf = req.nconf.connections;
13 var MongoURI = require('mongo-uri');
14 var connPool = require('../connections');
15 var connection_list = req.nconf.connections.get('connections');
16
17 // check if name already exists
18 if(connection_list !== undefined){
19 if(connection_list[req.body[0]] !== undefined){
20 res.status(400).json({'msg': req.i18n.__('Config error: A connection by that name already exists')});
21 return;
22 }
23 }
24
25 // try parse uri string. If pass, add, else throw an error
26 try{
27 MongoURI.parse(req.body[1]);
28 var options = {};
29 try{
30 options = JSON.parse(req.body[2]);
31 }catch(err){
32 res.status(400).json({'msg': req.i18n.__('Error in connection options') + ': ' + err});
33 return;
34 }
35
36 // try add the connection
37 connPool.addConnection({connName: req.body[0], connString: req.body[1], connOptions: options}, req.app, function (err, data){
38 if(err){
39 console.error('DB Connect error: ' + err);
40 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
41 }else{
42 // set the new config
43 nconf.set('connections:' + req.body[0], {'connection_string': req.body[1], 'connection_options': options});
44
45 // save for ron
46 nconf.save(function (err){
47 if(err){
48 console.error('Config error: ' + err);
49 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
50 }else{
51 res.status(200).json({'msg': req.i18n.__('Config successfully added')});
52 }
53 });
54 }
55 });
56 }catch(err){
57 console.error('Config error: ' + err);
58 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
59 }
60});
61
62// Updates an existing connection config
63router.post('/config/update_config', function (req, res, next){
64 var nconf = req.nconf.connections;
65 var connPool = require('../connections');
66 var MongoURI = require('mongo-uri');
67
68 // try parse uri string. If pass, add, else throw an error
69 try{
70 MongoURI.parse(req.body.conn_string);
71
72 // var get current options
73 var current_options = nconf.store.connections[req.body.curr_config].connection_options;
74
75 // try add the connection
76 connPool.addConnection({connName: req.body.conn_name, connString: req.body.conn_string, connOptions: current_options}, req.app, function (err, data){
77 if(err){
78 console.error('DB Connect error: ' + err);
79 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
80 }else{
81 // delete current config
82 delete nconf.store.connections[req.body.curr_config];
83
84 // set the new
85 nconf.set('connections:' + req.body.conn_name, {'connection_string': req.body.conn_string, 'connection_options': current_options});
86
87 // save for ron
88 nconf.save(function (err){
89 if(err){
90 console.error('Config error1: ' + err);
91 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
92 }else{
93 res.status(200).json({'msg': req.i18n.__('Config successfully updated'), 'name': req.body.conn_name, 'string': req.body.conn_string});
94 }
95 });
96 }
97 });
98 }catch(err){
99 console.error('Config error: ' + err);
100 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
101 }
102});
103
104// Drops an existing connection config
105router.post('/config/drop_config', function (req, res, next){
106 var nconf = req.nconf.connections;
107 var connPool = require('../connections');
108
109 // delete current config
110 delete nconf.store.connections[req.body.curr_config];
111 connPool.removeConnection(req.body.curr_config, req.app);
112
113 // save config
114 nconf.save(function (err){
115 if(err){
116 console.error('Config error: ' + err);
117 res.status(400).json({'msg': req.i18n.__('Config error') + ': ' + err});
118 }else{
119 res.status(200).json({'msg': req.i18n.__('Config successfully deleted')});
120 }
121 });
122});
123
124module.exports = router;