-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
94 lines (80 loc) · 2.04 KB
/
server.go
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
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
// SPDX-License-Identifier: Apache-2.0
package consent
import (
_ "embed"
"fmt"
"html/template"
"io"
"log"
"net/http"
"time"
)
type handler struct {
logger *log.Logger
cookieName string
cookieDomain string
cookiePath string
cookieTTL time.Duration
cookieSecure bool
tpl *template.Template
templateData templateData
clientScript []byte
}
type templateData struct {
Script *template.JS
Styles *template.CSS
Wording wording
CustomTemplates *map[string]template.HTML
}
type wording struct {
Paragraph string
Yes string
No string
}
// NewHandler returns a http.Handler that serves the consent server configured
// to use the the given options.
func NewHandler(options ...Option) (http.Handler, error) {
s, err := newDefaultHandler()
if err != nil {
return nil, err
}
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
return s, nil
}
//go:embed proxy/index.go.html
var proxyHostTemplate string
//go:embed proxy/proxy.js
var proxyScript string
//go:embed client/client.js
var clientScript string
func newDefaultHandler() (*handler, error) {
tpl := template.New("proxy")
if _, err := tpl.Parse(string(proxyHostTemplate)); err != nil {
return nil, fmt.Errorf("newDefaultServer: error parsing template: %w", err)
}
minifiedProxyScript, err := minifyJS(proxyScript)
if err != nil {
return nil, fmt.Errorf("newDefaultServer: error minifying proxy script: %w", err)
}
safeScript := template.JS(minifiedProxyScript)
minifiedClientScript, err := minifyJS(clientScript)
if err != nil {
return nil, fmt.Errorf("newDefaultServer: error minifying client script: %w", err)
}
return &handler{
logger: log.New(io.Discard, "", log.Ldate),
cookieName: defaultConsentCookieName,
cookieSecure: defaultCookieSecure,
cookieTTL: defaultCookieTTL,
clientScript: []byte(minifiedClientScript),
tpl: tpl,
templateData: templateData{
Script: &safeScript,
},
}, nil
}