-
Notifications
You must be signed in to change notification settings - Fork 1
/
devfest.go
123 lines (107 loc) · 3.15 KB
/
devfest.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package devfest
import (
"appengine"
"appengine/datastore"
"appengine/memcache"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
type User struct {
Favs []string `json:"favs"`
Login string `json:"login"`
}
func init() {
http.HandleFunc("/api/v1/stars/get", getStars)
http.HandleFunc("/api/v1/stars/put", putStars)
http.HandleFunc("/.well-known/acme-challenge/", challengeHandler)
}
var challenges = map[string]string{
"xMqmJQFaopDwdUOascB6aNSx4gJuNTm2QOrgjpao10U": "xMqmJQFaopDwdUOascB6aNSx4gJuNTm2QOrgjpao10U.9YJdoJWoiu0xDzLP9F7ljCyuaCPjWSgsGxp3DVDgdr0",
"hello": "world",
}
func challengeHandler(w http.ResponseWriter, r *http.Request) {
challenge := strings.Split(r.RequestURI, "/.well-known/acme-challenge/")[1]
if responseToChallenge, ok := challenges[challenge]; !ok {
http.Error(w, "Error", http.StatusNotFound)
return
} else {
fmt.Fprintf(w, "%s", responseToChallenge)
}
}
func getStars(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
//w.Header().Set("SUPER-HACK", "@GDGNANTES")
//w.WriteHeader(http.StatusFound)
c := appengine.NewContext(r)
login, _ := url.QueryUnescape(r.FormValue("login"))
//favs := r.FormValue("favs")
// Mise en cache direct du spreadsheet
keyMemCache := "favs" + login
item, err := memcache.Get(c, keyMemCache)
var strJson []byte
if err == memcache.ErrCacheMiss || err != nil {
// Si c'est pas en cache, on regarde si c'est en base
var user User
keyUser := datastore.NewKey(c, "User", keyMemCache, 0, nil)
if err := datastore.Get(c, keyUser, &user); err != nil {
// Si c'est pas présent en base, alors on renvoie un tableau vide
user = User{
Login: login,
Favs: []string{},
}
}
tmpStrJson, _ := json.Marshal(user)
strJson = tmpStrJson
itemValue := &memcache.Item{
Key: keyMemCache,
Value: strJson,
}
// On ajoute dans le memcache
if err := memcache.Add(c, itemValue); err == memcache.ErrNotStored {
c.Infof("itemValue with key %q already exists", itemValue.Key)
} else if err != nil {
c.Errorf("error adding itemValue: %v", err)
}
} else {
strJson = item.Value
}
fmt.Fprintf(w, "%s\n", strJson)
}
func putStars(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
login, _ := url.QueryUnescape(r.FormValue("login"))
favs, _ := url.QueryUnescape(r.FormValue("favs"))
if len(login) == 0 || len(favs) == 0 {
fmt.Fprint(w, "Parametre manquant\n")
// TODO jetter une erreur
return
}
fmt.Fprint(w, login+"\n")
fmt.Fprint(w, favs+"\n")
c := appengine.NewContext(r)
// Get the item from the dataStore
keyMemCache := "favs" + login
var user = User{
Login: login,
}
keyUser := datastore.NewKey(c, "User", keyMemCache, 0, nil)
json.Unmarshal([]byte(favs), &user.Favs)
_, err := datastore.Put(c, keyUser, &user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// On met à jour aussi le memcache
strJson, _ := json.Marshal(user)
itemValue := &memcache.Item{
Key: keyMemCache,
Value: strJson,
}
if err := memcache.Set(c, itemValue); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}