Repository navigation
Expand file tree
/
Copy pathendpoints.go
More file actions
113 lines (92 loc) · 2.57 KB
/
Copy pathendpoints.go
File metadata and controls
113 lines (92 loc) · 2.57 KB
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
package main
import (
"encoding/json"
"net/http"
)
type kvValuePayload struct {
Data string `json:"data"`
ParentVideo string `json:"parent_video"`
Metadata []string `json:"metadata"`
}
type kvRequest struct {
Key string `json:"key"`
Value kvValuePayload `json:"value"`
}
func (n *Node) put_chunk(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var req kvRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid JSON payload", http.StatusBadRequest)
return
}
if req.Key == "" {
http.Error(w, "missing key", http.StatusBadRequest)
return
}
descriptors := make(map[string]struct{}, len(req.Value.Metadata))
for _, metadata := range req.Value.Metadata {
descriptors[metadata] = struct{}{}
}
if !n.Put(req.Key, Value{Data: req.Value.Data, ParentVideo: req.Value.ParentVideo, Descriptors: descriptors}) {
http.Error(w, "failed to store key/value pair", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"ok":true}`))
}
func (n *Node) get_chunk(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
key := r.URL.Query().Get("key")
if key == "" {
http.Error(w, "missing key", http.StatusBadRequest)
return
}
ok, value := n.Get(key)
if !ok {
http.Error(w, "key not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(value.Data)
}
func (n *Node) search_chunk(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
tag := r.URL.Query().Get("tag")
// if key == "" {
// http.Error(w, "missing key", http.StatusBadRequest)
// return
// }
res := n.Search(tag)
// if !ok {
// http.Error(w, "key not found", http.StatusNotFound)
// return
// }
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}
func (n *Node) delete_chunk(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
key := r.URL.Query().Get("key")
if key == "" {
http.Error(w, "missing key", http.StatusBadRequest)
return
}
if !n.Delete(key) {
http.Error(w, "key not found", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}