-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·130 lines (112 loc) · 5.1 KB
/
server.js
File metadata and controls
executable file
·130 lines (112 loc) · 5.1 KB
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
#!/usr/bin/env node
/**
* Mongorilla Server.
*/
var express = require('express'),
http = require('http'),
path = require('path'),
fs = require('fs'),
mongoose = require('mongoose'),
mongooseWhen = require('mongoose-when'),
gridfs = require('gridfs-stream'),
_ = require('underscore'),
moment = require('moment'),
authRoute = require('./routes/auth'),
appMainRoute = require('./routes/app/main'),
appGenericRoute = require('./routes/app/generic'),
appJsRoute = require('./routes/app/js'),
apiGenericRoute = require('./routes/api/generic'),
apiRevisionRoute = require('./routes/api/revision'),
apiFileRoute = require('./routes/api/file');
var app = express();
// all environments
app.set('port', process.env.PORT || 8000);
app.set('views', __dirname + '/views');
app.engine('html', require('uinexpress').__express);
app.engine('js', require('uinexpress').__express);
app.set('view engine', 'html')
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.limit('30mb'));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
global.config = require('./helpers/config').loadConfig();
// mongo
var dbConnString = process.env.MONGORILLA_MONGO_URL || process.env.MONGOLAB_URI || process.env.MONGOHQ_URL;
try {
console.log('Connecting to ' + dbConnString.replace(/^.*@/, '') + ' ...');
app.set('db', mongoose.connect(dbConnString, { db: { safe: true }}));
app.set('gfs', gridfs(app.get('db').connections[0].db, mongoose.mongo));
} catch (err) {
console.log(
'Impossible to connect to MongoDB.\n\n'
+ 'Make sure you have defined MONGO_URL or MONGORILLA_MONGO_URL env vars.');
return;
}
// models
var models_path = __dirname + '/models'
fs.readdirSync(models_path).forEach(function (file) {
if (file.match(/\.js$/)) {
require(models_path+'/'+file);
}
});
// inlcude helpers module
require('./helpers/string');
global.helpers = require('./helpers/crappy');
global.moment = moment; // date formatting library
global.getModel = require('./models/generic').getModel;
global.getRevisionModel = require('./models/revision').getModel;
// preload all the models set in the config file
config.collections.forEach(function (collection) {
getModel(collection.name);
});
// frontend
app.get('/', authRoute.bootstrap, appMainRoute.getIndex);
app.get('/auth/login', authRoute.bootstrap, authRoute.getLogin);
app.post('/auth/login', authRoute.postLogin);
app.post('/auth/logout', authRoute.postLogout);
app.get('/dashboard', authRoute.bootstrap, appMainRoute.getDashboard);
app.get('/add/:collectionName', authRoute.bootstrap, appGenericRoute.getAdd);
app.get('/search/:collectionName', authRoute.bootstrap, appGenericRoute.getSearch);
app.get('/edit/:collectionName/:objectId', authRoute.bootstrap, appGenericRoute.getEdit);
app.get('/preview/:collectionName/:objectId', authRoute.bootstrap, appGenericRoute.getPreview);
// dynamic javascript assets
app.get('/models/:collectionName.js', authRoute.bootstrap, appJsRoute.getModel);
app.get('/collections/:collectionName.js', authRoute.bootstrap, appJsRoute.getCollection);
app.get('/forms/:collectionName.js', authRoute.bootstrap, appJsRoute.getForm);
app.get('/config/:collectionName.json', authRoute.bootstrap, appJsRoute.getConfig);
// api gridfs interface
app.post('/api/fs.files', authRoute.bootstrap, apiFileRoute.post);
app.get('/api/fs.files/:objectId', authRoute.bootstrap, apiFileRoute.get);
app.get('/api/fs.files/:objectId/:view', authRoute.bootstrap, apiFileRoute.get);
app.del('/api/fs.files/:objectId', authRoute.bootstrap, apiFileRoute.del);
// api revision
app.get('/api/revision', authRoute.bootstrap, apiRevisionRoute.getList);
app.get('/api/:collectionName/:objectId/revisions', authRoute.bootstrap, apiRevisionRoute.getLatestList);
app.post('/api/:collectionName/:objectId/revisions', authRoute.bootstrap, apiRevisionRoute.post);
app.del('/api/:collectionName/:objectId/revisions/:revisionId', authRoute.bootstrap, apiRevisionRoute.del);
// api generic
app.get('/api/search/:collectionName', authRoute.bootstrap, apiGenericRoute.getSearch);
app.get('/api/:collectionName', authRoute.bootstrap, apiGenericRoute.getList);
app.post('/api/:collectionName', authRoute.bootstrap, apiGenericRoute.post);
app.get('/api/:collectionName/:objectId', authRoute.bootstrap, apiGenericRoute.get);
app.put('/api/:collectionName/:objectId', authRoute.bootstrap, apiGenericRoute.put);
app.del('/api/:collectionName/:objectId', authRoute.bootstrap, apiGenericRoute.del);
// expose config to the app local context
app.locals(global.config);
// frontend optimization
global.frontendBuilt = false;
fs.exists('./public/init-build.js', function (exsists) {
global.frontendBuilt = exsists;
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Mongorilla server listening on port ' + app.get('port') + ' on ' + app.get('env') + ' env.');
});