-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (115 loc) · 3.18 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, mongodb = require('mongodb');
var _ = require('underscore');
var cons = require('consolidate');
var swig = require('swig');
var swigFilters = require('./filters');
var http = require('http');
var utils = require('./utils');
var config = require('./config');
var path = require('path');
var app = express();
var MongoStore = require('connect-mongo')(express);
var server = http.createServer(app);
app.engine('html', cons.swig);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.set('view options', {layout: true});
app.use("/public", express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.favicon('./public/images/favicon.ico'));
app.use(express.cookieParser());
app.use(express.session({
secret: 'mattia',
cookie:{ expires: false },
store: new MongoStore({
db: 'local',
collection: 'session'
}),
expires:60000
}));
app.use(app.router);
swig.setDefaults({ cache: false });
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
/*app.configure('production', function(){
app.use(express.errorHandler());
});*/
mainDb = {}; //dati connessione...
adminDb = {};
collection = {}; //elenco collezioni...
databases = []; //elenco database...
connessioni = []; //elenco dati connessioni singoli database...
//Connessione a istanza MongoDB
connetti = function(URL, server, mongoClient, next){
mongoClient.connect(URL,function(err,db){
if(err){
next(err);
} else {
console.log("Database connesso!!");
mainDb = db;
recuperoInfo(mainDb);
setTimeout(next(),1000);
}
});
};
/*
* Recupero informazioni sui database disponibili
*/
recuperoInfo = function(db){
adminDb = db.admin();
adminDb.listDatabases(function(err, dbs){
for(var data in dbs.databases){
var dbName = dbs.databases[data]['name'];
if(dbName == 'local'){
continue;
}
databases.push(dbName);
connessioni[dbName] = mainDb.db(dbName);
aggiornaCollezioni(connessioni[dbName], dbName);
}
databases.sort();
});
}
/*
* Recupero informazioni sulle collezioni presenti nei database disponibili
*/
aggiornaCollezioni = function(db,dbName){
db.collectionNames(function(err, items) {
var names = [];
for(var i = 0; i < items.length; i++){
var coll = utils.parseCollectionName(items[i].name);
names.push(coll.name);
}
names.sort();
collection[dbName] = names;
});
};
app.param('database', function(req, res, next, id) {
req.dbName = id;
res.locals.dbName = id;
req.mainDb = mainDb;
next();
});
app.all('*', function(req, res, next) {
req.adminDb = adminDb;
next();
});
// Routes
require('./routes/index')(app);
require('./routes/database')(app);
require('./routes/collection')(app);
require('./routes/document')(app);
app.listen(8000, function(){
console.log("Express server listening on port 8000...");
//console.log("Express server listening on port %d in %s mode", server.address(), app.settings.env);
});