-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
164 lines (127 loc) · 3.91 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/dimfeld/httptreemux"
)
// This is our JSON response renderer that just sets the right content type.
func JsonResponse(h httptreemux.HandlerFunc) httptreemux.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
h(w, r, p)
}
}
// List out all the routes.
func Index(w http.ResponseWriter, r *http.Request, _ map[string]string) {
data := map[string]interface{}{
"info": "This is yakdb, a highly performant in-memory key-value store.",
"docs": "https://github.com/needcaffeine/yakdb",
}
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// List all the collections.
func CollectionsList(w http.ResponseWriter, r *http.Request, _ map[string]string) {
w.WriteHeader(http.StatusOK)
data := collections.ListCollections()
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// Get the contents of a collection.
func CollectionsGet(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
collectionId := p["collectionId"]
data := collections[collectionId]
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// Add a collection.
func CollectionsPut(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusCreated)
collectionId := p["collectionId"]
data := collections.CreateEmptyCollection(collectionId)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// Delete a collection.
func CollectionsDelete(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
data := collections.DeleteOneCollection(p["collectionId"])
json.NewEncoder(w).Encode(data)
}
// Add an item to a collection.
func ItemsPut(w http.ResponseWriter, r *http.Request, p map[string]string) {
// Read in the json document supplied to us.
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
// Decode the json document.
var document interface{}
if err := json.Unmarshal(body, &document); err != nil {
if err := json.NewEncoder(w).Encode(err); err != nil {
w.WriteHeader(422)
panic(err)
}
}
if len(body) > 0 {
collectionId := p["collectionId"]
itemId := p["itemId"]
data := collections.AddItem(collectionId, itemId, document)
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
} else {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Empty body.")
}
}
// List all the items in the collection.
func ItemsList(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
collectionId := p["collectionId"]
collection := collections[collectionId]
data := collection.GetItems()
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// Get an item.
func ItemsGet(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
collectionId := p["collectionId"]
collection := collections[collectionId]
itemId := p["itemId"]
data := collection.FindOneItemById(itemId)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
// Delete an item.
func ItemsDelete(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
collectionId := p["collectionId"]
collection := collections[collectionId]
itemId := p["itemId"]
data := collection.DeleteOneItemById(itemId)
json.NewEncoder(w).Encode(data)
}
// Delete all items.
func ItemsFlush(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.WriteHeader(http.StatusOK)
collectionId := p["collectionId"]
collection := collections[collectionId]
data := collection.FlushItems()
json.NewEncoder(w).Encode(data)
}