-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie551.go
64 lines (49 loc) · 933 Bytes
/
cookie551.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
package cookie551
import (
"net/http"
"time"
)
type Cookie struct {
w http.ResponseWriter
r *http.Request
}
func New(w http.ResponseWriter, r *http.Request) *Cookie {
cookie := Cookie{
w: w,
r: r,
}
return &cookie
}
func (c *Cookie) Set(name, value string, second time.Duration) {
cookie := http.Cookie{
Name: name,
Value: value,
HttpOnly: true,
Path: "/",
Domain: c.r.URL.Host,
}
if second == 0 {
cookie.MaxAge = 0
} else {
expire := time.Now().Add(second * time.Second)
cookie.Expires = expire
}
http.SetCookie(c.w, &cookie)
}
func (c *Cookie) Get(name string) (string, error) {
cookie, err := c.r.Cookie(name)
if err != nil {
return "", err
}
return cookie.Value, nil
}
func (c *Cookie) Delete(name string) {
cookie := http.Cookie{
Name: name,
HttpOnly: true,
Path: "/",
Domain: c.r.URL.Host,
MaxAge: -1,
}
http.SetCookie(c.w, &cookie)
}