-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
63 lines (52 loc) · 1.55 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
const Hapi = require('hapi')
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 () {
this.app.config.set('web.server', 'hapi')
this.app.config.set('web.routes.files.relativeTo', this.app.config.get('main.paths.root'))
this.serverConfig = {
host: this.app.config.get('web.host'),
port: this.app.config.get('web.port'),
routes: this.app.config.get('web.routes')
}
}
/**
* Start Hapi Server
*/
async initialize () {
this.server = new Hapi.Server(this.serverConfig)
const { server, app } = this
await lib.Server.registerPlugins(server, app)
lib.Server.registerRoutes(this.app.config.web, server, app)
lib.Server.registerViews(this.app.config.web, server, app)
lib.Server.registerExtensions(this.app.config.web, server, app)
await this.server.start()
this.app.emit('webserver:http:ready', this.server.listener)
}
async unload () {
this.server.stop()
}
constructor (app, config) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}