-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
185 lines (150 loc) · 4.48 KB
/
routes.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
package v1
import (
"errors"
"net/http"
"time"
"github.com/dstdfx/bookish-spork/internal/pkg/backend"
"github.com/dstdfx/bookish-spork/internal/pkg/qqcache"
"github.com/go-chi/chi"
)
// Routes initializes v1 handler.
func Routes(b *backend.Backend) http.Handler {
r := chi.NewRouter()
// GET /v1/get/<key>
r.
With(RequireKeyName).
Get("/get/{key}", getHandler(b))
// POST /v1/set
r.
With(RequireSetParams).
Post("/set", setHandler(b))
// GET /v1/keys
r.Get("/keys", keysHandler(b))
// DELETE /v1/remove/<key>
r.
With(RequireKeyName).
Delete("/remove/{key}", removeHandler(b))
// POST /v1/rpush
r.
With(RequireRPushParams).
Post("/rpush", rpushHandler(b))
// GET /v1/lindex/<key>/<index>
r.
With(RequireKeyName).
With(RequireIndex).
Get("/lindex/{key}/{index}", lindexHandler(b))
// POST /v1/hset
r.
With(RequireHSetParams).
Post("/hset", hsetHandler(b))
// GET /v1/hget/<key>/<hkey>
r.
With(RequireKeyName).
With(RequireHKeyName).
Get("/hget/{key}/{hkey}", hgetHandler(b))
return r
}
func getHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get key from router's context
key := GetKeyName(req.Context())
// Get value from cache
k, ok := b.Cache.Get(key)
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
JSON(w, map[string]interface{}{"value": k})
}
}
func setHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get set body from router's context
body := GetSetBody(req.Context())
// Set new entity
b.Cache.Set(body.Key, body.Value, time.Duration(body.TTL)*time.Second)
w.WriteHeader(http.StatusOK)
}
}
func keysHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
JSON(w, map[string]interface{}{"keys": b.Cache.Keys()})
}
}
func removeHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get key from router's context
key := GetKeyName(req.Context())
// Remove key from the cache
b.Cache.Remove(key)
w.WriteHeader(http.StatusNoContent)
}
}
func rpushHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get rpush body from router's context
body := GetRPushBody(req.Context())
err := b.Cache.RPush(body.Key, body.Value, time.Duration(body.TTL)*time.Second)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
JSON(w, map[string]string{"error": err.Error()})
return
}
w.WriteHeader(http.StatusOK)
}
}
func lindexHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get key from router's context
key := GetKeyName(req.Context())
index := GetIndex(req.Context())
v, err := b.Cache.LIndex(key, index)
if err != nil {
if errors.Is(err, qqcache.ErrNotFound) {
w.WriteHeader(http.StatusNotFound)
}
if errors.Is(err, qqcache.ErrWrongTypeIndex) {
w.WriteHeader(http.StatusBadRequest)
JSON(w, map[string]string{"error": err.Error()})
}
return
}
w.WriteHeader(http.StatusOK)
JSON(w, map[string]interface{}{"value": v})
}
}
func hsetHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get hset body from router's context
body := GetHSetBody(req.Context())
err := b.Cache.HSet(body.Key, body.Value, time.Duration(body.TTL)*time.Second)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
JSON(w, map[string]string{"error": err.Error()})
return
}
w.WriteHeader(http.StatusOK)
}
}
func hgetHandler(b *backend.Backend) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
// Get key from router's context
key := GetKeyName(req.Context())
hkey := GetHKeyName(req.Context())
v, err := b.Cache.HGet(key, hkey)
if err != nil {
if errors.Is(err, qqcache.ErrNotFound) {
w.WriteHeader(http.StatusNotFound)
}
if errors.Is(err, qqcache.ErrWrongTypeHGet) {
w.WriteHeader(http.StatusBadRequest)
JSON(w, map[string]string{"error": err.Error()})
}
return
}
w.WriteHeader(http.StatusOK)
JSON(w, map[string]interface{}{"value": v})
}
}