-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute_options.go
More file actions
65 lines (54 loc) · 1.61 KB
/
route_options.go
File metadata and controls
65 lines (54 loc) · 1.61 KB
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
package rip
import (
"log/slog"
"github.com/dolanor/rip/encoding"
)
type StatusMap map[error]int
type entityRouteConfig struct {
codecs encoding.Codecs
logger *slog.Logger
middlewares []Middleware
statusMap StatusMap
listPageSize int
listPageSizeMax int
}
// EntityRouteOption is the optional configuration for a [EntityRoute].
type EntityRouteOption func(cfg *entityRouteConfig)
// WithEntityRouteLogger configures the logger used for this route.
func WithEntityRouteLogger(logger *slog.Logger) EntityRouteOption {
return func(cfg *entityRouteConfig) {
cfg.logger = logger
}
}
// WithCodecs configures the encoding available for this route.
func WithCodecs(codecs ...encoding.Codec) EntityRouteOption {
return func(cfg *entityRouteConfig) {
if cfg.codecs.Codecs == nil {
cfg.codecs.Codecs = map[string]encoding.Codec{}
}
for _, c := range codecs {
cfg.codecs.Register(c)
}
}
}
// WithErrors maps errors with an HTTP status code for this route.
func WithErrors(statusMap StatusMap) EntityRouteOption {
return func(cfg *entityRouteConfig) {
cfg.statusMap = statusMap
}
}
// WithListPage configures the number of entities displayed in a list page for this route.
func WithListPage(size int, max int) EntityRouteOption {
return func(cfg *entityRouteConfig) {
cfg.listPageSize = size
cfg.listPageSizeMax = max
}
}
// WithMiddleware configures the middlewares for this route.
func WithMiddlewares(middlewares ...Middleware) EntityRouteOption {
return func(cfg *entityRouteConfig) {
for _, m := range middlewares {
cfg.middlewares = append(cfg.middlewares, m)
}
}
}