-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconn.go
405 lines (341 loc) · 11.2 KB
/
conn.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/fern4lvarez/piladb/config"
"github.com/fern4lvarez/piladb/pila"
"github.com/fern4lvarez/piladb/pkg/uuid"
"github.com/gorilla/mux"
)
// Conn represents the current piladb connection, containing
// the Pila instance and its status.
type Conn struct {
// Pila is the object that handles all data entities.
Pila *pila.Pila
// Config handles the connection configuration.
Config *config.Config
// Status holds the status of the connection and
// resources management.
Status *Status
opDate time.Time
}
// NewConn creates and returns a new piladb connection.
func NewConn() *Conn {
conn := &Conn{}
conn.Pila = pila.NewPila()
conn.Config = config.NewConfig()
conn.Status = NewStatus(v(), time.Now().UTC(), MemStats())
return conn
}
// Connection Handlers
// rootHandler shows information about piladb.
func (c *Conn) rootHandler(w http.ResponseWriter, r *http.Request) {
var links = []byte(`{"thank you":"for using piladb","www":"https://www.piladb.org","code":"https://github.com/fern4lvarez/piladb","docs":"https://docs.piladb.org"}`)
w.Header().Set("Content-Type", "application/json")
log.Println(r.Method, r.URL, http.StatusOK)
w.Write(links)
}
// pingHandler writes pong.
func (c *Conn) pingHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL, http.StatusOK)
w.Write([]byte("pong"))
}
// statusHandler writes the piladb status into the response.
func (c *Conn) statusHandler(w http.ResponseWriter, r *http.Request) {
c.Status.Update(time.Now().UTC(), MemStats())
w.Header().Set("Content-Type", "application/json")
log.Println(r.Method, r.URL, http.StatusOK)
w.Write(c.Status.ToJSON())
}
// databasesHandler returns the information of the running databases.
func (c *Conn) databasesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
c.createDatabaseHandler(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
log.Println(r.Method, r.URL, http.StatusOK)
w.Write(c.Pila.Status().ToJSON())
}
// createDatabaseHandler creates a Database and returns 201 and the ID and name
// of the Database.
func (c *Conn) createDatabaseHandler(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if name == "" {
log.Println(r.Method, r.URL, http.StatusBadRequest, "missing name")
w.WriteHeader(http.StatusBadRequest)
return
}
db := pila.NewDatabase(name)
err := c.Pila.AddDatabase(db)
if err != nil {
log.Println(r.Method, r.URL, http.StatusConflict, err)
w.WriteHeader(http.StatusConflict)
return
}
w.Header().Set("Content-Type", "application/json")
log.Println(r.Method, r.URL, http.StatusCreated)
w.WriteHeader(http.StatusCreated)
w.Write(db.Status().ToJSON())
}
// databaseHandler returns the information of a single database given its ID
// or name.
func (c *Conn) databaseHandler(databaseID string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// we override the mux vars to be able to test
// an arbitrary database ID
if databaseID != "" {
vars = map[string]string{
"id": databaseID,
}
}
db, ok := ResourceDatabase(c, vars["id"])
if !ok {
c.goneHandler(w, r, fmt.Sprintf("database %s is Gone", vars["id"]))
return
}
if r.Method == "DELETE" {
_ = c.Pila.RemoveDatabase(db.ID)
log.Println(r.Method, r.URL, http.StatusNoContent)
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Content-Type", "application/json")
log.Println(r.Method, r.URL, http.StatusOK)
w.Write(db.Status().ToJSON())
})
}
// stacksHandler handles the stacks of a database, being able to get the status
// of them, or create a new one.
func (c *Conn) stacksHandler(databaseID string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.opDate = time.Now().UTC()
vars := mux.Vars(r)
// we override the mux vars to be able to test
// an arbitrary database ID
if databaseID != "" {
vars = map[string]string{
"database_id": databaseID,
}
}
db, ok := ResourceDatabase(c, vars["database_id"])
if !ok {
c.goneHandler(w, r, fmt.Sprintf("database %s is Gone", vars["database_id"]))
return
}
if r.Method == "PUT" {
c.createStackHandler(w, r, db.ID.String())
return
}
var status pila.StackStatuser
_ = r.ParseForm()
if _, ok := r.Form["kv"]; ok {
status = db.StacksKV()
} else {
status = db.StacksStatus()
}
res, err := status.ToJSON()
if err != nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"error on response serialization:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(res)
log.Println(r.Method, r.URL, http.StatusOK)
})
}
// createStackHandler handles the creation of a stack, given a database
// by its id and the time of creation. Returns the status of the new stack.
func (c *Conn) createStackHandler(w http.ResponseWriter, r *http.Request, databaseID string) {
name := r.FormValue("name")
if name == "" {
log.Println(r.Method, r.URL, http.StatusBadRequest, "missing name")
w.WriteHeader(http.StatusBadRequest)
return
}
db, ok := c.Pila.Database(uuid.UUID(databaseID))
if !ok {
c.goneHandler(w, r, fmt.Sprintf("database %s is Gone", databaseID))
return
}
stack := pila.NewStack(name, c.opDate)
err := db.AddStack(stack)
if err != nil {
log.Println(r.Method, r.URL, http.StatusConflict, err)
w.WriteHeader(http.StatusConflict)
return
}
stack.Update(c.opDate)
// Do not check error as the Status of a new stack does
// not contain types that could cause such case.
// See http://golang.org/src/encoding/json/encode.go?s=5438:5481#L125
res, _ := stack.Status().ToJSON()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(res)
log.Println(r.Method, r.URL, http.StatusCreated)
}
// stackHandler handles operations on a single stack of a database. It holds
// the PUSH, POP, PEEK and SIZE methods, and the stack deletion.
func (c *Conn) stackHandler(params *map[string]string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.opDate = time.Now().UTC()
vars := mux.Vars(r)
// we override the mux vars to be able to test
// an arbitrary database and stack ID
if params != nil {
vars = *params
}
db, ok := ResourceDatabase(c, vars["database_id"])
if !ok {
c.goneHandler(w, r, fmt.Sprintf("database %s is Gone", vars["database_id"]))
return
}
stack, ok := ResourceStack(db, vars["stack_id"])
if !ok {
c.goneHandler(w, r, fmt.Sprintf("stack %s is Gone", vars["stack_id"]))
return
}
switch {
case r.Method == "GET":
_ = r.ParseForm()
if _, ok := r.Form["peek"]; ok {
c.peekStackHandler(w, r, stack)
return
}
if _, ok := r.Form["size"]; ok {
c.sizeStackHandler(w, r, stack)
return
}
c.statusStackHandler(w, r, stack)
return
case r.Method == "POST":
c.checkMaxStackSize(c.pushStackHandler)(w, r, stack)
return
case r.Method == "DELETE":
_ = r.ParseForm()
if _, ok := r.Form["flush"]; ok {
c.flushStackHandler(w, r, stack)
return
}
if _, ok := r.Form["full"]; ok {
c.deleteStackHandler(w, r, db, stack)
return
}
c.popStackHandler(w, r, stack)
return
}
})
}
// statusStackHandler returns the status of the Stack.
func (c *Conn) statusStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
stack.Read(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider that a flushed
// stack has no JSON encoding issues.
b, _ := stack.Status().ToJSON()
w.Write(b)
}
// peekStackHandler returns the peek of the Stack without modifying it.
func (c *Conn) peekStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
var element pila.Element
element.Value = stack.Peek()
stack.Read(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK, element.Value)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider our element
// suitable for a JSON encoding.
b, _ := element.ToJSON()
w.Write(b)
}
// sizeStackHandler returns the size of the Stack.
func (c *Conn) sizeStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
stack.Read(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK, stack.Size())
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider the size
// of a stack valid for a JSON encoding.
w.Write(stack.SizeToJSON())
}
// pushStackHandler adds an element into a Stack and returns 200 and the element.
func (c *Conn) pushStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
if r.Body == nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"no element provided")
w.WriteHeader(http.StatusBadRequest)
return
}
var element pila.Element
err := element.Decode(r.Body)
if err != nil {
log.Println(r.Method, r.URL, http.StatusBadRequest,
"error on decoding element:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
stack.Push(element.Value)
stack.Update(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK, element.Value)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider our element
// suitable for a JSON encoding.
b, _ := element.ToJSON()
w.Write(b)
}
// popStackHandler extracts the peek element of a Stack, returns 200 and returns it.
func (c *Conn) popStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
value, ok := stack.Pop()
if !ok {
log.Println(r.Method, r.URL, http.StatusNoContent)
w.WriteHeader(http.StatusNoContent)
return
}
stack.Update(c.opDate)
element := pila.Element{Value: value}
log.Println(r.Method, r.URL, http.StatusOK, element.Value)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider our element
// suitable for a JSON encoding.
b, _ := element.ToJSON()
w.Write(b)
}
// flushStackHandler flushes the Stack, setting the size to 0 and emptying all
// the content.
func (c *Conn) flushStackHandler(w http.ResponseWriter, r *http.Request, stack *pila.Stack) {
stack.Flush()
stack.Update(c.opDate)
log.Println(r.Method, r.URL, http.StatusOK)
w.Header().Set("Content-Type", "application/json")
// Do not check error as we consider that a flushed
// stack has no JSON encoding issues.
b, _ := stack.Status().ToJSON()
w.Write(b)
}
// deleteStackHandler deletes the Stack from a database.
func (c *Conn) deleteStackHandler(w http.ResponseWriter, r *http.Request, database *pila.Database, stack *pila.Stack) {
stack.Flush()
// Do not check output as we validated that
// stack always exists.
_ = database.RemoveStack(stack.ID)
log.Println(r.Method, r.URL, http.StatusNoContent)
w.WriteHeader(http.StatusNoContent)
return
}
// notFoundHandler logs and returns a 404 NotFound response.
func (c *Conn) notFoundHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL, http.StatusNotFound)
http.NotFound(w, r)
}
// goneHandler logs and returns a 410 Gone response with information
// about the missing resource.
func (c *Conn) goneHandler(w http.ResponseWriter, r *http.Request, message string) {
log.Println(r.Method, r.URL,
http.StatusGone, message)
w.WriteHeader(http.StatusGone)
}