-
Notifications
You must be signed in to change notification settings - Fork 8
/
header.go
43 lines (36 loc) · 1.04 KB
/
header.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
package filter
import (
"strconv"
"time"
"github.com/cuigh/auxo/net/web"
)
// Header is a filter which can inject headers to response.
type Header map[string]string
func (h Header) Apply(next web.HandlerFunc) web.HandlerFunc {
return func(ctx web.Context) error {
for key, value := range h {
ctx.SetHeader(key, value)
}
return next(ctx)
}
}
// DynamicHeader is a filter which can inject dynamic headers to response.
type DynamicHeader map[string]func() string
// NewCacheHeader return a Header instance which adds `Expires` and `Cache-Control` headers.
func NewCacheHeader(expiry time.Duration) DynamicHeader {
maxAge := "max-age=" + strconv.Itoa(int(expiry.Seconds()))
return DynamicHeader{
web.HeaderExpires: func() string {
return time.Now().Add(expiry).Format(time.RFC1123)
},
web.HeaderCacheControl: func() string { return maxAge },
}
}
func (h DynamicHeader) Apply(next web.HandlerFunc) web.HandlerFunc {
return func(ctx web.Context) error {
for key, fn := range h {
ctx.SetHeader(key, fn())
}
return next(ctx)
}
}