-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (170 loc) · 5.19 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Fetch the site configuration
var config = require('./config');
process.title = config.uri.replace(/http:\/\/(www)?/, '');
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 express = require('express');
//var assetManager = require('connect-assetmanager');
var mongoose = require('mongoose');
mongoose.connect(config.mongoUrl);
var Schema = require('./db/schema');
var RedisStore = require('connect-redis')(express);
// var sessionStore = new RedisStore(config.redisOptions);
var app = module.exports = express.createServer();
app.config = config;
app.Schema = Schema;
var Auth = require('./lib/auth');
app.listen(config.internal_port, null);
if (process.argv.length > 2 && process.argv[2] === '--init') {
console.log('Initializing DB');
require('./db/init')(app, function() {
console.log('DB initialized');
process.exit();
});
}
// var assetsSettings = {
// 'js': {
// 'route': /\/static\/js\/[^]+\.js/
// , 'path': './public/js/'
// , 'dataType': 'javascript'
// , 'files': [
// 'jquery-latest.js'
// // , siteConf.uri+'/socket.io/socket.io.js' // special case since the socket.io module serves its own js
// ]
// , 'debug': true
// /* , 'postManipulate': {
// '^': [
// assetHandler.uglifyJsOptimize
// , function insertSocketIoPort(file, path, index, isLast, callback) {
// callback(file.replace(/.#socketIoPort#./, siteConf.port));
// }
// ]
// }*/
// }
// , 'css': {
// 'route': /\/static\/css\/[^]+\.css/
// , 'path': './public/css/'
// , 'dataType': 'css'
// , 'files': [
// 'style.css'
// ]
// , 'debug': true
// /* , 'postManipulate': {
// '^': [
// assetHandler.fixVendorPrefixes
// , assetHandler.fixGradients
// , assetHandler.replaceImageRefToBase64(__dirname+'/public')
// , assetHandler.yuiCssOptimize
// ]
// }*/
// }
// };
//var assetsMiddleware = assetManager(assetsSettings);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.logger({format: ':response-time ms - :date - :req[x-real-ip] - :method :url :user-agent / :referrer'}));
// app.use(assetsMiddleware);
var stylus = require('stylus');
function compile(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', false);
}
app.use(stylus.middleware({
src: __dirname + '/public',
dest: __dirname + '/public',
compile: compile
}));
app.use(express.favicon());
app.use(express.session({
// 'store': sessionStore,
'secret': config.sessionSecret
}));
app.use(Auth.middleware());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
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'});
});
});
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) {
return req.session;
},
req: function(req) {
return req;
},
hasMessages: function(req) {
if (!req.session) return false;
return Object.keys(req.session.flash || {}).length;
},
messages: function(req) {
return function() {
var msgs = req.flash();
console.log('msgs: ', msgs);
return Object.keys(msgs).reduce(function(arr, type){
return arr.concat(msgs[type]);
}, []);
}
}
});
app.helpers({
staticPrefix: '',
appName: 'Wazapi'
});
// Error handling
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
app.error(function(err, req, res, next){
// Log the error to Airbreak if available, good for backtracking.
console.log(JSON.stringify(err));
if (err instanceof NotFound) {
res.render('errors/404');
} else {
res.render('errors/500');
}
});
// Routing
var routes = require('./routes');
app.get('/', routes.index);
app.get('/books/search', routes.books.search);
app.get('/books/popup/:bookId([0-9a-f]+)', routes.books.popup);
app.post('/books/rent/:bookId([0-9a-f]+)', routes.books.rent);
app.get('/books/tags/:tagId([0-9a-f]+)?', routes.books.tags.search);
app.post('/books/:bookId([0-9a-f]+)/comments', routes.books.comments.create);
Auth.helpExpress(app);
// If all fails, hit em with the 404
// This will be enabled when using assetManager
/*
app.all('*', function(req, res){
throw new NotFound;
});
*/
console.log('Running in ' + ( process.env.NODE_ENV || 'development' ) + ' mode @ ' + config.uri);