-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
96 lines (81 loc) · 1.98 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
package mux
import (
"net/http"
"golang.org/x/text/language"
)
// Option represents a functional option for configuration.
type Option func(*Handler)
// WithRouter sets the request router.
func WithRouter(router Router) Option {
return func(h *Handler) {
h.router = router
}
}
// WithLocales sets the supported language tags.
func WithLocales(tags []language.Tag) Option {
return func(h *Handler) {
h.locales = newLocaleMatcher(tags)
}
}
// WithDecoder sets the decoder negotiation function.
func WithDecoder(fn DecoderFunc) Option {
return func(h *Handler) {
h.decoder = fn
}
}
// WithEncoder sets the encoder negotiation function.
func WithEncoder(fn EncoderFunc) Option {
return func(h *Handler) {
h.encoder = fn
}
}
// WithResolver sets the error resolver.
func WithResolver(resolver Resolver) Option {
return func(h *Handler) {
h.resolver = resolver
}
}
// WithPool sets the buffer pool for encoding responses.
func WithPool(pool Pool) Option {
return func(h *Handler) {
h.pool = pool
}
}
// WithLogger sets the logger.
func WithLogger(logger Logger) Option {
return func(h *Handler) {
h.log = logger
}
}
// WithObserver sets the observer.
func WithObserver(observer Observer) Option {
return func(h *Handler) {
h.observer = observer
}
}
// RouteOption represents a functional option for configuration.
type RouteOption func(*Route)
// WithName sets the route name.
func WithName(name string) RouteOption {
return func(r *Route) {
r.name = name
}
}
// WithMethod sets the methods for which the route is valid.
func WithMethod(method ...string) RouteOption {
return func(r *Route) {
for _, m := range method {
r.methods[m] = struct{}{}
}
_, ok := r.methods[http.MethodGet]
if ok {
r.methods[http.MethodHead] = struct{}{}
}
}
}
// WithMiddleware appends middleware to the middleware stack.
func WithMiddleware(middleware ...func(http.Handler) http.Handler) RouteOption {
return func(r *Route) {
r.middleware = append(r.middleware, middleware...)
}
}