/
mongoweb.go
358 lines (265 loc) · 8.77 KB
/
mongoweb.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Created By: Hari Kolasani. June 8, 2014.
*/
/* This program contains the HTTP Server and the Request Handler
that calls the appropriate CRUD methods on the DBManager
*/
package main
import (
"net/http"
"net/url"
"encoding/json"
"fmt"
"mongodb"
"strings"
"strconv"
"errors"
"labix.org/v2/mgo/bson"
"io/ioutil"
)
const (
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
MAX_ROWS = 2000
)
var dbMgr *mongodb.DBManager
func main() {
initDB()
mux := http.NewServeMux()
mux.HandleFunc("/gomongo/services/", handleRequest)
http.ListenAndServe(":8088", mux)
}
func initDB() {
dbMgr = mongodb.NewDBManager()
err := dbMgr.InitSession("mongodb://amsAppUser:password@localhost,localhost/AMS") //TODO: Externalize the URL
if err != nil {
fmt.Println("DBManager init failed",err)
} else {
fmt.Println("DBManager Initialized")
}
}
func handleRequest(response http.ResponseWriter, request *http.Request) {
switch request.Method {
case GET:
processGET(response,request)
case POST:
processPOST(response,request)
case PUT:
processPUT(response,request)
case DELETE:
processDELETE(response,request)
}
}
func processGET(response http.ResponseWriter, request *http.Request) {
var result []mongodb.Document
var err error
var content []byte
var queryParms mongodb.Document
var selectParms mongodb.Document
var docId string
serviceURL := request.URL
collection := getCollectionName(serviceURL)
docId = getDocId(serviceURL)
if len(docId) != 0 { //get document by Id
result,err = dbMgr.GetDocument(collection,docId)
} else { //run the query using query aprms
queryParms,err = getParms(serviceURL,"q")
if err == nil { //query parms look good ..
selectParms,err = getParms(serviceURL,"select")
}
sortParms := getSortParms(serviceURL) //get any sort parms
limit := getLimit(serviceURL) //get any limit that's passed
if err == nil {
result,err = dbMgr.RunQuery(collection,queryParms,selectParms,sortParms,limit)
}
}
//check for results
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":err.Error()}
content,err = json.MarshalIndent(mwError, "", " ")
}else {
if result == nil || len(result) == 0 {
content,err = json.MarshalIndent(map[string]mongodb.FieldValue{"errorCode":"404","errorMessage":"No Data Found"}, "", " ")
} else {
content,err = json.MarshalIndent(result, "", " ")
}
}
//something went wrong marshalling response
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":err.Error()}
content,_ = json.MarshalIndent(mwError, "", " ")
}
response.Header().Add("Content-Type","application/json")
response.Write(content)
}
func processPOST(response http.ResponseWriter, request *http.Request) {
var err error
var data mongodb.Document
var content []byte
var docId bson.ObjectId
serviceURL := request.URL
collection := getCollectionName(serviceURL)
defer request.Body.Close()
//get POSTed data and unmarshall to JSON
body, _ := ioutil.ReadAll(request.Body)
err = json.Unmarshal(body, &data)
//check JSON validity of posteed data
if err != nil {
err = errors.New("Invalid JSON Data: " + err.Error())
}else {
err,docId = dbMgr.InsertDocument(collection,data)
}
//check for result of the Insert
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":errors.New("POST Failed - " + err.Error()).Error()}
content,err = json.MarshalIndent(mwError, "", " ")
}else {
successData := bson.M{"success":true,"message":"Posted Successfully!","_id":docId}
content,err = json.MarshalIndent(successData, "", " ")
}
//something went wrong marshalling response
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":err.Error()}
content,_ = json.MarshalIndent(mwError, "", " ")
}
response.Header().Add("Content-Type","application/json")
response.Write(content)
}
func processPUT(response http.ResponseWriter, request *http.Request) {
var err error
var content []byte
var docId string
var data mongodb.Document
serviceURL := request.URL
collection := getCollectionName(serviceURL)
docId = getDocId(serviceURL)
if len(docId) != 0 { //delete document
if bson.IsObjectIdHex(docId) {
//get POSTed data and unmarshall to JSON
body, _ := ioutil.ReadAll(request.Body)
err = json.Unmarshal(body, &data)
//check JSON validity of posteed data
if err != nil {
err = errors.New("Invalid JSON Data: " + err.Error())
}else {
query := mongodb.Document{"_id": bson.ObjectIdHex(docId)}
err = dbMgr.UpdateDocument(collection,query,data)
}
}else {
err = errors.New("Invalid Document Id")
}
} else {
err = errors.New("Please provide the Id of the document to be Updated")
}
//check for result of the Update
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":errors.New("UPDATE Failed - " + err.Error()).Error()}
content,err = json.MarshalIndent(mwError, "", " ")
}else {
successData := bson.M{"success":true,"message":"Updated Successfully!"}
content,err = json.MarshalIndent(successData, "", " ")
}
//something went wrong marshalling response
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":err.Error()}
content,_ = json.MarshalIndent(mwError, "", " ")
}
response.Header().Add("Content-Type","application/json")
response.Write(content)
}
func processDELETE(response http.ResponseWriter, request *http.Request) {
var err error
var content []byte
var docId string
serviceURL := request.URL
collection := getCollectionName(serviceURL)
docId = getDocId(serviceURL)
if len(docId) != 0 { //delete document
if bson.IsObjectIdHex(docId) {
err = dbMgr.DeleteDocument(collection,mongodb.Document{"_id": bson.ObjectIdHex(docId)})
}else {
err = errors.New("Invalid Document Id")
}
} else {
err = errors.New("Please provide the Id of the document to be Deleted")
}
//check for result of the Insert
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":errors.New("DELETE Failed - " + err.Error()).Error()}
content,err = json.MarshalIndent(mwError, "", " ")
}else {
successData := bson.M{"success":true,"message":"Deleted Successfully!"}
content,err = json.MarshalIndent(successData, "", " ")
}
//something went wrong marshalling response
if err != nil {
mwError := bson.M{"errorCode":"500","errorMessage":err.Error()}
content,_ = json.MarshalIndent(mwError, "", " ")
}
response.Header().Add("Content-Type","application/json")
response.Write(content)
}
func getCollectionName(serviceURL *url.URL) (string) {
path := serviceURL.Path // 'gomongo/services/collectionName/documentId
pathSplits := strings.Split(path,"/")
return pathSplits[3]
}
func getDocId(serviceURL *url.URL) (string) {
path := serviceURL.Path // 'gomongo/services/collectionName/documentId
pathSplits := strings.Split(path,"/")
if len(pathSplits) > 4 {
return pathSplits[4]
}
return ""
}
func getParms(serviceURL *url.URL,parmType string) (parmsDocument mongodb.Document,err error ) {
var parmsString string
rawQuery := serviceURL.RawQuery
parmsMap, _ := url.ParseQuery(rawQuery)
if(parmsMap[parmType] != nil && len(parmsMap[parmType][0]) > 0) {
parmsString = parmsMap[parmType][0]
err = json.Unmarshal([]byte(parmsString) , &parmsDocument)
}else {
return nil,nil
}
if err != nil {
if parmType == "q" {
err = errors.New("Invalid Query Parms- " + err.Error())
} else {
err = errors.New("Invalid Select Parms-" + err.Error())
}
}
return parmsDocument,err
}
func getSortParms(serviceURL *url.URL) (sortParms []string) {
rawQuery := serviceURL.RawQuery
parmsMap, _ := url.ParseQuery(rawQuery)
if(parmsMap["sort"] != nil && len(parmsMap["sort"][0]) > 0) {
sortParms = parmsMap["sort"]
}
return sortParms
}
func getLimit(serviceURL *url.URL) (limit int) {
var err error
limit = MAX_ROWS
rawQuery := serviceURL.RawQuery
parmsMap, _ := url.ParseQuery(rawQuery)
if(parmsMap["limit"] != nil && len(parmsMap["limit"][0]) > 0) {
limitString := parmsMap["limit"][0]
limit,err = strconv.Atoi(limitString)
if err != nil {
limit = MAX_ROWS
}
}
return limit
}