/
api.go
170 lines (147 loc) · 4.04 KB
/
api.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
"github.com/nu7hatch/gouuid"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
)
func checkUserName(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
bs, err := ioutil.ReadAll(req.Body)
sbs := string(bs)
log.Infof(ctx, "REQUEST BODY: %v", sbs)
var user User
key := datastore.NewKey(ctx, "Users", sbs, 0, nil)
err = datastore.Get(ctx, key, &user)
// if there is an err, there is NO user
log.Infof(ctx, "ERR: %v", err)
if err != nil {
// there is an err, there is a NO user
fmt.Fprint(res, "false")
return
} else {
fmt.Fprint(res, "true")
}
}
func createUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
user := User{
Email: req.FormValue("email"),
UserName: req.FormValue("userName"),
Password: req.FormValue("password"),
}
key := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
key, err := datastore.Put(ctx, key, &user)
if err != nil {
log.Errorf(ctx, "error adding todo: %v", err)
http.Error(res, err.Error(), 500)
return
}
createSession(res, req, user)
// redirect
http.Redirect(res, req, "/", 302)
}
func loginProcess(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
key := datastore.NewKey(ctx, "Users", req.FormValue("userName"), 0, nil)
var user User
err := datastore.Get(ctx, key, &user)
if err != nil || req.FormValue("password") != user.Password {
// failure logging in
var sd SessionData
sd.LoginFail = true
tpl.ExecuteTemplate(res, "login.html", sd)
return
} else {
user.UserName = req.FormValue("userName")
// success logging in
createSession(res, req, user)
// redirect
http.Redirect(res, req, "/", 302)
}
}
func createSession(res http.ResponseWriter, req *http.Request, user User) {
ctx := appengine.NewContext(req)
// SET COOKIE
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: "session",
Value: id.String(),
Path: "/",
// UNCOMMENT WHEN DEPLOYED:
// Secure: true,
// HttpOnly: true,
}
http.SetCookie(res, cookie)
// SET MEMCACHE session data (sd)
json, err := json.Marshal(user)
if err != nil {
log.Errorf(ctx, "error marshalling during user creation: %v", err)
http.Error(res, err.Error(), 500)
return
}
sd := memcache.Item{
Key: id.String(),
Value: json,
Expiration: time.Duration(20 * time.Minute),
// Expiration: time.Duration(20 * time.Second),
}
memcache.Set(ctx, &sd)
}
func logout(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
cookie, err := req.Cookie("session")
// cookie is not set
if err != nil {
http.Redirect(res, req, "/", 302)
return
}
// clear memcache
sd := memcache.Item{
Key: cookie.Value,
Value: []byte(""),
Expiration: time.Duration(1 * time.Microsecond),
}
memcache.Set(ctx, &sd)
// clear the cookie
cookie.MaxAge = -1
http.SetCookie(res, cookie)
// redirect
http.Redirect(res, req, "/", 302)
}
func post(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
post := Post{
Username: req.FormValue("Username"),
Post: req.FormValue("Post"),
}
key := datastore.NewIncompleteKey(ctx, "Posts", nil)
key, err := datastore.Put(ctx, key, &post)
if err != nil {
log.Errorf(ctx, "error adding %v", err)
http.Error(res, err.Error(), 500)
return
}
fmt.Fprint(res, "Post -"+req.FormValue("Post")+"- Created, by: "+req.FormValue("Username"))
http.Redirect(res, req, "/", 302)
}
func getPosts(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
q := datastore.NewQuery("Posts")
for t := q.Run(ctx); ; {
var x Post
_, err1 := t.Next(&x)
fmt.Fprint(res, x.Username+" - "+x.Post+"/")
if err1 == datastore.Done {
log.Infof(ctx, "ERR: %v", err1)
break
}
}
}