-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
126 lines (113 loc) · 2.96 KB
/
option.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
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
// Copyright 2022 - Offen Authors <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package consent
import (
"fmt"
"html/template"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// Option is a function used to configured the server.
type Option func(*handler) error
// WithLogger overrides the server's default logger with the given
// implementation.
func WithLogger(l *log.Logger) Option {
return func(s *handler) error {
s.logger = l
return nil
}
}
// WithCookieName sets the name of the cookie that is used for storing
// user's consent decisions
func WithCookieName(n string) Option {
return func(s *handler) error {
s.cookieName = n
return nil
}
}
// WithCookiePath sets the Path attribute used when setting cookie headers.
func WithCookiePath(p string) Option {
return func(s *handler) error {
s.cookiePath = p
return nil
}
}
// WithCookieDomain sets the Domain attribute used when setting cookie headers.
func WithCookieDomain(d string) Option {
return func(s *handler) error {
s.cookieDomain = d
return nil
}
}
// WithCookieTTL defines the expected lifetime of a cookie.
func WithCookieTTL(d time.Duration) Option {
return func(s *handler) error {
s.cookieTTL = d
return nil
}
}
// WithCookieSecure defines whether used cookies are using the Secure attribute
func WithCookieSecure(a bool) Option {
return func(s *handler) error {
s.cookieSecure = a
return nil
}
}
// WithCustomizedWording passes custom copy to be used in the default consent UI
func WithCustomizedWording(copy, yes, no string) Option {
return func(s *handler) error {
s.templateData.Wording.Paragraph = copy
s.templateData.Wording.Yes = yes
s.templateData.Wording.No = no
return nil
}
}
// WithStylesheet adds a stylesheet that is injected into the iframe element.
func WithStylesheet(loc string) Option {
return func(s *handler) error {
if loc == "" {
return nil
}
b, err := os.ReadFile(loc)
if err != nil {
return fmt.Errorf("WithCustomStyles: error reading given file: %w", err)
}
css := template.CSS(string(b))
s.templateData.Styles = &css
return nil
}
}
// WithTemplatesDirectory configures the server to look for custom templates
// in the given location.
func WithTemplatesDirectory(dir string) Option {
return func(s *handler) error {
if dir == "" {
return nil
}
templates := map[string]template.HTML{}
if err := filepath.WalkDir(dir, func(path string, di fs.DirEntry, err error) error {
if di.IsDir() {
return nil
}
if filepath.Ext(path) != ".html" {
return nil
}
b, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("WithTemplatesDirectory: error reading file %s: %w", path, err)
}
id := filepath.Base(path)
id = strings.TrimSuffix(id, ".html")
templates[id] = template.HTML(string(b))
return nil
}); err != nil {
return fmt.Errorf("WithTemplatesDirectory: error walking directory %s: %w", dir, err)
}
s.templateData.CustomTemplates = &templates
return nil
}
}