-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcookie.go
More file actions
56 lines (47 loc) · 1.1 KB
/
cookie.go
File metadata and controls
56 lines (47 loc) · 1.1 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
package literoute
import (
"net/http"
"time"
)
var setCookieKVExpiration = time.Duration(8760) * time.Hour
type CookieOption func(*http.Cookie)
func CookiePath(path string) CookieOption {
return func(c *http.Cookie) {
c.Path = path
}
}
func CookieCleanPath(c *http.Cookie) {
c.Path = ""
}
func CookieExpires(durFromNow time.Duration) CookieOption {
return func(c *http.Cookie) {
c.Expires = time.Now().Add(durFromNow)
c.MaxAge = int(durFromNow.Seconds())
}
}
func CookieHTTPOnly(httpOnly bool) CookieOption {
return func(c *http.Cookie) {
c.HttpOnly = httpOnly
}
}
type (
CookieEncoder func(cookieName string, value interface{}) (string, error)
CookieDecoder func(cookieName string, cookieValue string, v interface{}) error
)
func CookieEncode(encode CookieEncoder) CookieOption {
return func(c *http.Cookie) {
newVal, err := encode(c.Name, c.Value)
if err != nil {
c.Value = ""
} else {
c.Value = newVal
}
}
}
func CookieDecode(decode CookieDecoder) CookieOption {
return func(c *http.Cookie) {
if err := decode(c.Name, c.Value, &c.Value); err != nil {
c.Value = ""
}
}
}