Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to define a custom default response encoder #11

Merged
merged 10 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kitty

import kithttp "github.com/go-kit/kit/transport/http"

// Config holds configuration info for kitty.HTTPTransport.
type Config struct {
// LivenessCheckPath is the path of the health handler (default: "/alivez").
Expand All @@ -10,6 +12,8 @@ type Config struct {
HTTPPort int
// EnablePProf enables pprof urls (default: false).
EnablePProf bool
// EncodeResponse overrides go-kit reponse encoder without to have to pass the encoder to each endpoint, but is still overrided by the endpoint's encoder
b-2-83 marked this conversation as resolved.
Show resolved Hide resolved
EncodeResponse kithttp.EncodeResponseFunc
}

// DefaultConfig defines the default config of kitty.HTTPTransport.
Expand All @@ -18,4 +22,5 @@ var DefaultConfig = Config{
LivenessCheckPath: "/alivez",
ReadinessCheckPath: "/readyz",
EnablePProf: false,
EncodeResponse: kithttp.EncodeJSONResponse,
}
1 change: 0 additions & 1 deletion endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (t *HTTPTransport) Endpoint(method, path string, ep endpoint.Endpoint, opts
path: path,
endpoint: ep,
decoder: kithttp.NopRequestDecoder,
encoder: kithttp.EncodeJSONResponse,
}
for _, opt := range opts {
e = opt(e)
Expand Down
7 changes: 6 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewHTTPTransport(cfg Config) *HTTPTransport {
t.cfg.ReadinessCheckPath = cfg.ReadinessCheckPath
}
t.cfg.EnablePProf = cfg.EnablePProf
t.cfg.EncodeResponse = cfg.EncodeResponse
b-2-83 marked this conversation as resolved.
Show resolved Hide resolved
return t
}

Expand All @@ -80,11 +81,15 @@ func (t *HTTPTransport) RegisterEndpoints(m endpoint.Middleware) error {

// register endpoints
for _, ep := range t.endpoints {
encoder := t.cfg.EncodeResponse
if ep.encoder != nil {
encoder = ep.encoder
}
t.mux.Handle(ep.method, ep.path,
kithttp.NewServer(
m(ep.endpoint),
ep.decoder,
ep.encoder,
encoder,
append(opts, ep.options...)...))
}

Expand Down