-
Notifications
You must be signed in to change notification settings - Fork 5
/
option.go
90 lines (78 loc) · 1.89 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
package gonertia
import (
"fmt"
"io"
"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
}
}
// 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
i.ssrHTTPClient = &http.Client{}
return nil
}
}
// WithFlashProvider returns Option that will set Inertia's flash data provider.
func WithFlashProvider(flashData FlashProvider) Option {
return func(i *Inertia) error {
i.flash = flashData
return nil
}
}