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