-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
55 lines (48 loc) · 1.74 KB
/
main.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
package main
import (
"fmt"
"html/template"
"net/http"
)
/*
type Cookie struct {
Name string
Value string
Path string - indicates a URL path that must exist in req URL to send Cookie Header
Domain string - specifies which hosts are allowed to recieve the cookie
Expires time.Time - deletes at specified date
RawExpires string // for reading cookies only
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int - deletes cookie after specified amount of time in seconds
Secure bool - sent to server on encrypted req, never unsecured (HTTP)
HttpOnly bool - not accessible by JavaScript, only sent to server
SameSite SameSite - servers require that a cookie shouldn't be sent with cross-orign req
Raw string
Unparsed []string - raw text of unparsed attribute-value pairs
}
*/
var tpl *template.Template
func main() {
tpl, _ = template.ParseGlob("templates/*.html")
http.HandleFunc("/", indexHandler)
http.ListenAndServe("localhost:8080", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// looks for cookie on our machine
// func (r *Request) Cookie(name string) (*Cookie, error)
cookie, err := r.Cookie("2nd-cookie")
fmt.Println("cookie:", cookie, "err:", err)
if err != nil {
fmt.Println("cookie was not found")
cookie = &http.Cookie{
Name: "2nd-cookie",
Value: "my second cookie value",
HttpOnly: true,
}
// func SetCookie(w ResponseWriter, cookie *Cookie)
http.SetCookie(w, cookie)
}
tpl.ExecuteTemplate(w, "index.html", nil)
}