/
document.go
214 lines (206 loc) · 5.74 KB
/
document.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
// Document management handlers.
package httpapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// Insert a document into collection.
func Insert(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col, doc string
if !Require(w, r, "col", &col) {
return
}
defer r.Body.Close()
bodyBytes, _ := ioutil.ReadAll(r.Body)
doc = string(bodyBytes)
if doc == "" && !Require(w, r, "doc", &doc) {
return
}
var jsonDoc map[string]interface{}
if err := json.Unmarshal([]byte(doc), &jsonDoc); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON document.", doc), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
id, err := dbcol.Insert(jsonDoc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
w.WriteHeader(201)
w.Write([]byte(fmt.Sprint(id)))
}
// Find and retrieve a document by ID.
func Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col, id string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
docID, err := strconv.Atoi(id)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
doc, err := dbcol.Read(docID)
if doc == nil {
http.Error(w, fmt.Sprintf("No such document ID %d.", docID), 404)
return
}
resp, err := json.Marshal(doc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
w.Write(resp)
}
// Divide documents into roughly equally sized pages, and return documents in the specified page.
func GetPage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col, page, total string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "page", &page) {
return
}
if !Require(w, r, "total", &total) {
return
}
totalPage, err := strconv.Atoi(total)
if err != nil || totalPage < 1 {
http.Error(w, fmt.Sprintf("Invalid total page number '%v'.", totalPage), 400)
return
}
pageNum, err := strconv.Atoi(page)
if err != nil || pageNum < 0 || pageNum >= totalPage {
http.Error(w, fmt.Sprintf("Invalid page number '%v'.", page), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
docs := make(map[string]interface{})
dbcol.ForEachDocInPage(pageNum, totalPage, func(id int, doc []byte) bool {
var docObj map[string]interface{}
if err := json.Unmarshal(doc, &docObj); err == nil {
docs[strconv.Itoa(id)] = docObj
}
return true
})
resp, err := json.Marshal(docs)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
w.Write(resp)
}
// Update a document.
func Update(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col, id, doc string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
defer r.Body.Close()
bodyBytes, _ := ioutil.ReadAll(r.Body)
doc = string(bodyBytes)
if doc == "" && !Require(w, r, "doc", &doc) {
return
}
docID, err := strconv.Atoi(id)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
var newDoc map[string]interface{}
if err := json.Unmarshal([]byte(doc), &newDoc); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON document.", newDoc), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
err = dbcol.Update(docID, newDoc)
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
}
// Delete a document.
func Delete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col, id string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "id", &id) {
return
}
docID, err := strconv.Atoi(id)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid document ID '%v'.", id), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
dbcol.Delete(docID)
}
// Return approximate number of documents in the collection.
func ApproxDocCount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var col string
if !Require(w, r, "col", &col) {
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
w.Write([]byte(strconv.Itoa(dbcol.ApproxDocCount())))
}