forked from GoesToEleven/GolangTraining
/
main.go
executable file
·47 lines (39 loc) · 835 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"github.com/nu7hatch/gouuid"
"google.golang.org/appengine"
"google.golang.org/appengine/memcache"
"net/http"
)
func init() {
http.HandleFunc("/", handleIndex)
}
func handleIndex(res http.ResponseWriter, req *http.Request) {
cookie, _ := req.Cookie("sessionid")
if cookie == nil {
id, _ := uuid.NewV4()
cookie = &http.Cookie{
Name: "sessionid",
Value: id.String(),
}
http.SetCookie(res, cookie)
}
ctx := appengine.NewContext(req)
item, _ := memcache.Get(ctx, cookie.Value)
if item == nil {
m := map[string]string{
"email": "test@example.com",
}
bs, _ := json.Marshal(m)
item = &memcache.Item{
Key: cookie.Value,
Value: bs,
}
memcache.Set(ctx, item)
}
var m map[string]string
json.Unmarshal(item.Value, &m)
fmt.Fprintln(res, m)
}