-
Notifications
You must be signed in to change notification settings - Fork 120
/
server.js
192 lines (172 loc) · 5.5 KB
/
server.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Fetch the site configuration
var siteConf = require('./lib/getConfig');
process.title = siteConf.uri.replace(/http:\/\/(www)?/, '');
var airbrake;
if (siteConf.airbrakeApiKey) {
airbrake = require('airbrake').createClient(siteConf.airbrakeApiKey);
}
process.addListener('uncaughtException', function (err, stack) {
console.log('Caught exception: '+err+'\n'+err.stack);
console.log('\u0007'); // Terminal bell
if (airbrake) { airbrake.notify(err); }
});
var connect = require('connect');
var express = require('express');
var assetManager = require('connect-assetmanager');
var assetHandler = require('connect-assetmanager-handlers');
var notifoMiddleware = require('connect-notifo');
var DummyHelper = require('./lib/dummy-helper');
// Session store
var RedisStore = require('connect-redis')(express);
var sessionStore = new RedisStore;
var app = module.exports = express.createServer();
app.listen(siteConf.port, null);
// Setup socket.io server
var socketIo = new require('./lib/socket-io-server.js')(app, sessionStore);
var authentication = new require('./lib/authentication.js')(app, siteConf);
// Setup groups for CSS / JS assets
var assetsSettings = {
'js': {
'route': /\/static\/js\/[a-z0-9]+\/.*\.js/
, 'path': './public/js/'
, 'dataType': 'javascript'
, 'files': [
'http://code.jquery.com/jquery-latest.js'
, siteConf.uri+'/socket.io/socket.io.js' // special case since the socket.io module serves its own js
, 'jquery.client.js'
]
, 'debug': true
, 'postManipulate': {
'^': [
assetHandler.uglifyJsOptimize
, function insertSocketIoPort(file, path, index, isLast, callback) {
callback(file.replace(/.#socketIoPort#./, siteConf.port));
}
]
}
}
, 'css': {
'route': /\/static\/css\/[a-z0-9]+\/.*\.css/
, 'path': './public/css/'
, 'dataType': 'css'
, 'files': [
'reset.css'
, 'client.css'
]
, 'debug': true
, 'postManipulate': {
'^': [
assetHandler.fixVendorPrefixes
, assetHandler.fixGradients
, assetHandler.replaceImageRefToBase64(__dirname+'/public')
, assetHandler.yuiCssOptimize
]
}
}
};
// Add auto reload for CSS/JS/templates when in development
app.configure('development', function(){
assetsSettings.js.files.push('jquery.frontend-development.js');
assetsSettings.css.files.push('frontend-development.css');
[['js', 'updatedContent'], ['css', 'updatedCss']].forEach(function(group) {
assetsSettings[group[0]].postManipulate['^'].push(function triggerUpdate(file, path, index, isLast, callback) {
callback(file);
dummyHelpers[group[1]]();
});
});
});
var assetsMiddleware = assetManager(assetsSettings);
// Settings
app.configure(function() {
app.set('view engine', 'ejs');
app.set('views', __dirname+'/views');
});
// Middleware
app.configure(function() {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(assetsMiddleware);
app.use(express.session({
'store': sessionStore
, 'secret': siteConf.sessionSecret
}));
app.use(express.logger({format: ':response-time ms - :date - :req[x-real-ip] - :method :url :user-agent / :referrer'}));
app.use(authentication.middleware.auth());
app.use(authentication.middleware.normalizeUserData());
app.use(express['static'](__dirname+'/public', {maxAge: 86400000}));
// Send notification to computer/phone @ visit. Good to use for specific events or low traffic sites.
if (siteConf.notifoAuth) {
app.use(notifoMiddleware(siteConf.notifoAuth, {
'filter': function(req, res, callback) {
callback(null, (!req.xhr && !(req.headers['x-real-ip'] || req.connection.remoteAddress).match(/192.168./)));
}
, 'format': function(req, res, callback) {
callback(null, {
'title': ':req[x-real-ip]/:remote-addr @ :req[host]'
, 'message': ':response-time ms - :date - :req[x-real-ip]/:remote-addr - :method :user-agent / :referrer'
});
}
}));
}
});
// ENV based configuration
// Show all errors and keep search engines out using robots.txt
app.configure('development', function(){
app.use(express.errorHandler({
'dumpExceptions': true
, 'showStack': true
}));
app.all('/robots.txt', function(req,res) {
res.send('User-agent: *\nDisallow: /', {'Content-Type': 'text/plain'});
});
});
// Suppress errors, allow all search engines
app.configure('production', function(){
app.use(express.errorHandler());
app.all('/robots.txt', function(req,res) {
res.send('User-agent: *', {'Content-Type': 'text/plain'});
});
});
// Template helpers
app.dynamicHelpers({
'assetsCacheHashes': function(req, res) {
return assetsMiddleware.cacheHashes;
}
, 'session': function(req, res) {
return req.session;
}
});
// Error handling
app.error(function(err, req, res, next){
// Log the error to Airbreak if available, good for backtracking.
console.log(err);
if (airbrake) { airbrake.notify(err); }
if (err instanceof NotFound) {
res.render('errors/404');
} else {
res.render('errors/500');
}
});
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
// Routing
app.all('/', function(req, res) {
// Set example session uid for use with socket.io.
if (!req.session.uid) {
req.session.uid = (0 | Math.random()*1000000);
}
res.locals({
'key': 'value'
});
res.render('index');
});
// Initiate this after all other routing is done, otherwise wildcard will go crazy.
var dummyHelpers = new DummyHelper(app);
// If all fails, hit em with the 404
app.all('*', function(req, res){
throw new NotFound;
});
console.log('Running in '+(process.env.NODE_ENV || 'development')+' mode @ '+siteConf.uri);