-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
120 lines (104 loc) · 2.9 KB
/
main.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
/**
* api
*
* @author Simon Townsend <stownsend@unifiedsocial.com>
*/
var conf = require('./conf'),
express = require('express'),
app = express(),
_ = require('lodash'),
async = require('async'),
RedisStore = require('connect-redis')(express),
https = require('https'),
http = require('http'),
Resource = require('express-resource-plus')(app),
Cluster = require('./lib/cluster'),
versions;
/* Globals */
// all globals exposed via the express.locals api
global.app = app;
app.locals.conf = conf;
if (!app.hasOwnProperty('db')) {
app.locals.db = require('./models')(conf);
}
app.locals.Logger = require('./lib/logger');
app.locals.appPath = __dirname;
/* Dependencies */
var middleware = require('./lib/middleware');
/* Configuration */
// all environments
app.configure(function(){
app.set('app_dir', __dirname + '/app');
});
// test only
app.configure('test', function(){
});
// development only
app.configure('development', function(){
});
// production only
app.configure('production', function(){
});
/* Middleware */
app.use(express.favicon());
app.use(express.logger({ immediate : conf.logger.web.immediate,
format : conf.logger.web.format }));
app.use(express.bodyParser());
app.use(express.methodOverride());
//app.use(middleware.mergeParams);
app.use(express.cookieParser(conf.session.secret));
app.use(express.session({ store: new RedisStore({ host : conf.redis.host,
port : conf.redis.port,
pass : conf.redis.pass
}), secret: conf.session.secret }));
//app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.use(middleware.cors);
if(conf.useErrorHandler) app.use(express.errorHandler());
/* Routes */
versions = {'Version 1': '/v1',
'Version 2': '/v2'};
// route to display versions
app.get('/', function(req, res) {
res.json(versions);
});
// import and apply the routes
for (var k in versions) {
route = require('./app' + versions[k] + '/routes');
route.init(versions[k], conf);
}
app.locals.db.sequelize.sync().complete(function(err) {
if (err) {
throw err;
} else {
if (isProduction()) { // only run clustered in prod
new Cluster()
.worker(function () {
startServer();
});
}
else {
startServer();
}
}
});
/* misc */
function isProduction() {
if ('production' == app.get('env')) {
return true;
}
return false;
}
function isTest() {
if ('test' == app.get('env')) {
return true;
}
return false;
}
/* Initialization */
function startServer() {
http.createServer(app).listen(conf.server.port.http, function(){
console.log('Express server listening on port ' + conf.server.port.http);
});
}
exports = module.exports = app;