forked from tortis/study
/
server.go
296 lines (260 loc) · 6.7 KB
/
server.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const PORT = ":8888"
var decks *mgo.Collection
func main() {
// Create a gorilla router
router := mux.NewRouter()
// Open connection to database
dbs, err := mgo.Dial("localhost")
if err != nil {
log.Fatal(err)
}
studyDB := dbs.DB("study")
decks = studyDB.C("decks")
// Attach handlers
router.HandleFunc("/decks", listDecks).Methods("GET")
router.HandleFunc("/decks", postDeck).Methods("POST")
router.HandleFunc("/decks/{did}", getDeck).Methods("GET")
router.HandleFunc("/decks/{did}", deleteDeck).Methods("DELETE")
router.HandleFunc("/decks/{did}/cards", postCard).Methods("POST")
router.HandleFunc("/decks/{did}/cards/{cid}", deleteCard).Methods("DELETE")
router.HandleFunc("/decks/{did}/cards/{cid}", putCard).Methods("PUT")
router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
log.Printf("Starting server on port %s\n.", PORT)
log.Fatal(http.ListenAndServe(PORT, router))
}
func listDecks(w http.ResponseWriter, r *http.Request) {
// Response object that will be returned
resp := struct {
Decks []Deck
}{}
// Read decks from database, exclude the 'cards' field
err := decks.Find(bson.M{}).Select(bson.M{"cards": 0}).All(&resp.Decks)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
// Marshel response to json and return
j, err := json.Marshal(&resp)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func postDeck(w http.ResponseWriter, r *http.Request) {
// Try to read a Deck from request body
dec := json.NewDecoder(r.Body)
d := Deck{}
err := dec.Decode(&d)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
// Validate the deck
if d.Name == "" {
http.Error(w, "No name given for deck", http.StatusBadRequest)
return
}
// Insert deck into the database
err = decks.Insert(&d)
if err != nil {
log.Println(err)
http.Error(w, "", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func getDeck(w http.ResponseWriter, r *http.Request) {
deckId, err := parseObjectId(mux.Vars(r)["did"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
// Get the deck from the database
d := Deck{}
err = decks.FindId(deckId).One(&d)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, "", http.StatusNotFound)
return
}
http.Error(w, "", http.StatusInternalServerError)
return
}
b, err := json.Marshal(&d)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func deleteDeck(w http.ResponseWriter, r *http.Request) {
// Get deck id from the url
deckId, err := parseObjectId(mux.Vars(r)["did"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
// Delete deck from database
err = decks.RemoveId(deckId)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, "", http.StatusNotFound)
return
}
http.Error(w, "", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func postCard(w http.ResponseWriter, r *http.Request) {
// Get deck id from the url
deckId, err := parseObjectId(mux.Vars(r)["did"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
// Read card from "json" part of body
c := Card{}
err = json.Unmarshal([]byte(r.FormValue("json")), &c)
if err != nil {
http.Error(w, "Failed to parse json.", http.StatusBadRequest)
return
}
// Ensure a title was speicified.
if c.Title == "" {
http.Error(w, "The card must have a title.", http.StatusBadRequest)
return
}
c.Id = bson.NewObjectId()
// Read the file.
file, header, err := r.FormFile("image")
if err != nil {
log.Println("Could not open form file: ", err)
http.Error(w, "There was a problem uploading the image.", http.StatusBadRequest)
return
}
defer file.Close()
// Generate the file name
fname := fmt.Sprintf("%d-%s", time.Now().Unix(), header.Filename)
c.Image = fname
// Write the file to disk
out, err := os.OpenFile("static/images/"+fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
log.Println("Failed to open image file", err)
return
}
_, err = io.Copy(out, file)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
log.Println("Failed to copy file from http form.")
return
}
// Add the new card to the deck
q := bson.M{
"$push": bson.M{"cards": &c},
}
err = decks.UpdateId(deckId, q)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, "", http.StatusNotFound)
return
}
http.Error(w, "", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func deleteCard(w http.ResponseWriter, r *http.Request) {
// Get deck id from the url
deckId, err := parseObjectId(mux.Vars(r)["did"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
// Get the card number to delete
cardId, err := parseObjectId(mux.Vars(r)["cid"])
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
q := bson.M{
"$pull": bson.M{"cards": bson.M{"id": cardId}},
}
err = decks.UpdateId(deckId, q)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, "", http.StatusNotFound)
return
}
http.Error(w, "", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func putCard(w http.ResponseWriter, r *http.Request) {
// Get deck id from the url
deckId, err := parseObjectId(mux.Vars(r)["did"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
// Get the card number to delete
cardId, err := parseObjectId(mux.Vars(r)["cid"])
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
// Read the new card from json body
dec := json.NewDecoder(r.Body)
c := Card{}
err = dec.Decode(&c)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
// Validate the card
if c.Title == "" {
http.Error(w, "The card must have a title.", http.StatusBadRequest)
return
}
// Update the card in the database
q := bson.M{
"$set": bson.M{
"cards.$.title": c.Title,
"cards.$.fields": c.Fields,
"cards.$.notes": c.Notes,
},
}
err = decks.Update(bson.M{"_id": deckId, "cards.id": cardId}, q)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, "", http.StatusNotFound)
return
}
http.Error(w, "", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func parseObjectId(s string) (bson.ObjectId, error) {
if !bson.IsObjectIdHex(s) {
return bson.NewObjectId(), fmt.Errorf("Id: %s is not a bson id.", s)
}
return bson.ObjectIdHex(s), nil
}