This repository was archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebbox.js
More file actions
272 lines (231 loc) · 6.67 KB
/
Copy pathwebbox.js
File metadata and controls
272 lines (231 loc) · 6.67 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* global __dirname */
import Hapi from 'hapi';
import Path from 'path';
import Inert from 'inert';
import Good from 'good';
import Vision from 'vision';
import Jade from 'jade';
import Crumb from 'crumb';
import Blipp from 'blipp';
import HapiIO from './lib/io';
import HapiRateLimit from 'hapi-rate-limit';
import CatboxMemory from 'catbox-memory';
//import CatboxRedis from 'catbox-redis'; // see comment below
import Log from './lib/models/log';
import HapiPM2 from './lib/util/hapi-pm2';
import isString from 'lodash/isString';
// own imports
import config from './config/webbox.config';
import Package from './package.json';
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});
let isProvisioned = false;
// The default context is available for every response (template)
const defaultContext = {
webboxVersion: Package.version,
isProd: config.isProd,
isDev: config.isDev,
showHelp: config.app.showHelpInFooter,
baseTitle: config.app.baseTitle
};
// ToDo import depending on the environment
let cache;
if (config.isDev) {
cache = { engine: CatboxMemory };
} else {
// ToDo: add Redis for production
//cache = {
// engine: CatboxRedis,
/*database: config.cache.database,*/
// host: config.cache.host,
// port: config.cache.port,
// password: config.cache.password,
/*partition: config.cache.partition*/
//};
// ToDo: Change this, but for now we stick with a simple in memory cache
cache = { engine: CatboxMemory };
}
const server = new Hapi.Server({
host: config.app.hostname,
port: config.app.port,
routes: {
files: {
relativeTo: Path.join(__dirname, '')
}
},
cache: cache
});
const provision = async (shouldStart) => {
if (isProvisioned === true) {
console.warn('Already provisioned, called twiced!');
return;
}
await server.register({
plugin: HapiRateLimit,
options: config.ratelimit.cacheOptions
});
// Add PM2 shutdown integration for graceful reloads and shutdowns
await server.register({
plugin: HapiPM2,
options: {
timeout: 4000
}
});
// add the good process monitor/logging plugin
await server.register({
plugin: Good,
options: config.good,
});
// register crumb for csrf
await server.register({
plugin: Crumb,
options: {
cookieOptions: {
isSecure: config.crumb.isSecure // ToDo: Change this when dealing with SSL/HTTPS
}
}
});
await server.register(Vision);
server.views({
engines: {
jade: Jade
},
path: Path.join(__dirname, '/lib/views/'),
compileOptions: {
cache: true,
pretty: true,
debug: false,
compileDebug: false
},
context: defaultContext
});
// add WebSocket-Plugin
server.register({
plugin: HapiIO,
options: {
}
});
// Do the routing and auth stuff here
// add authentification with cookie and basic username/password
await server.register([Inert, require('./lib/auth/base.js'), require('hapi-auth-jwt2')]);
// Now register the jwt stuff too
server.auth.strategy('jwt', 'jwt', {
key: config.websocket.secret,
validate: async (decoded, request, h) => {
console.info('in validate func', decoded);
//callback(null, true);
return { isValid: true };
}
});
/**
* Set default strategy for every route
* Use route config to disable authentication
* with `auth: false`
* Additionally, each route is scoped only for admins,
* which can be also overriden.
*/
server.auth.default({
strategy: 'session',
scope: ['admin']
});
// Finally hook up the routes
// register routes
server.route(require('./lib/routes'));
// blibb for server routes -> only for dev
if (config.isDev) {
await server.register({
plugin: Blipp, options: {
showStart: config.blibb.showStart,
showAuth: config.blibb.showAuth
}
});
}
/**
* register named-routes-plugin
*
* This allows use to use "path.routename" in views as
* all named views are passed in the context.
*/
await server.register(require('hapi-named-routes'));
if (shouldStart === true) {
await server.start();
console.info(`Server started at ${server.info.uri}`);
}
isProvisioned = true;
};
// register better error pages
server.ext('onPreResponse', (request, h) => {
let user;
if (request.pre.user === undefined) {
user = {
isAnonymous: true
};
} else {
user = request.pre.user;
}
// ToDo: change this to hide information in production mode
if (!request.response.isBoom) {
return h.continue;
}
if (request.response.output.statusCode >= 500) {
console.info('Server error 500', 500);
const errorMessage = isString(request.response) ? request.repsonse : 'Server Error 500';
Log.createLog('Server.Error', errorMessage, {
path: request.path,
user: request.pre.user || {},
data: {
message: request.response.toString()
}
}, 'Error');
console.error(`Repsonse is Error`, request.response.stack);
} else {
console.log(`Repsonse is Error with status ${request.response.output.statusCode}`, request.response.stack);
}
// We should try to do some useful logging
let errorMessage = isString(request.response) ? request.repsonse : 'Server Error 500';
let errorStack = request.response.stack != undefined ? request.response.stack : 'No stack available';
//console.log('Error in request: %s', errorMessage, errorStack);
let err;
let errName;
let statusCode;
if (config.isProd) {
err = 'Das hätte nicht passieren sollen - Wir kümmern uns darum!';
errName = 'Fehler';
statusCode = request.response.output.statusCode;
} else {
err = 'Wir kümmern ums darum.';
errName = 'Fehler';
statusCode = request.response.output.statusCode;
}
if (statusCode === 403) {
err = 'Sie besitzen nicht die benötigten Rechte, um auf diese Seite zuzugreifen.';
}
return h.view('errors/default', {
statusCode: statusCode,
errName: errName,
errorMessage: err,
user: user
}).code(statusCode);
});
server.events.on({ name: 'request', channels: 'error' }, (request, event, tags) => {
try {
const error = event.response.source || {};
error._error = event.response. _error.toString();
error.stack = event.response._error.stack || '';
Log.createLog('Server.Error', 'Response Error', {
path: event.path,
user: event.auth,
error: error,
}, 'Error');
} catch (e) {
Log.createLog('Server.Error', 'Error while loggin server error', {
error: e.toString(),
}, 'Error');
}
});
module.exports = {
server,
provision
};