/
main.go
157 lines (146 loc) · 4.27 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
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
package main
import (
"encoding/json"
"github.com/dustin/go-humanize"
"github.com/julienschmidt/httprouter"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"html/template"
"net/http"
)
var tpl *template.Template
func init() {
r := httprouter.New()
http.Handle("/", r)
r.GET("/", home)
r.GET("/following", fing)
r.GET("/followingme", fingme)
r.GET("/user/:user", user)
r.GET("/form/login", login)
r.GET("/form/signup", signup)
r.POST("/api/checkusername", checkUserName)
r.POST("/api/createuser", createUser)
r.POST("/api/login", loginProcess)
r.POST("/api/tweet", tweetProcess)
r.GET("/api/logout", logout)
r.GET("/api/follow/:user", follow)
r.GET("/api/unfollow/:user", unfollow)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir("public/"))))
tpl = template.New("roottemplate")
tpl = tpl.Funcs(template.FuncMap{
"humanize_time": humanize.Time,
})
tpl = template.Must(tpl.ParseGlob("templates/html/*.html"))
}
func home(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
ctx := appengine.NewContext(req)
//get tweets
tweets, err := getTweets(req, nil)
if err != nil {
log.Errorf(ctx, "error getting tweets: %v", err)
http.Error(res, err.Error(), 500)
return
}
// get session
memItem, err := getSession(req)
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
}
sd.Tweets = tweets
tpl.ExecuteTemplate(res, "home.html", &sd)
}
func user(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
ctx := appengine.NewContext(req)
user := User{UserName: ps.ByName("user")}
//get tweets
tweets, err := getTweets(req, &user)
if err != nil {
log.Errorf(ctx, "error getting tweets: %v", err)
http.Error(res, err.Error(), 500)
return
}
// get session
memItem, err := getSession(req)
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
sd.ViewingUser = user.UserName
sd.FollowingUser, err = following(sd.UserName, user.UserName, req)
if err != nil {
log.Errorf(ctx, "error running following query: %v", err)
http.Error(res, err.Error(), 500)
return
}
}
sd.Tweets = tweets
tpl.ExecuteTemplate(res, "user.html", &sd)
}
func fing(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
// get session
memItem, err := getSession(req)
if err != nil {
log.Infof(ctx, "Attempt to see following from logged out user")
http.Error(res, "You must be logged in", http.StatusForbidden)
return
}
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
}
// Get followees
// get the datastore key for the follower
followerKey := datastore.NewKey(ctx, "Users", sd.UserName, 0, nil)
var XF []F
_, err = datastore.NewQuery("Follows").Ancestor(followerKey).Project("Following").GetAll(ctx, &XF)
log.Infof(ctx, "here is type %T \n and value %v", XF, XF)
if err != nil {
log.Errorf(ctx, "error getting followees: %v", err)
http.Error(res, err.Error(), 500)
return
}
sd.Following = XF
tpl.ExecuteTemplate(res, "follow.html", &sd)
}
func fingme(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
// get session
memItem, err := getSession(req)
if err != nil {
log.Infof(ctx, "Attempt to see followingme from logged out user")
http.Error(res, "You must be logged in", http.StatusForbidden)
return
}
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
}
// get those who are following this user
var XF []F
_, err = datastore.NewQuery("Follows").Filter("Following =", sd.UserName).GetAll(ctx, &XF)
log.Errorf(ctx, "here is type %T \n and value %v", XF, XF)
if err != nil {
log.Errorf(ctx, "error getting followees: %v", err)
http.Error(res, err.Error(), 500)
return
}
sd.Following = XF
tpl.ExecuteTemplate(res, "followingme.html", &sd)
}
func login(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "login.html")
}
func signup(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "signup.html")
}