-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
236 lines (192 loc) · 6.05 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Module dependencies.
*/
var
config = require('./config')
, rollbar = require('rollbar')
, util = require('util')
, express = require('express')
, session = require('express-session')
, fs = require('fs')
, hbs = require('hbs')
, crypto = require('crypto')
, forky = require('forky')
, utils = require('./utils')
, routes = require('./routes')
, helpers = require('./helpers')
, partials = require('./lib/partials')
, errors = require('./errors')
, middleware = require('./middleware')
, Models = require('./models')
, pkg = require('./package.json')
, odsChecker = require('./public/js/lib/order-delivery-service-checker')
;
hbs.handlebars = require('handlebars');
var app = module.exports = express();
app.set('view engine', 'hbs');
app.set('port', config.http.port || 3000);
app.use( middleware.logger() );
// Intercept status codes and render HTML if necessary
app.use( middleware.statusCodeIntercept() );
// If our request times out, something must be wrong with
// our server. Likely caught in some impossible condition,
// so let's just kill the worker
app.use( middleware.timeout({ timeout: config.http.timeout }) );
app.use(require('serve-favicon')(__dirname + '/public/favicon.ico'));
app.use(require('compression')());
app.use((function(){
var receiptReg = /receipts\/order\-\d+\.pdf/;
// Exclude receipts from this static server so we can do ?rebuild=true for
// receipts that exist in the local filesystem
return function(req, res, next) {
if (receiptReg.test(req.path)) return next();
return express.static(__dirname + '/public')(req, res, next);
};
})());
app.use( middleware.getUser2() );
app.use(require('body-parser').json({ limit: '200kb' }));
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(middleware.methodOverride);
app.use(middleware.logRequest());
app.use(middleware.uuid());
app.use(middleware.domains);
app.use(middleware.cors);
app.use(middleware.setSession());
app.use(middleware.storeUserAgent());
app.use( middleware.getRegions() );
app.use( middleware.consumeNewSignup() );
app.use( middleware.setUserRegion() );
// Using bitesquad's stuff without https
// if (config.isProduction || config.isStaging) {
// app.use(middleware.sslRedirect);
// }
app.use( function( req, res, next ){
res.locals.req = req;
return next();
});
app.use( require('dirac-middleware')({ envelope: false }) );
// app.use( require('./middleware/query-inspector')() );
app.use( function( req, res, next ){
res.locals.reqQuery = req.query;
return next();
});
// Re-register partials
if ( config.isDev ){
app.use( function( req, res, next ){
partials.register( hbs );
return next();
});
}
app.use( function( req, res, next ){
if ( req.route && req.route.path ){
req.logger.options.data.path = req.route.path;
}
next();
});
helpers.register(hbs);
partials.register(hbs);
routes.register(app);
if (config.rollbar) app.use(rollbar.errorHandler(config.rollbar.accessToken));
app.use( function logErrors( error, req, res, next ){
req.logger.error({
error: error instanceof Error ? utils.extend( {}
, errors.runtime.ERROR
, { message: error.message, stack: error.stack }
) : error
});
return next( error );
});
app.use( function clientErrors( error, req, res, next ){
if ( req.accepts(['html', 'json']) === 'html' ){
return next( error );
}
if ( 'httpCode' in error ){
return res.error( error );
}
res.error( errors.internal.UNKNOWN, error );
});
if ( process.env.NODE_ENV === 'production' ){
app.use( function renderErrors( error, req, res, next ){
if ( res.headersSent ){
return next( error );
}
if ( 'httpCode' in error ){
res.status( error.httpCode );
} else {
res.status(500);
}
res.render( 'error', {
error: error
, layout: 'layout/default'
});
});
}
app.use( function devErrors( error, req, res, next ){
// If instance of error, the default error handler will do just fine
if ( error instanceof Error || res.headersSent ){
return next( error );
}
res.status( error.httpCode || 500 );
try {
// Ensure we can stringify
JSON.stringify( error );
res.json( error );
} catch( e ) {
res.send( util.inspect( error ) );
}
});
app.use(require('@goodybag/scheduler-app')(config.postgresConnStr))
/**
* Request & Response prototype updates
*/
app.response.error = function(error, details, callback) {
utils.sendError(this, error, details);
if (callback) callback(error);
};
app.response.noContent = function() {
this.status(204).send('{}');
};
var render = app.response.render;
app.response.render = function(path, options, callback) {
var partialConfig = { intercomAppId: config.intercom.appId, phone: config.phone, emails: config.emails };
options = options || {};
options = utils.extend( options, {
config: utils.extend({}, partialConfig, options.config, config)
, session: this.req.session
, pkg: pkg
}
);
if (this.req.user && this.req.user.attributes && this.req.user.attributes.email) {
options.intercom = !config.intercom.enabled ? false : {
user_hash: crypto.createHmac('sha256', new Buffer(config.intercom.apiSecret, 'utf8')).update(this.req.user.attributes.email).digest('hex')
};
}
render.call(this, path, options, callback);
}
/**
* More readable app.all implementation for applying multiple
* middlewares to a group of routes
*
* app.before( m.restrict(), function(){
* app.get('/orders', ... );
* app.get('/orders/:id', ... );
* ...
* })
*/
app.before = function(){
var args = Array.prototype.slice.call( arguments );
var handler = args.pop();
var handle = function( verb, path ){
var _args = [ path ].concat( args, Array.prototype.slice.call( arguments, 2 ) );
return this[ verb ].apply( this, _args );
};
handler({
get: handle.bind( app, 'get' )
, post: handle.bind( app, 'post' )
, put: handle.bind( app, 'put' )
, patch: handle.bind( app, 'patch' )
, delete: handle.bind( app, 'delete' )
, all: handle.bind( app, 'all' )
})
};
utils.overload.config({ dataTypes: Models });