-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoption.go
118 lines (102 loc) · 2.69 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
package gonertia
import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
)
// Option is an option parameter that modifies Inertia.
type Option func(i *Inertia) error
// WithVersion returns Option that will set Inertia's version.
func WithVersion(version string) Option {
return func(i *Inertia) error {
i.version = md5(version)
return nil
}
}
// WithVersionFromFileFS returns Option that will set Inertia's version based on file checksum from rootFS.
func WithVersionFromFileFS(rootFS fs.FS, path string) Option {
return func(i *Inertia) (err error) {
i.version, err = md5FileFromFS(rootFS, path)
if err != nil {
return fmt.Errorf("calculating md5 hash of manifest file: %w", err)
}
return nil
}
}
// WithVersionFromFile returns Option that will set Inertia's version based on file checksum.
func WithVersionFromFile(path string) Option {
return func(i *Inertia) (err error) {
i.version, err = md5File(path)
if err != nil {
return fmt.Errorf("calculating md5 hash of manifest file: %w", err)
}
return nil
}
}
// WithJSONMarshaller returns Option that will set Inertia's JSON marshaller.
func WithJSONMarshaller(jsonMarshaller JSONMarshaller) Option {
return func(i *Inertia) error {
i.jsonMarshaller = jsonMarshaller
return nil
}
}
// WithLogger returns Option that will set Inertia's logger.
func WithLogger(logs ...Logger) Option {
var l Logger
if len(logs) > 0 {
l = logs[0]
} else {
l = log.Default()
}
if l == nil {
l = log.New(io.Discard, "", 0)
}
return func(i *Inertia) error {
i.logger = l
return nil
}
}
// WithContainerID returns Option that will set Inertia's container id.
func WithContainerID(id string) Option {
return func(i *Inertia) error {
i.containerID = id
return nil
}
}
// WithSSR returns Option that will enable server side rendering on Inertia.
func WithSSR(url ...string) Option {
return func(i *Inertia) error {
var u string
if len(url) > 0 {
u = url[0]
} else {
const defaultURL = "http://127.0.0.1:13714"
u = defaultURL
}
i.ssrURL = u
return nil
}
}
// WithSSRHTTPClient returns Option that will set Inertia's SSR http client.
func WithSSRHTTPClient(ssrHTTPClient *http.Client) Option {
return func(i *Inertia) error {
i.ssrHTTPClient = ssrHTTPClient
return nil
}
}
// WithFlashProvider returns Option that will set Inertia's flash data provider.
func WithFlashProvider(flash FlashProvider) Option {
return func(i *Inertia) error {
i.flash = flash
return nil
}
}
// WithEncryptHistory returns Option that will enable Inertia's global history encryption.
func WithEncryptHistory(encryptHistory ...bool) Option {
return func(i *Inertia) error {
i.encryptHistory = firstOr[bool](encryptHistory, true)
return nil
}
}