http_binding.go 737 B

12345678910111213141516171819202122232425
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/go-kit/kit/endpoint"
  6. httptransport "github.com/go-kit/kit/transport/http"
  7. "golang.org/x/net/context"
  8. )
  9. func makeHTTPBinding(ctx context.Context, e endpoint.Endpoint, reqDecoder httptransport.DecodeFunc, before []httptransport.BeforeFunc, after []httptransport.AfterFunc) http.Handler {
  10. encode := func(w http.ResponseWriter, response interface{}) error {
  11. return json.NewEncoder(w).Encode(response)
  12. }
  13. return httptransport.Server{
  14. Context: ctx,
  15. Endpoint: e,
  16. DecodeFunc: reqDecoder,
  17. EncodeFunc: encode,
  18. Before: before,
  19. After: append([]httptransport.AfterFunc{httptransport.SetContentType("application/json; charset=utf-8")}, after...),
  20. }
  21. }