-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
100 lines (91 loc) · 3.14 KB
/
store.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
package main
import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo"
)
// #ROUTES
// List all the keys in the bucket starting from "offset" key up to "limit" number of keys.
// This returns all the keys joined by "\n" so a simple split will give back an array.
func (app *App) List(c echo.Context) error {
// Check that we can parse limit, ignore errors
limit, err := strconv.ParseInt(c.QueryParam("limit"), 10, 64)
if err != nil {
limit = LIMIT
}
// Get all keys up to limit
data := app.Store[c.Param("user")].All("store", c.QueryParam("offset"), int(limit))
// Join the array with new lines
return c.String(http.StatusOK, strings.Join(data, "\n"))
}
// Create a value for key if not exist, returns [201] on creation, [200] if existed,
// [400] if request body can't be read, and [500] if error saving the data.
// Different status codes are useful to know if they key existed previously.
func (app *App) Create(c echo.Context) error {
// If request body can't be read, return [400]
data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// Get params (check params?)
user, key := c.Param("user"), c.Param("_*")
// Check if key exists already
exist := app.Store[user].Has("store", key)
// Save the data
err = app.Store[user].Set("store", key, data)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) // [500]
}
// [201] if key existed previously
if !exist {
return c.String(http.StatusCreated, key)
}
return c.String(http.StatusOK, key)
}
// Read the value for the given key
func (app *App) Read(c echo.Context) error {
// Look for the value
data := app.Store[c.Param("user")].Get("store", c.Param("_*"))
if data == nil {
return echo.NewHTTPError(http.StatusNotFound)
}
// Return a string? raw? mimetype?
return c.String(http.StatusOK, string(data))
}
// Update the value of an already existing key, (returns [404] if it does not exist?)
func (app *App) Update(c echo.Context) error {
// If request body can't be read, return [400]
data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// Get params (check params?)
user, key := c.Param("user"), c.Param("_*")
// Return [404] if key does not exist?
if !app.Store[user].Has("store", key) {
return echo.NewHTTPError(http.StatusNotFound)
}
// Saves the data
err = app.Store[user].Set("store", key, data)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) // [500]
}
return c.String(http.StatusOK, key)
}
// Delete a key and it's value.
func (app *App) Delete(c echo.Context) error {
// Get params (check params?)
user, key := c.Param("user"), c.Param("_*")
err := app.Store[user].Del("store", key)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError) // [500]
}
return c.String(http.StatusOK, key)
}
// WebSocket WIP? Need implementation, operations via websocket should allow all CRUD operations
// with a single persistent connection.
func (app *App) WebSocket(c echo.Context) error {
return c.String(http.StatusNotFound, "Not implemented")
}