-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkv.go
138 lines (117 loc) · 2.94 KB
/
kv.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
package natssse
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"github.com/nats-io/nats.go"
)
const (
PUT Method = "PUT"
GET Method = "GET"
)
type Method string
func NewKVHandler(conn *nats.Conn, authFunc AuthFunc) http.HandlerFunc {
n := NatsContext{
Conn: conn,
Auth: authFunc,
}
return func(w http.ResponseWriter, r *http.Request) {
err := newKVHandler(w, r, n)
if err == nil {
return
}
e, ok := err.(ClientError)
if !ok {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(e.Code())
w.Write(e.Body())
return
}
}
func newKVHandler(w http.ResponseWriter, r *http.Request, nc NatsContext) error {
key := replacer(r, "key")
bucket := r.PathValue("bucket")
domain := r.URL.Query().Get("domain")
ok := nc.Auth(r.Header.Get("Authorization"), key)
if !ok {
return NewClientError(fmt.Errorf(http.StatusText(http.StatusUnauthorized)), http.StatusUnauthorized)
}
js, err := nc.Conn.JetStream(nats.Domain(domain))
if err != nil {
return err
}
kv, err := js.KeyValue(bucket)
if err != nil && errors.Is(err, nats.ErrBucketNotFound) {
return NewClientError(fmt.Errorf("bucket not found"), http.StatusNotFound)
}
if err != nil && errors.Is(err, nats.ErrKeyNotFound) {
return NewClientError(fmt.Errorf(http.StatusText(http.StatusNotFound)), http.StatusNotFound)
}
if err != nil && errors.Is(err, nats.ErrNoResponders) {
return NewClientError(fmt.Errorf(http.StatusText(http.StatusNotFound)), http.StatusNotFound)
}
if err != nil {
return err
}
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
switch r.Method {
case http.MethodPut:
err := putKV(key, kv, body)
if err != nil {
return err
}
w.WriteHeader(204)
case http.MethodGet:
data, err := getKV(key, kv)
if err != nil {
return err
}
w.Write(data)
case http.MethodDelete:
err := deleteKV(key, kv)
if err != nil {
return err
}
default:
return NewClientError(fmt.Errorf(http.StatusText(http.StatusMethodNotAllowed)), http.StatusMethodNotAllowed)
}
return nil
}
func getKV(key string, kv nats.KeyValue) ([]byte, error) {
resp, err := kv.Get(key)
if err != nil && errors.Is(err, nats.ErrKeyNotFound) {
return nil, NewClientError(fmt.Errorf(http.StatusText(http.StatusNotFound)), http.StatusNotFound)
}
if err != nil {
return nil, err
}
return resp.Value(), nil
}
func putKV(key string, kv nats.KeyValue, data []byte) error {
_, err := kv.Put(key, data)
if err != nil && errors.Is(err, nats.ErrKeyNotFound) {
return NewClientError(fmt.Errorf(http.StatusText(http.StatusNotFound)), http.StatusNotFound)
}
if err != nil {
return err
}
return nil
}
func deleteKV(key string, kv nats.KeyValue) error {
err := kv.Delete(key)
if err != nil && errors.Is(err, nats.ErrKeyNotFound) {
return NewClientError(fmt.Errorf(http.StatusText(http.StatusNotFound)), http.StatusNotFound)
}
if err != nil {
return err
}
return nil
}