-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
118 lines (101 loc) · 3.04 KB
/
app.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
'use strict'
require('./globals')
const Hapi = require('hapi')
const Path = require('path')
const Config = util('config')
const Laabr = require('laabr')
const Inert = require('inert')
const Vision = require('vision')
const Globby = require('globby')
const Handlebars = require('./views')
// configure hapi response logging format
Laabr.format('log', ':time :level :message')
class Bootstrap {
async launchWeb() {
// create new web instance
// add web’s connection information
const web = new Hapi.Server({
host: 'localhost',
port: Config.get('app.port')
})
// register plugins to web instance
await web.register([
Inert,
Vision,
{
plugin: require('hapi-dev-errors'),
options: {
showErrors: Config.get('app.env') !== 'production',
useYouch: true
}
},
{
plugin: Laabr.plugin,
options: {
colored: true,
hapiPino: {
logPayload: false
}
}
}
])
this.configureViews(web)
const middlewarePath = Path.resolve(__appRoot, 'boost', 'middleware')
const boostMiddlewares = await this.loadHapiPluginsFromFolder(middlewarePath)
await web.register(boostMiddlewares)
// register Web plugins created by the user
const userWebPluginsPath = Path.resolve(__appRoot, 'app', 'web')
const userWebPlugins = await this.loadHapiPluginsFromFolder(userWebPluginsPath)
await web.register(userWebPlugins)
await web.start()
}
async loadHapiPluginsFromFolder(path) {
const plugins = await Globby(path, {
expandDirectories: {
files: ['index'],
extensions: ['js'],
filesOnly: true
}
})
return plugins
.filter(item => {
const plugin = require(item)
return this.isHapiPlugin(plugin)
})
.map(item => require(item))
}
isHapiPlugin(plugin) {
return plugin && plugin.plugin
}
configureViews(server) {
// view configuration
const viewsPath = Path.resolve(__appRoot, 'resources', 'views')
server.views({
engines: {
hbs: Handlebars
},
path: [viewsPath, Path.resolve(viewsPath, 'vendor', 'boost')],
layoutPath: [Path.resolve(viewsPath, 'layouts'), Path.resolve(viewsPath, 'vendor', 'boost', 'layouts')],
layout: 'default',
helpersPath: [Path.resolve(viewsPath, 'helpers'), Path.resolve(viewsPath, 'vendor', 'boost', 'helpers')],
partialsPath: [Path.resolve(viewsPath, 'partials'), Path.resolve(viewsPath, 'vendor', 'boost', 'partials')],
isCached: Config.get('app.env') === 'production',
context: {
title: Config.get('app.name')
}
})
}
async fireOff() {
try {
await Promise.all([this.launchWeb()])
} catch (err) {
console.error(err)
process.exit(1)
}
}
}
process.on('unhandledRejection', error => {
console.log(error)
process.exit(1)
})
module.exports = new Bootstrap()