-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
98 lines (80 loc) · 2.31 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
var Hapi = require('hapi');
var fs = require('fs');
var good = require('good');
var path = require('path');
var env = process.env.NODE_ENV;
if(!env) throw "Please set NODE_ENV variable.";
const getConfigFile = function () {
try {
// Try to load the user's personal configuration file
return require(process.cwd() + '/conf.my.' + env + '.json');
} catch (e) {
// Else, read the default configuration file
return require(process.cwd() + '/conf.' + env + '.json');
}
}
const startServer = function(cluster){
var conf = getConfigFile();
var server = new Hapi.Server();
var tls = undefined;
if(conf.tls && conf.tls.key && conf.tls.cert){
tls = {
key: fs.readFileSync(conf.tls.key),
cert: fs.readFileSync(conf.tls.cert)
};
}
server.connection({
host: conf.host,
port: conf.port,
tls: tls
});
var plugins = [];
Object.keys(conf.plugins).forEach(function(pluginName){
var plugin = {};
plugin.register = require(pluginName);
plugin.options = conf.plugins[pluginName];
plugins.push(plugin);
});
plugins.push({
register: good,
options: {
reporters: {
myConsoleReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*' }]
},
{
module: 'good-console'
}, 'stdout'],
myFileReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ ops: '*' }]
}, {
module: 'good-squeeze',
name: 'SafeJson'
}, {
module: 'good-file',
args: ['all.log']
}]
}
}
});
server.method({
name: 'getCluster',
method: function(){
return cluster;
},
options: {}
});
server.register(plugins, function(err){
if (err) {
throw err; // something bad happened loading the plugin
}
});
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
}
startServer();