Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (133 loc) · 4.83 KB
/
Copy pathserver.js
File metadata and controls
147 lines (133 loc) · 4.83 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
import 'dotenv/config'
import Hapi from '@hapi/hapi'
import Inert from '@hapi/inert'
import Vision from '@hapi/vision'
import Crumb from '@hapi/crumb'
import Scooter from '@hapi/scooter'
import Blankie from 'blankie'
import nunjucks from 'nunjucks'
import {
createGovUkAnalyticsConsent,
govukAnalyticsConsentPlugin,
govukAnalyticsConsentTemplatePath
} from '../../dist/index.js'
import { exampleLanguage, languageCookieName, languageReturnUrl, languageView } from '../language.js'
// Standard Hapi setup: server, static files, and Nunjucks views.
const server = Hapi.server({ port: Number(process.env.PORT ?? 3000), host: 'localhost' })
await server.register([Inert, Vision, Scooter])
const env = new nunjucks.Environment(
new nunjucks.FileSystemLoader([
govukAnalyticsConsentTemplatePath(),
'node_modules/govuk-frontend/dist',
'examples/views'
])
)
server.views({
engines: {
njk: {
compile: (src, opts) => {
const template = new nunjucks.Template(src, env, opts.filename, true)
return (context) => template.render(context)
}
}
},
relativeTo: process.cwd(),
path: 'examples/views'
})
// Consent integration: describe the categories, tags, and cookies this service uses.
const consent = createGovUkAnalyticsConsent({
serviceName: 'Example service',
cookiesPageUrl: '/cookies',
auditEnabled: true,
categories: ['default', 'personalization'],
tags: ['google-analytics'],
gtmAllowlist: ['auto'],
cookies: [
{ name: 'session_id', categoryId: 'essential', purposeKey: 'govukAnalyticsConsent.cookies.session.purpose', expiry: { value: 'session' } },
{ name: 'crumb', categoryId: 'essential', purposeKey: 'govukAnalyticsConsent.cookies.csrf.purpose', expiry: { value: 'session' } },
{ name: languageCookieName, categoryId: 'essential', purposeKey: 'govukAnalyticsConsent.cookies.language.purpose', expiry: { value: 1, unit: 'year' } },
{
name: 'example_preferences',
categoryId: 'personalization',
purpose: 'Remembers your display preferences',
expiry: { value: 1, unit: 'year' }
}
],
getLanguage: (request) => exampleLanguage(request.headers.cookie),
getCsrfFormFields: (request) => ({ crumb: request.plugins.crumb })
})
// Host security middleware: merge consent's CSP sources and issue CSRF tokens before registration.
await server.register({
plugin: Blankie,
options: consent.blankieCsp({
generateNonces: true,
scriptSrc: ['self'],
connectSrc: ['self'],
imgSrc: ['self'],
fontSrc: ['self']
})
})
await server.register({
plugin: Crumb,
options: { cookieOptions: { isSecure: process.env.NODE_ENV === 'production' } }
})
// The consent plugin installs its routes and injects context into Hapi views.
await server.register({ plugin: govukAnalyticsConsentPlugin, options: consent })
// Ordinary service routes: language switching and GOV.UK assets belong to the host application.
server.route({
method: 'GET',
path: '/language/{language}',
handler: (request, h) => {
const language = request.params.language
if (language !== 'en' && language !== 'cy') {
return h.response().code(404)
}
return h.redirect(languageReturnUrl(request.query.returnUrl)).code(303).state(languageCookieName, language, {
path: '/',
isHttpOnly: true,
isSameSite: 'Lax',
isSecure: request.server.info.protocol === 'https',
ttl: 365 * 24 * 60 * 60 * 1000
})
}
})
server.route({
method: 'GET',
path: '/govuk-frontend/{param*}',
options: { auth: false },
handler: {
directory: {
path: 'node_modules/govuk-frontend/dist/govuk',
index: false
}
}
})
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
// Host handlers can use the request's consent state to select their own content.
const consent = request.app.govukAnalyticsConsent
const personalizationMessage = consent.isCategoryAccepted('personalization')
? 'Personalisation cookies are enabled. This page can use your saved display preferences.'
: consent.hasChoice
? 'Personalisation cookies are disabled. This page is using the default display settings.'
: 'You have not chosen your cookie preferences yet. This page is using the default display settings.'
return h.view('index.njk', {
cspNonce: request.plugins.blankie.nonces.script,
personalizationMessage,
...languageView(request.headers.cookie, `${request.url.pathname}${request.url.search}`)
})
}
})
server.route({
method: 'GET',
path: '/cookies',
// The host owns this route; the template renders the consent-provided cookies fragment.
handler: (request, h) => h.view('cookies.njk', {
cspNonce: request.plugins.blankie.nonces.script,
...languageView(request.headers.cookie, `${request.url.pathname}${request.url.search}`)
})
})
await server.start()
console.log(`Listening on ${server.info.uri}`)