-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
101 lines (83 loc) · 2.2 KB
/
index.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
/**
* Initializes directories, installs dependencies then starts all
* configured servers for the Hoodie application
*/
var domain = require('domain')
var async = require('async')
var Hapi = require('hapi')
var config = require('./core/config.js')
var couch = require('./couchdb')
var hapi_plugins = require('./hapi_plugins')
var hooks = require('./core/hooks')
var installer = require('./couchdb/installer')
var plugins = require('./core/plugins')
var utils = require('./utils')
exports.start = function (options, callback) {
var app_domain = domain.create()
var env_config = config(options)
// wrap in top-level domain, otherwise we sometimes don't get uncaught
// exceptions printed in node 0.10.x
app_domain.run(exports.init.bind(null, env_config, callback))
// make sure we print a stack trace in node 0.10.x
app_domain.on('error', function (err) {
console.error(err.stack || err.toString())
process.exit(1)
})
}
var options = {
connections: {
routes: {
cors: {override: false},
payload: {maxBytes: 1048576 * 10} // 10 MB
}
}
}
exports.init = function (env_config, callback) {
if (env_config.debug) {
options.debug = {
log: ['error'],
request: ['error']
}
}
var server = new Hapi.Server(options)
async.applyEachSeries([
utils.ensurePaths,
utils.showConfigPath,
couch.start,
installer.install,
plugins.load,
hooks,
exports.configureServer.bind(null, server),
plugins.startAll,
utils.processSend
], env_config, function (err) {
if (err) return callback(err)
callback(null, server, env_config)
})
}
exports.configureServer = function (server, env_config, callback) {
env_config.hooks.runStatic('server.pack.pre', [server])
server.connection({
port: env_config.www_port,
labels: ['web']
})
server.connection({
port: env_config.admin_port,
labels: ['admin']
})
server.register(
hapi_plugins.map(function (plugin) {
return {
register: plugin,
options: {
app: env_config
}
}
}),
function (err) {
if (err) return callback(err)
env_config.hooks.runStatic('server.pack.post', [server])
callback(null)
}
)
}