-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
263 lines (228 loc) · 6.04 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"tailscale.com/client/tailscale"
)
type emptyRequest struct{}
func decodeEmptyRequest(_ context.Context, r *http.Request) (interface{}, error) {
return emptyRequest{}, nil
}
type emptyResponse struct {
Err error `json:"error,omitempty"`
}
type whoRequest struct {
remoteAddr string
}
func decodeWhoRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return whoRequest{
remoteAddr: r.RemoteAddr,
}, nil
}
type jsonResponse struct {
emptyResponse
Res any
}
type streamResponse struct {
emptyResponse
Res io.ReadCloser
}
func makeWhoEndpoint(s Service, lc *tailscale.LocalClient) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(whoRequest)
userProfile, err := s.Who(lc, req.remoteAddr)
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
Res: userProfile,
}, nil
}
}
func makeDatabasesEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
databases, err := s.Databases()
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
Res: databases,
}, nil
}
}
func makeApplicationsEndpoint(s Service, lc *tailscale.LocalClient) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
apps, err := s.RunningApplications(lc)
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
Res: apps,
}, nil
}
}
func makeJSONResponseEncoder(logger log.Logger) func(ctx context.Context, w http.ResponseWriter, res any) error {
return func(ctx context.Context, w http.ResponseWriter, response any) error {
res := response.(jsonResponse)
if res.Err != nil {
return encodeJSONResponse(ctx, w, res.Err)
}
// use res == nil to signal we want 201 CREATED instead
if res.Res == nil {
w.WriteHeader(http.StatusCreated)
return nil
}
return encodeJSONResponse(ctx, w, res.Res)
}
}
func makeStreamCARResponseEncoder(logger log.Logger) func(ctx context.Context, w http.ResponseWriter, res any) error {
return func(ctx context.Context, w http.ResponseWriter, response any) error {
res := response.(streamResponse)
if res.Err != nil {
return encodeJSONResponse(ctx, w, res.Err)
}
return streamCAR(ctx, w, res.Res)
}
}
type uploadDataRequest struct {
name string
car string
data io.ReadCloser
}
func decodeUploadDataRequest(ctx context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
return nil, fmt.Errorf("download request missing name")
}
car, ok := vars["car"]
if !ok {
return nil, fmt.Errorf("download request missing car")
}
return uploadDataRequest{
name: name,
car: car,
data: r.Body,
}, nil
}
func makeUploadDataEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(uploadDataRequest)
err := s.SetData(req.name, req.car, req.data)
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
}, nil
}
}
type downloadRequest struct {
name string
car string
}
func decodeDownloadRequest(ctx context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
return nil, fmt.Errorf("download request missing name")
}
car, ok := vars["car"]
if !ok {
return nil, fmt.Errorf("download request missing car")
}
return downloadRequest{
name: name,
car: car,
}, nil
}
func makeDownloadEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(downloadRequest)
data, err := s.Data(req.name, req.car)
return streamResponse{
emptyResponse: emptyResponse{Err: err},
Res: data,
}, nil
}
}
type uploadGetRequest struct {
typ string
name string
branch string
car string
}
func decodeUploadGetRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return uploadGetRequest{
typ: r.FormValue("type"),
name: r.FormValue("name"),
branch: r.FormValue("branch"),
car: r.FormValue("car"),
}, nil
}
func makeUploadGetEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(uploadGetRequest)
var err error
var res any
switch req.typ {
case "meta":
var metaItems []*MetaRequest
metaItems, err = s.Meta(req.name, req.branch)
wrapper := MetaItems{
Items: metaItems,
}
itemsJSON, err := json.Marshal(wrapper)
if err != nil {
return nil, fmt.Errorf("error building items wrapper json: %w", err)
}
res = MetaResponse{
Status: 200,
Body: string(itemsJSON),
}
case "data":
res, err = s.PrepareData(req.name, req.car)
default:
return nil, fmt.Errorf("unsupported upload type: %s", req.typ)
}
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
Res: res,
}, nil
}
}
type setMetaRequest struct {
typ string
name string
branch string
meta *MetaRequest
raw []byte
}
func decodeSetMetaRequest(ctx context.Context, r *http.Request) (interface{}, error) {
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("error reading body: %w", err)
}
var metaRequest MetaRequest
err = json.Unmarshal(bodyBytes, &metaRequest)
if err != nil {
return nil, fmt.Errorf("error parsing meta json: %w", err)
}
return setMetaRequest{
typ: r.FormValue("type"),
name: r.FormValue("name"),
branch: r.FormValue("branch"),
meta: &metaRequest,
raw: bodyBytes,
}, nil
}
func makeSetMetaEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(setMetaRequest)
err := s.SetMeta(req.name, req.branch, req.meta, req.raw)
return jsonResponse{
emptyResponse: emptyResponse{Err: err},
Res: MetaResponse{
Status: 201,
Body: `{}`,
},
}, nil
}
}