forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
178 lines (157 loc) · 4.07 KB
/
routes.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
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
"net/http"
"strings"
"time"
)
func init() {
http.HandleFunc("/", home)
http.HandleFunc("/login", login)
http.HandleFunc("/tweet", tweet)
http.HandleFunc("/logout", logout)
http.HandleFunc("/profile", profile)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir("public/"))))
}
func home(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
profile(res, req)
return
}
ctx := appengine.NewContext(req)
u := user.Current(ctx)
log.Infof(ctx, "user: ", u)
// pointers can be NIL so don't use a Profile * Profile here:
var model struct {
Profile Profile
Tweets []Tweet
}
if u != nil {
profile, err := getProfileByEmail(ctx, u.Email)
if err != nil {
http.Redirect(res, req, "/login", 302)
return
}
model.Profile = *profile
}
// TODO: get recent tweets
var tweets []Tweet
tweets, err := recentTweets(ctx)
if err != nil {
http.Error(res, err.Error(), 500)
}
model.Tweets = tweets
renderTemplate(res, "home.html", model)
}
func login(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
// look for the user's profile
profile, err := getProfileByEmail(ctx, u.Email)
// if it exists redirect
if err == nil && profile.Username != "" {
http.Redirect(res, req, "/"+profile.Username, 302)
return
}
var model struct {
Profile *Profile
Error string
}
model.Profile = &Profile{Email: u.Email}
// create the profile
username := req.FormValue("username")
if username != "" {
_, err = getProfileByUsername(req, username)
// if the username is already taken
if err == nil {
model.Error = "username is not available"
model.Profile.Username = username // caleb didn't have this line
} else {
model.Profile.Username = username
// try to create the profile
err = createProfile(req, model.Profile)
if err != nil {
model.Error = err.Error()
} else {
// on success redirect to the user's timeline
waitForProfile(req, username)
http.SetCookie(res, &http.Cookie{Name: "logged_in", Value: "true"})
http.Redirect(res, req, "/"+username, 302)
return
}
}
}
// render the login template
renderTemplate(res, "login.html", model)
}
func profile(res http.ResponseWriter, req *http.Request) {
// get the username
username := strings.SplitN(req.URL.Path, "/", 2)[1]
// get the profile
profile, err := getProfileByUsername(req, username)
if err != nil {
http.Error(res, err.Error(), 404)
return
}
// get user's tweets
ctx := appengine.NewContext(req)
var tweets []Tweet
tweets, err = userTweets(ctx, profile.Email)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
// Render the template
var model struct {
Profile *Profile
Tweets []Tweet
}
model.Profile = profile
model.Tweets = tweets
renderTemplate(res, "profile.html", model)
/*
// Render the template
type Model struct {
Profile *Profile
}
renderTemplate(res, "user-profile", Model{
Profile: profile,
})
*/
}
func tweet(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
var tweet *Tweet
tweet = receiveTweet(ctx, res, req)
tweet.Time = time.Now()
// add in username
var profile *Profile
profile, err := getProfileByEmail(ctx, u.Email)
tweet.Username = profile.Username
err = putTweet(ctx, tweet, u.Email)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
json.NewEncoder(res).Encode(true)
}
func receiveTweet(ctx context.Context, res http.ResponseWriter, req *http.Request) *Tweet {
var tweet Tweet
err := json.NewDecoder(req.Body).Decode(&tweet)
if err != nil {
log.Errorf(ctx, "error unmarshalling todos: %v", err)
http.Error(res, err.Error(), 500)
return nil
}
return &tweet
}
func logout(res http.ResponseWriter, req *http.Request) {
http.SetCookie(res, &http.Cookie{Name: "logged_in", Value: ""})
http.Redirect(res, req, "/", 302)
}