-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxying.go
42 lines (35 loc) · 992 Bytes
/
proxying.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
package main
import (
"context"
"errors"
"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
)
type proxymw struct {
next StringService // Serve most requests via this service...
uppercase endpoint.Endpoint // ...except Uppercase, which gets served by this endpoint
}
func (mw proxymw) Uppercase(ctx context.Context, s string) (string, error) {
response, err := mw.uppercase(ctx, uppercaseRequest{S: s})
if err != nil {
return "", err
}
resp := response.(uppercaseResponse)
if resp.Err != "" {
return resp.V, errors.New(resp.Err)
}
return resp.V, nil
}
func proxyingMiddleware(proxyURL string) ServiceMiddleware {
return func(next StringService) StringService {
return proxymw{next, makeUppercaseEndpoint(proxyURL)}
}
}
func makeUppercaseEndpoint(proxyURL string) endpoint.Endpoint {
return httptransport.NewClient(
"GET",
mustParseURL(proxyURL),
encodeUppercaseRequest,
decodeUppercaseResponse,
).Endpoint()
}