forked from glynnbird/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (108 loc) · 3.27 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
'use strict';
module.exports = function(opts) {
var express = require('express'),
app = module.exports = express(),
compression = require('compression'),
session = require('express-session'),
Cloudant = require('cloudant'),
bodyParser = require('body-parser'),
async = require('async'),
init = require('./lib/init'),
events = require('events'),
ee = new events.EventEmitter(),
auth = require('./lib/auth'),
morgan = require('morgan'),
cors = require('./lib/cors');
// Required environment variables
app.opts = require('./lib/env').getCredentials(opts);
var cloudant = new Cloudant(app.opts.couchHost),
dbName = app.dbName = app.opts.databaseName;
app.db = cloudant.db.use(dbName);
app.usersdb = cloudant.db.use('_users');
app.metaKey = 'com_cloudant_meta';
app.events = ee;
app.cloudant = cloudant;
app.serverURL = app.opts.couchHost;
app.auth = auth;
// Setup the logging format
if (app.opts.logFormat !== 'off') {
app.use(morgan(app.opts.logFormat));
}
function main() {
// Load custom middleware
if (app.opts.middleware.length) {
console.log('[OK] Loading middleware');
app.opts.middleware.forEach(function(m) {
app.use(m);
});
}
var production = (app.opts.production && app.opts.production === 'true');
if (app.opts.static) {
console.log('[OK] Serving out directory: ' + app.opts.static);
app.use('/', express.static(app.opts.static));
} else if (!production) {
// setup static public directory
app.use(express.static(__dirname + '/public'));
}
// enable cors
app.use(cors());
// gzip responses
app.use(compression());
app.use(bodyParser.json({ limit: '50mb'}));
app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' }));
// session support
if (opts && opts.sessionHandler) {
app.use(opts.sessionHandler)
} else {
console.log('[OK] Using default session handler');
app.use(session({
secret: app.metaKey,
resave: true,
saveUninitialized: true
}));
}
// plug in custom routes
if (app.opts.router) {
app.use(app.opts.router);
}
// load the routes
var router = require('./lib/routes/index');
app.use('/', router);
// Catch unknown paths
app.use(function(req, res, next) {
res.status(400).send({error: 'bad_request', reason: 'unknown path'})
});
// Error handlers
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send({error: 'internal_server_error', reason: 'server error'});
});
app.listen(app.opts.port);
}
// Make sure that any init stuff is executed before
// kicking off the app.
async.series(
[
init.verifyDB,
init.verifyBulkGet,
init.verifySecurityDoc,
init.installSystemViews,
auth.init
],
function (err, results) {
for (var result in results) {
if (results[result]) {
console.log(results[result]);
}
}
if (err != null) {
process.exit(1);
}
main();
ee.emit('listening');
console.log('[OK] main: Started app on', app.opts.url);
}
);
require("cf-deployment-tracker-client").track();
return app;
};