forked from ByteStorage/FlyDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.go
152 lines (135 loc) · 3.26 KB
/
http_server.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
package http_handler
import (
"encoding/json"
"github.com/ByteStorage/FlyDB/db/engine"
"io"
"net/http"
)
type HttpHandler struct {
*engine.DB
}
func NewHttpHandler(DB *engine.DB) *HttpHandler {
return &HttpHandler{DB: DB}
}
// PutHandler 支持http进行Put
func (hs *HttpHandler) PutHandler(w http.ResponseWriter, r *http.Request) {
type PutRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}
var putReq PutRequest
err := json.NewDecoder(r.Body).Decode(&putReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if putReq.Key == "" {
http.Error(w, "key is empty", http.StatusBadRequest)
return
}
if putReq.Value == "" {
http.Error(w, "value is empty", http.StatusBadRequest)
return
}
err = hs.Put([]byte(putReq.Key), []byte(putReq.Value))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write([]byte("ok"))
if err != nil {
return
}
}
// DelHandler 支持http进行Delete
func (hs *HttpHandler) DelHandler(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
if key == "" {
http.Error(w, "key is empty", http.StatusBadRequest)
return
}
err := hs.Delete([]byte(key))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write([]byte("ok"))
if err != nil {
return
}
}
// GetHandler 支持http进行Get
func (hs *HttpHandler) GetHandler(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
if key == "" {
http.Error(w, "key is empty", http.StatusBadRequest)
return
}
val, err := hs.Get([]byte(key))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(val)
if err != nil {
return
}
}
// PostHandler 支持http进行Post
func (hs *HttpHandler) PostHandler(w http.ResponseWriter, r *http.Request) {
type PostRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}
var postReq PostRequest
err := json.NewDecoder(r.Body).Decode(&postReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(r.Body)
if postReq.Key == "" {
http.Error(w, "key is empty", http.StatusBadRequest)
return
}
if postReq.Value == "" {
http.Error(w, "value is empty", http.StatusBadRequest)
return
}
err = hs.Put([]byte(postReq.Key), []byte(postReq.Value))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write([]byte("ok"))
if err != nil {
// 处理写入响应失败的错误
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// GetListKeysHandler 支持http获取数据库中所有键
func (hs *HttpHandler) GetListKeysHandler(w http.ResponseWriter, r *http.Request) {
keys := hs.GetListKeys()
if keys == nil {
http.Error(w, "key is empty", http.StatusBadRequest)
return
}
jsonKeys, err := json.Marshal(keys)
if err != nil {
// Handle the error
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Write the JSON response
_, err = w.Write(jsonKeys)
if err != nil {
// 处理写入响应失败的错误
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}