-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
129 lines (102 loc) · 3.81 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
var express = require('express')
, app = express()
, log = require('metalogger')()
, cluster = require('cluster')
, CONF = require('config')
, http = require('http');
exports = module.exports;
exports.setup = function(initapp, callback) {
// Default to configuration value, but let Heroku/others override via env
var server_port = CONF.app.port;
// Support for Heroku. Heroku expects Node apps to start on $PORT
if ('undefined' !== typeof process.env.PORT && process.env.PORT) {
server_port = process.env.PORT;
}
if (typeof callback !== 'undefined' && initapp) {
app = initapp;
} else if(typeof callback === 'undefined') {
// This is to support old clients who do not
// know about the "initapp" parameter and are
// only passing callback, through.
callback = initapp;
} else {
// remaining condition:
// if initapp is false but is actually passed
// the right thing to do is to ignore it.
}
configure_logging();
var isClusterMaster = (cluster.isMaster && (process.env.NODE_CLUSTERED == 1));
var is_http_thread = true;
if (isClusterMaster ||
( 'undefined' !== typeof process.env.NODE_ISNOT_HTTP_SERVER_THREAD &&
process.env.NODE_ISNOT_HTTP_SERVER_THREAD != 'true')) {
is_http_thread = false;
}
log.debug("is current thread a HTTP thread? " + is_http_thread);
if (isClusterMaster) {
require('nodebootstrap-clustering').setup();
}
if (is_http_thread) {
http = http.createServer(app);
http.listen(server_port);
}
// If we are not running a cluster at all:
if (!isClusterMaster && cluster.isMaster) {
log.notice("Express server instance listening on port " + server_port);
}
module.parent.exports.setAppDefaults(app);
app.http = http; // Expose the original http object, for socket.io support or other needs.
callback(app);
};
/**
* Setup for the testing framework of nodebootstrap
* Does not include clustering as this is not usually needed for endpoint testing
* @param initapp
* @param callback
*/
exports.setupTest = function(initapp, callback) {
var app = initapp || express();
configure_logging();
var server = http.createServer(app);
module.parent.exports.setAppDefaults(app);
app.http = server;
callback(app);
};
/**
* Setting up sensible default configurations
* @param initapp optional. You can pass-in the app that should be configured.
*/
module.parent.exports.setAppDefaults = function(initapp) {
var someapp = initapp || express();
// var root_dir = require('path').dirname(module.parent.filename);
var root_dir = require('path').dirname(require.main.filename);
var defaultLimit = '50mb';
var bodyParser = require('body-parser');
// parse application/x-www-form-urlencoded
someapp.use(bodyParser.urlencoded({extended: true, limit: defaultLimit }));
// parse application/anything+json
someapp.use(bodyParser.json({ type: 'application/*+json', limit: defaultLimit }));
// parse application/json
someapp.use(bodyParser.json({ type: 'application/json', limit: defaultLimit }));
// parse text/plain
someapp.use(bodyParser.text({ type: 'text/plain', limit: defaultLimit }));
// parse anything else
someapp.use(bodyParser.raw({ limit: defaultLimit }));
someapp.use(require('connect-multiparty')());
someapp.use(require('method-override')('X-HTTP-Method-Override'));
if (typeof initapp === 'undefined') return someapp;
}
/**
* Default configuration of logging
*/
function configure_logging() {
if ('log' in CONF) {
if ('plugin' in CONF.log) { process.env.NODE_LOGGER_PLUGIN = CONF.log.plugin; }
if ('level' in CONF.log) { process.env.NODE_LOGGER_LEVEL = CONF.log.level; }
if ('customlevels' in CONF.log) {
for (var key in CONF.log.customlevels) {
process.env['NODE_LOGGER_LEVEL_' + key] = CONF.log.customlevels[key];
}
}
}
}