/
main.go
94 lines (82 loc) · 2.11 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
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
package vronpass
import (
"log"
"net/http"
"appengine"
"io/ioutil"
"appengine/datastore"
"appengine/user"
)
func init() {
http.HandleFunc("/get/", fg)
http.HandleFunc("/set/", fs)
}
// This is for returning the data
type Entity struct {
Value []byte
}
func fg(w http.ResponseWriter, r *http.Request) {
// Start by checking so that the user is logged in!
c := appengine.NewContext(r)
c.Infof("THis is info")
u := user.Current(c)
if u == nil {
c.Infof("User not signed in")
// Generate a response that may be used to login the user
// since this might be needed!
str, er := user.LoginURL(c, "https://vronpass.appspot.com/")
if er != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(str))
return
}
// We simply store the information under the username!
path := u.Email
k := datastore.NewKey(c, "Entity", path, 0, nil)
e := new(Entity)
if err := datastore.Get(c, k, e); err!= nil {
// This does not exist so simply return a empty response
w.Write([]byte("{}"))
return
}
// Otherwise return the string
w.Write([]byte(e.Value))
}
func fs(w http.ResponseWriter, r *http.Request) {
// Start by checking so that the user is logged in!
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
c.Infof("User not signed in")
// Generate a response that may be used to login the user
// since this might be needed!
str, er := user.LoginURL(c, "https://vronpass.appspot.com/")
if er != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(str))
return
}
// We simply store the information under the username!
path := u.Email
// Read the body
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Failed to read body: ", err)
w.WriteHeader(http.StatusBadRequest)
return
}
k := datastore.NewKey(c, "Entity", path, 0, nil)
e := Entity{b}
if _, err := datastore.Put(c, k, &e); err!= nil {
// This does not exist so simply return a empty response
log.Println("Could not put: ", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}