This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (102 loc) · 2.41 KB
/
server.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
if (process.env.NODE_ENV === 'production') {
try {
require('newrelic');
} catch (e) {
}
}
var Hapi = require('hapi'),
Scooter = require('scooter'),
Blankie = require('blankie'),
config = require('./config.js'),
getViewPath = config.getViewPath;
if (require.main === module) {
bootServer();
return;
}
module.exports = bootServer;
function bootServer (cb) {
var server = new Hapi.Server();
server.connection({
host: config.host,
port: config.port
});
server.views(config.server.views);
server.route({
path: '/static/{path*}',
method: 'GET',
handler: {
directory: {
path: __dirname + '/static/',
listing: false,
index: false
}
}
});
server.ext('onPreResponse', function (request, reply) {
var response = request.response,
code,
error,
msg;
if (!response.isBoom) {
return reply.continue();
}
error = response;
code = error.output.statusCode;
switch (code) {
case 404:
msg = '404 Page not found! So sad that this page does not exist :(';
break;
case 500:
msg = 'Something went wrong :(';
break;
}
return reply.view('error', {msg: msg}).code(code);
});
server.register([
Scooter,
{
register: Blankie,
options: {
defaultSrc: 'none',
scriptSrc: ['self', 'www.google-analytics.com'],
styleSrc: ['unsafe-inline', 'self', 'fonts.googleapis.com'],
imgSrc: '*',
connectSrc: 'self',
fontSrc: ['self', 'data:', 'fonts.gstatic.com']
}
},
{
register: require('./facets/about'),
options: getViewPath({
domain: config.domain,
eventname: config.eventname,
views: config.server.views
}, 'about')
},
{
register: require('./facets/submissions'),
options: getViewPath({
domain: config.domain,
eventname: config.eventname,
views: config.server.views
}, 'submissions')
},
{
register: require('./facets/tracking'),
options: {
googleAnalyticsUaCode: config.googleAnalyticsUaCode
}
},
{
register: require('./services/data'),
options: config.couch
}], function (err) {
if (err) {
throw err;
}
server.start(function () {
console.log('Hapi server started @ ' + server.info.uri);
cb && cb(server);
});
});
}