-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathendpoint.go
More file actions
82 lines (72 loc) · 2.15 KB
/
endpoint.go
File metadata and controls
82 lines (72 loc) · 2.15 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
package kitty
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
)
// httpendpoint encapsulates everything required to build
// an endpoint hosted on a kit server.
type httpendpoint struct {
method, path string
endpoint endpoint.Endpoint
decoder kithttp.DecodeRequestFunc
encoder kithttp.EncodeResponseFunc
options []kithttp.ServerOption
}
// HTTPEndpointOption is an option for an HTTP endpoint
type HTTPEndpointOption func(*httpendpoint) *httpendpoint
// Endpoint registers an endpoint to a kitty.HTTPTransport.
// Unless specified, NopRequestDecoder will decode the request (and do nothing),
// and EncodeJSONResponse will encode the response.
func (t *HTTPTransport) Endpoint(method, path string, ep endpoint.Endpoint, opts ...HTTPEndpointOption) *HTTPTransport {
e := &httpendpoint{
method: method,
path: path,
endpoint: ep,
decoder: kithttp.NopRequestDecoder,
}
for _, opt := range opts {
e = opt(e)
}
t.endpoints = append(t.endpoints, e)
return t
}
type decoderError struct {
error
}
func (e decoderError) StatusCode() int {
if err, ok := e.error.(kithttp.StatusCoder); ok {
return err.StatusCode()
}
return http.StatusBadRequest
}
// Decoder defines the request decoder for a HTTP endpoint.
// If none is provided, NopRequestDecoder is used.
func Decoder(dec kithttp.DecodeRequestFunc) HTTPEndpointOption {
return func(e *httpendpoint) *httpendpoint {
e.decoder = func(ctx context.Context, r *http.Request) (interface{}, error) {
request, err := dec(ctx, r)
if err != nil {
return nil, decoderError{error: err}
}
return request, nil
}
return e
}
}
// Encoder defines the response encoder for a HTTP endpoint.
// If none is provided, EncodeJSONResponse is used.
func Encoder(enc kithttp.EncodeResponseFunc) HTTPEndpointOption {
return func(e *httpendpoint) *httpendpoint {
e.encoder = enc
return e
}
}
// ServerOptions defines a liste of go-kit ServerOption to be used by a HTTP endpoint.
func ServerOptions(opts ...kithttp.ServerOption) HTTPEndpointOption {
return func(e *httpendpoint) *httpendpoint {
e.options = opts
return e
}
}