-
Notifications
You must be signed in to change notification settings - Fork 1
/
earlygrave.go
45 lines (39 loc) · 958 Bytes
/
earlygrave.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
package earlygrave
import (
"net/http"
)
type Filter func(r *http.Request) (*http.Request, error)
type ConfigFilter func(funct Filter) Filter
type Validate func(*http.Request) error
type Extract func(*http.Request) (*http.Request, error)
func identity(r *http.Request) (*http.Request, error) {
return r, nil
}
func New(cfgs ...ConfigFilter) Filter {
res := Filter(identity)
for _, decorator := range cfgs {
res = decorator(res)
}
return res
}
func ValidateParam(validate Validate) ConfigFilter {
return ConfigFilter(func(f Filter) Filter {
return Filter(func(r *http.Request) (*http.Request, error) {
if err := validate(r); err != nil {
return r, err
}
return f(r)
})
})
}
func ExtractParam(extract Extract) ConfigFilter {
return ConfigFilter(func(f Filter) Filter {
return Filter(func(r *http.Request) (*http.Request, error) {
nr, err := extract(r)
if err != nil {
return r, err
}
return f(nr)
})
})
}