-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.go
More file actions
42 lines (35 loc) · 828 Bytes
/
cookie.go
File metadata and controls
42 lines (35 loc) · 828 Bytes
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
package main
import (
"net/http"
"time"
)
// SetCookie will set cookies in the user browser
func setCookie(w http.ResponseWriter, v string) {
// Set expiration time
expiration := time.Now()
expiration = time.Now().Add(time.Hour * time.Duration(1))
// Generate cookie
cookie := http.Cookie{Name: "grafana_session", Value: v, Path: "/", Expires: expiration}
// Set cookie
http.SetCookie(w, &cookie)
}
func clearCookie(w http.ResponseWriter, cookie *http.Cookie) {
if cookie == nil {
return
}
cookie.Value = ""
// Set cookie
http.SetCookie(w, cookie)
}
func getCookie(r *http.Request) *http.Cookie {
if cookie, err := r.Cookie("grafana_session"); err == nil {
return cookie
}
return nil
}
func isCookieExist(cookie *http.Cookie) bool {
if cookie == nil {
return false
}
return cookie.Value != ""
}