forked from ben-bradley/hapi-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (112 loc) · 2.97 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
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
var Hapi = require('hapi'), // for reasons
config = require('config'), // for app config
glob = require('glob'), // for dynamically reading plugins
args = require('argify'), // for command-line args
path = require('path'), //
Lout = require('lout'), // for API documentation
Good = require('good'), // for logging
GoodFile = require('good-file');
// load the config files in each plugin
var ENV = process.env.NODE_ENV || 'default';
glob.sync('./plugins/*/config/' + ENV + '.json').forEach(function (file) {
var pluginConfig = require(file);
if (Object.keys(pluginConfig).length !== 1)
throw new Error('Plugin config must have 1 top-level property: ' + file);
config.util.extendDeep(config, pluginConfig);
});
var server = new Hapi.Server({
connections: {
routes: {
cors: true
},
router: {
stripTrailingSlash: true
}
}
});
// init the API
server.connection({
port: config.api_port,
labels: ['api']
});
// init the UI
server.connection({
port: config.ui_port,
labels: ['ui']
});
// store the config for reference
server.app.config = config;
var excludes = (args.excludes) ? args.excludes.split(',') : false;
var includes = (args.includes) ? args.includes.split(',') : false;
var reporters = [];
// register each plugin and configure a Good reporter for each
glob.sync(__dirname + '/plugins/*/index.js').forEach(function (file) {
var plugin = require(file),
name = path.dirname(file).split('/').pop(),
load = true;
if (includes && includes.indexOf(name) === -1)
load = false;
else if (excludes && excludes.indexOf(name) > -1)
load = false;
if (load)
server.register({
register: plugin
}, {
routes: {
prefix: '/' + name
}
}, function (err) {
if (err)
throw new Error(err);
reporters.push({
reporter: GoodFile,
args: [
path.dirname(file) + '/logs/' + name + '.log', {
log: ['plugins', name]
}
]
});
if (!config.test)
console.log('Plugin loaded: ' + name);
});
else
console.log('Plugin NOT loaded: ' + name);
});
// register Lout for the /docs routes
server.register({
register: Lout
}, function (err) {
if (err)
throw new Error(err);
if (!config.test)
console.log('Plugin loaded: Lout');
});
// register Good for logging
if (reporters.length)
server.register({
register: Good,
options: {
opsInterval: 1000,
reporters: reporters
}
}, function (err) {
if (err)
throw new Error(err);
if (!config.test)
console.log('Plugin loaded: Good');
});
// In every environment except test, fire up the server
if (!config.test)
server.start(function (err) { // oink
if (err)
throw new Error(err);
console.log('Server started!');
server.connections.forEach(function (cn) {
console.log({
labels: cn.settings.labels,
uri: cn.info.uri
});
});
});
// Export the server for testing
module.exports = server;