-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
276 lines (221 loc) · 6.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
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
268
269
270
271
272
273
274
275
276
// go-angular project main.go
package main
import (
"encoding/json"
"encoding/xml"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
)
const (
FlickrEndPoint = "https://api.flickr.com/services/rest"
FlickrQuery = "flickr.photos.search"
FlickrKey = "300d436fa36986e197efe2a62682e05b"
PathPrefix = "/pups"
TopPupsPrefix = "/top"
)
// badRequest is handled by setting the status code in the reply to StatusBadRequest.
type badRequest struct{ error }
// notFound is handled by setting the status code in the reply to StatusNotFound.
type notFound struct{ error }
// errorHandler wraps a function returning an error by handling the error and returning a http.Handler.
// If the error is of the one of the types defined above, it is handled as described for every type.
// If the error is of another type, it is considered as an internal error and its message is logged.
func errorHandler(f func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err == nil {
return
}
switch err.(type) {
case badRequest:
http.Error(w, err.Error(), http.StatusBadRequest)
case notFound:
http.Error(w, "task not found", http.StatusNotFound)
default:
log.Println(err)
http.Error(w, "oops", http.StatusInternalServerError)
}
}
}
func ListTopPuppies(w http.ResponseWriter, r *http.Request) {
page := mux.Vars(r)["page"]
if page == "" {
page = "0"
}
imageManager := NewImageManager()
dbError := imageManager.InitDB(false)
if dbError != nil {
log.Printf("%q\n", dbError)
return
}
defer imageManager.GetDB().Close()
pageInt, err := strconv.Atoi(page)
if err != nil {
pageInt = 0
}
puppies := imageManager.GetPuppiesByMostVotes(pageInt)
count := imageManager.GetPuppiesCount()
perPage := 10
pages := count / perPage
searchResponse := PuppiesResponse{pageInt, pages, perPage, count, puppies}
response, err := json.Marshal(searchResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func UpdatePuppy(w http.ResponseWriter, r *http.Request) {
var v Vote
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
//return badRequest{err}
println("some error")
}
imageManager := NewImageManager()
dbError := imageManager.InitDB(false)
if dbError != nil {
log.Printf("%q\n", dbError)
return
}
defer imageManager.GetDB().Close()
id, err := strconv.Atoi(v.ID)
imageManager.UpdateVotes(id, v.VT)
response, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func ListPuppies(w http.ResponseWriter, r *http.Request) {
page := mux.Vars(r)["page"]
if page == "" {
page = "1"
}
//tags := mux.Vars(r)["tags"]
tags := "puppies,dogs,cute"
baseUrl, err := url.Parse(FlickrEndPoint)
if err != nil {
log.Fatal(err)
}
params := url.Values{}
params.Add("method", FlickrQuery)
params.Add("api_key", FlickrKey)
params.Add("tags", tags)
params.Add("per_page", "10")
params.Add("page", page)
params.Add("safe_search", "2")
params.Add("sort", "date-posted-desc")
baseUrl.RawQuery = params.Encode()
resp, err := http.Get(baseUrl.String())
if err != nil {
// handle error, send proper error response
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error, send proper error response
log.Println(err)
}
flickrResponse := struct {
Stat string `xml:"stat,attr"`
Err flickrError `xml:"err"`
Photos SearchResponse `xml:"photos"`
}{}
xml.Unmarshal([]byte(body), &flickrResponse)
//stat := flickrResult.Stat
//if stat is "ok"
if flickrResponse.Stat != "ok" {
println(flickrResponse.Err.Msg)
//return error message
}
searchResponse := flickrResponse.Photos
flickrPhotos := searchResponse.Photos
var tempIDs []string
imageManager := NewImageManager()
for _, ph := range flickrPhotos {
img := imageManager.NewImage(ph)
imageManager.Save(img)
tempIDs = append(tempIDs, img.ID)
}
dbError := imageManager.InitDB(false)
if dbError != nil {
log.Printf("%q\n", dbError)
return
}
defer imageManager.GetDB().Close()
all := imageManager.All()
dbPuppies := imageManager.FindOldPuppies(tempIDs)
var newPuppies []*Image
if len(dbPuppies) == 0 {
imageManager.InsertPuppies(all)
} else {
for _, puppy := range dbPuppies {
id := puppy.ID
for _, allP := range all {
//allPID, _ := strconv.Atoi(allP.ID)
if allP.ID == id {
allP.DownVotes = puppy.DownVotes
allP.UpVotes = puppy.UpVotes
} else {
exists := true
var existingPuppy *Image
for _, np := range newPuppies {
//nPID, _ := strconv.Atoi(np.ID)
if np.ID == allP.ID {
exists = false
existingPuppy = allP
break
}
}
if exists == false {
newPuppies = append(newPuppies, existingPuppy)
}
}
}
}
imageManager.InsertPuppies(newPuppies)
}
puppiesResponse := imageManager.GetPuppiesResponse(&searchResponse)
response, err := json.Marshal(puppiesResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func main() {
imageManager := NewImageManager()
dbError := imageManager.InitDB(false)
defer imageManager.GetDB().Close()
if dbError != nil {
log.Printf("%q\n", dbError)
return
} else {
imageManager.CreateTables()
}
r := mux.NewRouter().StrictSlash(false)
pups := r.Path(PathPrefix).Subrouter()
pups.Methods("GET").HandlerFunc(ListPuppies)
pupsPerPage := r.Path(PathPrefix + "/{page}").Subrouter()
pupsPerPage.Methods("GET").HandlerFunc(ListPuppies)
topPups := r.Path(TopPupsPrefix).Subrouter()
topPups.Methods("GET").HandlerFunc(ListTopPuppies)
topPupsPerPage := r.Path(TopPupsPrefix + "/{page}").Subrouter()
topPupsPerPage.Methods("GET").HandlerFunc(ListTopPuppies)
pupsUpdate := r.Path(PathPrefix).Subrouter()
pupsUpdate.Methods("PUT").HandlerFunc(UpdatePuppy)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}