/
options.go
138 lines (119 loc) · 2.77 KB
/
options.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
127
128
129
130
131
132
133
134
135
136
137
138
package router
import (
"net/url"
)
// Option is for 'functional options' to the New and Serve-methods
type Option func(*Router) error
// WithName assigns a prefix to all router (ex "/prefix")
func WithName(name string) Option {
return func(r *Router) error {
r.name = name
return nil
}
}
// WithPrefix assigns a prefix to all router (ex "/prefix")
func WithPrefix(path string) Option {
return func(r *Router) error {
if err := isValidProbePath(path); err != nil {
return err
}
r.prefix = path
return nil
}
}
// WithStrictSlash sets the StrictSlash-option on the gorilla/mux router
func WithStrictSlash(flag bool) Option {
return func(r *Router) error {
r.strictSlash = flag
return nil
}
}
// WithPort tells what port to listen on for requests
func WithPort(port int) Option {
return func(r *Router) error {
if port <= 0 {
return ErrorInvalidPort
}
r.port = port
return nil
}
}
// WithHealth sets the path (with leading /) that the health-probe should listen on
func WithHealth(path string) Option {
return func(r *Router) error {
if err := isValidProbePath(path); err != nil {
return err
}
r.healthPath = path
return nil
}
}
// Without204 removes the automatic health-probe from the router
func Without204() Option {
return func(r *Router) error {
r.skip204 = true
return nil
}
}
// WithoutHealth removes the automatic health-probe from the router
func WithoutHealth() Option {
return func(r *Router) error {
r.healthPath = ""
return nil
}
}
// WithReady sets the path (with leading /) that the ready-probe should listen on
func WithReady(path string) Option {
return func(r *Router) error {
if err := isValidProbePath(path); err != nil {
return err
}
r.readyPath = path
return nil
}
}
// WithoutReady removes the automatic ready-probe from the router
func WithoutReady() Option {
return func(r *Router) error {
r.readyPath = ""
return nil
}
}
// WithExposedErrors will send any panic-errors as request-body
func WithExposedErrors() Option {
return func(r *Router) error {
r.exposedErrors = true
return nil
}
}
// Error is when a router is unable to handle to handle options or requests
type Error int
// Errors for router-options
const (
ErrorRequireLeadingSlash Error = 1
ErrorNotValidURL Error = 2
ErrorInvalidPort Error = 3
)
func (err Error) Error() string {
switch err {
case ErrorRequireLeadingSlash:
return "leading '/' is required"
case ErrorNotValidURL:
return "not a valid url path"
case ErrorInvalidPort:
return "invalid port"
}
return "unknown router error"
}
func isValidProbePath(path string) error {
if path == "" {
return nil
}
if path[0] != '/' {
return ErrorRequireLeadingSlash
}
if _, err := url.Parse(path); err != nil {
return ErrorNotValidURL
}
return nil
}