This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
internal.js
105 lines (88 loc) · 2.88 KB
/
internal.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Hapi = require('hapi');
const AppError = require('../error');
const auth = require('../auth');
const config = require('../config').getProperties();
const env = require('../env');
const logger = require('../logging')('server.clients');
const hapiLogger = require('../logging')('server.hapi');
const summary = require('../logging/summary');
exports.create = function createServer() {
var isProd = env.isProdLike();
var server = new Hapi.Server(
require('./config')
);
server.connection({
host: config.serverInternal.host,
port: config.serverInternal.port
});
server.auth.scheme(auth.AUTH_SCHEME, auth.strategy);
server.auth.strategy(auth.AUTH_STRATEGY, auth.AUTH_SCHEME);
if (config.hpkpConfig && config.hpkpConfig.enabled) {
var hpkpOptions = {
maxAge: config.hpkpConfig.maxAge,
sha256s: config.hpkpConfig.sha256s,
includeSubdomains: config.hpkpConfig.includeSubDomains
};
if (config.hpkpConfig.reportUri){
hpkpOptions.reportUri = config.hpkpConfig.reportUri;
}
if (config.hpkpConfig.reportOnly){
hpkpOptions.reportOnly = config.hpkpConfig.reportOnly;
}
server.register({
register: require('hapi-hpkp'),
options: hpkpOptions
}, function (err) {
if (err) {
throw err;
}
});
}
var routes = require('../routing').clients;
if (isProd) {
logger.info('prod', 'Disabling response schema validation');
routes.forEach(function(route) {
delete route.config.response;
});
}
// default to stricter content-type
routes.forEach(function(route) {
var method = route.method.toUpperCase();
if (method !== 'GET' && method !== 'HEAD') {
if (!route.config.payload) {
route.config.payload = {
allow: ['application/json', 'application/x-www-form-urlencoded']
};
}
logger.verbose('route.payload', {
path: route.path,
method: method,
payload: route.config.payload
});
}
});
server.route(routes);
// hapi internal logging: server and request
server.on('log', function onServerLog(ev, tags) {
if (tags.error && tags.implementation) {
hapiLogger.critical('error.uncaught', { tags: ev.tags, error: ev.data });
}
});
server.on('request', function onRequestLog(req, ev, tags) {
if (tags.error && tags.implementation) {
hapiLogger.critical('error.uncaught', { tags: ev.tags, error: ev.data });
}
});
server.ext('onPreResponse', function onPreResponse(request, next) {
var response = request.response;
if (response.isBoom) {
response = AppError.translate(response);
}
summary(request, response);
next(response);
});
return server;
};