This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendpoints.go
152 lines (132 loc) · 3.98 KB
/
endpoints.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 simplejson
import (
"encoding/json"
"github.com/go-http-utils/headers"
"net/http"
"sort"
)
func (s *Server) Search(w http.ResponseWriter, _ *http.Request) {
var targets []string
for target := range s.Handlers {
targets = append(targets, target)
}
sort.Strings(targets)
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.ContentType, "application/json")
output, _ := json.Marshal(targets)
_, _ = w.Write(output)
}
func (s *Server) Query(w http.ResponseWriter, req *http.Request) {
var request QueryRequest
handleEndpoint(w, req, &request, func() ([]json.Marshaler, error) {
return s.handleQuery(req.Context(), request)
})
}
func (s *Server) Annotations(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodOptions {
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.AccessControlAllowHeaders, "accept, content-type")
w.Header().Set(headers.AccessControlAllowMethods, "POST")
w.Header().Set(headers.AccessControlAllowOrigin, "*")
return
}
var request AnnotationRequest
handleEndpoint(w, req, &request, func() ([]json.Marshaler, error) {
var annotations []Annotation
for _, h := range s.Handlers {
if h.Endpoints().Annotations != nil {
if newAnnotations, err := h.Endpoints().Annotations(request); err == nil {
annotations = append(annotations, newAnnotations...)
}
}
}
var response []json.Marshaler
for index := range annotations {
annotations[index].Request = request.Annotation
response = append(response, &annotations[index])
}
return response, nil
})
}
func (s *Server) TagKeys(w http.ResponseWriter, req *http.Request) {
handleEndpoint(w, req, nil, func() (keys []json.Marshaler, _ error) {
for _, handler := range s.Handlers {
if handler.Endpoints().TagKeys != nil {
for _, newKey := range handler.Endpoints().TagKeys(req.Context()) {
keys = append(keys, &tagKey{Type: "string", Text: newKey})
}
}
}
return keys, nil
})
}
func (s *Server) TagValues(w http.ResponseWriter, req *http.Request) {
var key valueKey
handleEndpoint(w, req, &key, func() ([]json.Marshaler, error) {
var response []json.Marshaler
for _, handler := range s.Handlers {
if handler.Endpoints().TagValues == nil {
continue
}
values, err := handler.Endpoints().TagValues(req.Context(), key.Key)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, err
}
for _, v := range values {
response = append(response, &value{Text: v})
}
}
return response, nil
})
}
type tagKey struct {
Type string `json:"type"`
Text string `json:"text"`
}
func (t tagKey) MarshalJSON() (b []byte, err error) {
type tagKey2 tagKey
var t2 = tagKey2(t)
return json.Marshal(t2)
}
type valueKey struct {
Key string `json:"key"`
}
func (r *valueKey) UnmarshalJSON(b []byte) (err error) {
type valueKey2 valueKey
var c valueKey2
err = json.Unmarshal(b, &c)
if err == nil {
*r = valueKey(c)
}
return nil
}
type value struct {
Text string `json:"text"`
}
func (v value) MarshalJSON() (b []byte, err error) {
type value2 value
var v2 = value2(v)
return json.Marshal(v2)
}
// handleEndpoint is a wrapper for simplejson endpoint handlers. It parses the incoming http.Request, calls the processor
// and writes the response to the http.ResponseWriter.
func handleEndpoint(w http.ResponseWriter, req *http.Request, request json.Unmarshaler, processor func() ([]json.Marshaler, error)) {
if req.ContentLength > 0 {
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(w, "failed to parse request: "+err.Error(), http.StatusBadRequest)
return
}
}
response, err := processor()
if err != nil {
http.Error(w, "failed to process request: "+err.Error(), http.StatusInternalServerError)
return
}
//w.WriteHeader(http.StatusOK)
w.Header().Set(headers.ContentType, "application/json")
if err = json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "failed to create response: "+err.Error(), http.StatusInternalServerError)
return
}
}