forked from trailsjs/trailpack-hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (68 loc) · 1.99 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
'use strict'
const Hapi = require('hapi')
const _ = require('lodash')
const ServerTrailpack = require('trailpack/server')
const lib = require('./lib')
/**
* Hapi Trailpack
*
* @class Hapi
* @see {@link http://trailsjs.io/doc/trailpack}
*
* Bind application routes to Hapi.js (from trailpack-router)
*/
module.exports = class HapiTrailpack extends ServerTrailpack {
/**
* Ensure that config/web is valid, and that no other competing web
* server trailpacks are installed (e.g. express)
*/
validate () {
//return lib.Validator.validateWebConfig(this.app.config.web)
}
configure () {
const webConfig = this.app.config.web
webConfig.plugins || (webConfig.plugins = [ ])
webConfig.extensions || (webConfig.extensions = [ ])
webConfig.options || (webConfig.options = { })
webConfig.server = 'hapi'
webConfig.views.relativeTo = this.app.config.main.paths.root
_.defaultsDeep(webConfig.options, {
host: webConfig.host,
port: webConfig.port,
routes: {
files: {
relativeTo: webConfig.views.relativeTo
}
}
})
}
/**
* Start Hapi Server
*/
initialize () {
this.webConfig = _.cloneDeep(this.app.config.web)
this.server = new Hapi.Server()
this.server.connection(this.webConfig.options)
return lib.Server.registerPlugins(this.webConfig, this.server, this.app)
.then(() => {
lib.Server.registerRoutes(this.webConfig, this.server, this.app)
lib.Server.registerViews(this.webConfig, this.server, this.app)
lib.Server.registerExtensions(this.webConfig, this.server, this.app)
lib.Server.nativeServer = this.server
return this.server.start()
})
.then(() => {
this.app.emit('webserver:http:ready', lib.Server.nativeServer.listener)
})
}
unload () {
this.server.stop()
}
constructor (app, config) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}