/
index.js
49 lines (37 loc) · 992 Bytes
/
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
'use strict';
var Hoek = require('hoek');
function createServer(plugin, config, callback) {
var Hapi = require('hapi');
var server = new Hapi.Server(config.options);
server.connection({
host: config.host,
port: config.port
});
server.register(plugin, function (err) {
if (err) {
return callback(err);
}
callback(null, server);
});
}
exports.create = function create(pluginModule, config, callback) {
Hoek.assert(pluginModule.exports, 'Must provide the plugin module object');
Hoek.assert(typeof config === 'object', 'Must provide a server config object');
var doneCalled = false;
function done(err, server) {
if (doneCalled) {
return;
}
doneCalled = true;
if (callback) {
callback(err, server);
} else if (server) {
server.start();
}
}
if (pluginModule.parent) {
// Do nothing since the plugin is required
return done();
}
createServer(pluginModule.exports, config, done);
};