This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
/
handler.go
267 lines (227 loc) · 7.11 KB
/
handler.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package core implements the core backend service for the app */
package backend
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/GoogleCloudPlatform/issuetracker/pkg/auth"
"github.com/GoogleCloudPlatform/issuetracker/pkg/github"
"google.golang.org/appengine"
"github.com/gorilla/mux"
)
// StatusHandler is used for debugging the app as an admin
func StatusHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "{'message':'Running'}")
for k, v := range r.Header {
fmt.Fprintf(w, "%v = %v\n", k, v)
}
}
// UserGet retrieves a given user from the request
func UserGet(w http.ResponseWriter, r *http.Request) *AppError {
login := mux.Vars(r)["id"]
user, err := github.FindUserByLogin(login)
if err != nil {
return appErrorf(err, "No such user: %v", login)
}
writeJSON(w, user)
return nil
}
// GetSubs retrieves subscriptions for a given user
func GetSubs(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
subs, _ := user.GetSubscriptions()
writeJSON(w, subs)
return nil
}
// GetNotifications retrieves notifications sent for a given user
func GetNotifications(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
notifs, _ := user.GetNotifications(0)
writeJSON(w, notifs)
return nil
}
// GetRepos retrieves open issue counts for repositories that a user subscribes to
func GetRepos(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
repos, _ := user.GetRepos()
writeJSON(w, repos)
return nil
}
// AddSubs retrieves subscriptions for a given user
func AddSubs(w http.ResponseWriter, r *http.Request) *AppError {
ctx := appengine.NewContext(r)
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
repo := r.FormValue("repo")
if err = github.UpdateRepo(ctx, repo); err != nil {
log.Printf("Github API Fetch error: %v", err)
return appErrorf(err, "Couldn't subscribe to repo: %v", repo)
}
if err := user.Subscribe(repo, github.NewPreference()); err != nil {
writeJSON(w, status{err, "Could not subscribe to repo", 500})
return appErrorf(err, "Couldn't subscribe to repo: %v", repo)
}
subs, _ := user.GetSubscriptions()
writeJSON(w, subs)
return nil
}
// UpdateSub updates subscriptions for a given user
func UpdateSub(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
repo := r.FormValue("repo")
defaultEmail := r.FormValue("defaultEmail")
settings := []byte(r.FormValue("settings"))
preferences := github.EmailPreference{}
err = json.Unmarshal(settings, &preferences)
if err != nil {
return appErrorf(err, "Couldn't get settings for repo: %v", repo)
}
subs, _ := user.GetSubscriptions(repo)
if len(subs) == 1 {
sub := subs[0]
sub.EmailPreference = preferences
if len(defaultEmail) != 0 {
sub.DefaultEmail = defaultEmail
}
if err := user.UpdateSubscription(repo, &sub); err == nil {
writeJSON(w, sub)
return nil
}
}
return appErrorf(err, "Couldn't update settings for repo: %v", repo)
}
// DelSubs retrieves subscriptions for a given user
func DelSubs(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
repo := r.FormValue("repo")
if err := user.Unsubscribe(repo); err != nil {
writeJSON(w, status{err, "Could not unsubscribe repo", 500})
return appErrorf(err, "Couldn't unsubscribe repo: %v", repo)
}
writeJSON(w, status{err, "ok", 200})
return nil
}
// UserAdd handles creation of a new user
func UserAdd(w http.ResponseWriter, r *http.Request) *AppError {
var user github.User
user.Login = r.FormValue("github_login")
user.FireKey = r.FormValue("fb_id")
user.ID, _ = strconv.ParseUint(r.FormValue("github_id"), 10, 64)
user.Email = r.FormValue("github_email")
w.Header().Set("Content-Type", "application/json")
payload, _ := json.Marshal(user)
w.Write([]byte(payload))
err := user.Add()
if err != nil {
return appErrorf(err, "Error creating user")
}
return nil
}
// UserUpdate updates a user's preferences
func UserUpdate(w http.ResponseWriter, r *http.Request) *AppError {
user, err := getAuthenticatedUser(w, r)
if err != nil {
return appErrorf(err, "No such user: %v", user.Login)
}
if email := r.FormValue("email"); len(email) != 0 {
user.Email = email
}
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(user)
w.Write([]byte(response))
if err := user.Update(); err != nil {
return appErrorf(err, "Error updating user")
}
return nil
}
// GetHandler wraps a handler func with error handling logic
// http://blog.golang.org/error-handling-and-go
type GetHandler func(http.ResponseWriter, *http.Request) *AppError
// AppError xports type for a Handler errors
type AppError struct {
Error error
Message string
Code int
}
func (fn GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil { // e is *appError, not os.Error.
log.Printf("Handler error: status code: %d, message: %s, underlying err: %#v",
e.Code, e.Message, e.Error)
http.Error(w, e.Message, e.Code)
}
}
func appErrorf(err error, format string, v ...interface{}) *AppError {
return &AppError{
Error: err,
Message: fmt.Sprintf(format, v...),
Code: 500,
}
}
type status AppError
func writeJSON(w http.ResponseWriter, v interface{}) (int, error) {
w.Header().Set("Content-Type", "application/json")
payload, _ := json.Marshal(v)
return w.Write([]byte(payload))
}
// AuthTokenHandler verifies the passed token with firebase
// returns a user object as a JSON if succcessful
func AuthTokenHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
val, ok := auth.VerifyAuthToken(r)
payload, _ := json.Marshal(val)
if !ok {
writeJSON(w, struct {
Valid bool
Err string
}{
ok,
string(payload),
})
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(payload)
}
}
func getAuthenticatedUser(w http.ResponseWriter, r *http.Request) (github.User, error) {
payload, ok := auth.VerifyAuthToken(r)
user := payload.User
if !ok {
err := fmt.Errorf("Auth Error")
return user, err
}
return user, nil
}