diff --git a/examples/addsvc/cmd/addsvc/main.go b/examples/addsvc/cmd/addsvc/main.go index 3fba0ada3..f2079f8bc 100644 --- a/examples/addsvc/cmd/addsvc/main.go +++ b/examples/addsvc/cmd/addsvc/main.go @@ -207,7 +207,7 @@ func main() { // HTTP transport. go func() { logger := log.NewContext(logger).With("transport", "HTTP") - h := addsvc.MakeHTTPHandler(ctx, endpoints, tracer, logger) + h := addsvc.MakeHTTPHandler(endpoints, tracer, logger) logger.Log("addr", *httpAddr) errc <- http.ListenAndServe(*httpAddr, h) }() diff --git a/examples/addsvc/transport_http.go b/examples/addsvc/transport_http.go index 2452fdad7..75a9b839b 100644 --- a/examples/addsvc/transport_http.go +++ b/examples/addsvc/transport_http.go @@ -20,21 +20,19 @@ import ( // MakeHTTPHandler returns a handler that makes a set of endpoints available // on predefined paths. -func MakeHTTPHandler(ctx context.Context, endpoints Endpoints, tracer stdopentracing.Tracer, logger log.Logger) http.Handler { +func MakeHTTPHandler(endpoints Endpoints, tracer stdopentracing.Tracer, logger log.Logger) http.Handler { options := []httptransport.ServerOption{ httptransport.ServerErrorEncoder(errorEncoder), httptransport.ServerErrorLogger(logger), } m := http.NewServeMux() m.Handle("/sum", httptransport.NewServer( - ctx, endpoints.SumEndpoint, DecodeHTTPSumRequest, EncodeHTTPGenericResponse, append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "Sum", logger)))..., )) m.Handle("/concat", httptransport.NewServer( - ctx, endpoints.ConcatEndpoint, DecodeHTTPConcatRequest, EncodeHTTPGenericResponse, diff --git a/examples/apigateway/main.go b/examples/apigateway/main.go index 610418c76..76aa5f905 100644 --- a/examples/apigateway/main.go +++ b/examples/apigateway/main.go @@ -104,7 +104,7 @@ func main() { // HTTP handler, and just install it under a particular path prefix in // our router. - r.PathPrefix("/addsvc").Handler(http.StripPrefix("/addsvc", addsvc.MakeHTTPHandler(ctx, endpoints, tracer, logger))) + r.PathPrefix("/addsvc").Handler(http.StripPrefix("/addsvc", addsvc.MakeHTTPHandler(endpoints, tracer, logger))) } // stringsvc routes. @@ -140,8 +140,8 @@ func main() { // have to do provide it with the encode and decode functions for our // stringsvc methods. - r.Handle("/stringsvc/uppercase", httptransport.NewServer(ctx, uppercase, decodeUppercaseRequest, encodeJSONResponse)) - r.Handle("/stringsvc/count", httptransport.NewServer(ctx, count, decodeCountRequest, encodeJSONResponse)) + r.Handle("/stringsvc/uppercase", httptransport.NewServer(uppercase, decodeUppercaseRequest, encodeJSONResponse)) + r.Handle("/stringsvc/count", httptransport.NewServer(count, decodeCountRequest, encodeJSONResponse)) } // Interrupt handler. diff --git a/examples/profilesvc/cmd/profilesvc/main.go b/examples/profilesvc/cmd/profilesvc/main.go index 0cc9cd0e3..895ebb3b1 100644 --- a/examples/profilesvc/cmd/profilesvc/main.go +++ b/examples/profilesvc/cmd/profilesvc/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "flag" "fmt" "net/http" @@ -26,11 +25,6 @@ func main() { logger = log.NewContext(logger).With("caller", log.DefaultCaller) } - var ctx context.Context - { - ctx = context.Background() - } - var s profilesvc.Service { s = profilesvc.NewInmemService() @@ -39,7 +33,7 @@ func main() { var h http.Handler { - h = profilesvc.MakeHTTPHandler(ctx, s, log.NewContext(logger).With("component", "HTTP")) + h = profilesvc.MakeHTTPHandler(s, log.NewContext(logger).With("component", "HTTP")) } errs := make(chan error) diff --git a/examples/profilesvc/transport.go b/examples/profilesvc/transport.go index 52aa017a0..a0136ee4e 100644 --- a/examples/profilesvc/transport.go +++ b/examples/profilesvc/transport.go @@ -25,7 +25,7 @@ var ( // MakeHTTPHandler mounts all of the service endpoints into an http.Handler. // Useful in a profilesvc server. -func MakeHTTPHandler(ctx context.Context, s Service, logger log.Logger) http.Handler { +func MakeHTTPHandler(s Service, logger log.Logger) http.Handler { r := mux.NewRouter() e := MakeServerEndpoints(s) options := []httptransport.ServerOption{ @@ -44,63 +44,54 @@ func MakeHTTPHandler(ctx context.Context, s Service, logger log.Logger) http.Han // DELETE /profiles/:id/addresses/:addressID remove an address r.Methods("POST").Path("/profiles/").Handler(httptransport.NewServer( - ctx, e.PostProfileEndpoint, decodePostProfileRequest, encodeResponse, options..., )) r.Methods("GET").Path("/profiles/{id}").Handler(httptransport.NewServer( - ctx, e.GetProfileEndpoint, decodeGetProfileRequest, encodeResponse, options..., )) r.Methods("PUT").Path("/profiles/{id}").Handler(httptransport.NewServer( - ctx, e.PutProfileEndpoint, decodePutProfileRequest, encodeResponse, options..., )) r.Methods("PATCH").Path("/profiles/{id}").Handler(httptransport.NewServer( - ctx, e.PatchProfileEndpoint, decodePatchProfileRequest, encodeResponse, options..., )) r.Methods("DELETE").Path("/profiles/{id}").Handler(httptransport.NewServer( - ctx, e.DeleteProfileEndpoint, decodeDeleteProfileRequest, encodeResponse, options..., )) r.Methods("GET").Path("/profiles/{id}/addresses/").Handler(httptransport.NewServer( - ctx, e.GetAddressesEndpoint, decodeGetAddressesRequest, encodeResponse, options..., )) r.Methods("GET").Path("/profiles/{id}/addresses/{addressID}").Handler(httptransport.NewServer( - ctx, e.GetAddressEndpoint, decodeGetAddressRequest, encodeResponse, options..., )) r.Methods("POST").Path("/profiles/{id}/addresses/").Handler(httptransport.NewServer( - ctx, e.PostAddressEndpoint, decodePostAddressRequest, encodeResponse, options..., )) r.Methods("DELETE").Path("/profiles/{id}/addresses/{addressID}").Handler(httptransport.NewServer( - ctx, e.DeleteAddressEndpoint, decodeDeleteAddressRequest, encodeResponse, diff --git a/examples/shipping/booking/transport.go b/examples/shipping/booking/transport.go index 23f8b8aff..6592a9d1b 100644 --- a/examples/shipping/booking/transport.go +++ b/examples/shipping/booking/transport.go @@ -17,56 +17,49 @@ import ( ) // MakeHandler returns a handler for the booking service. -func MakeHandler(ctx context.Context, bs Service, logger kitlog.Logger) http.Handler { +func MakeHandler(bs Service, logger kitlog.Logger) http.Handler { opts := []kithttp.ServerOption{ kithttp.ServerErrorLogger(logger), kithttp.ServerErrorEncoder(encodeError), } bookCargoHandler := kithttp.NewServer( - ctx, makeBookCargoEndpoint(bs), decodeBookCargoRequest, encodeResponse, opts..., ) loadCargoHandler := kithttp.NewServer( - ctx, makeLoadCargoEndpoint(bs), decodeLoadCargoRequest, encodeResponse, opts..., ) requestRoutesHandler := kithttp.NewServer( - ctx, makeRequestRoutesEndpoint(bs), decodeRequestRoutesRequest, encodeResponse, opts..., ) assignToRouteHandler := kithttp.NewServer( - ctx, makeAssignToRouteEndpoint(bs), decodeAssignToRouteRequest, encodeResponse, opts..., ) changeDestinationHandler := kithttp.NewServer( - ctx, makeChangeDestinationEndpoint(bs), decodeChangeDestinationRequest, encodeResponse, opts..., ) listCargosHandler := kithttp.NewServer( - ctx, makeListCargosEndpoint(bs), decodeListCargosRequest, encodeResponse, opts..., ) listLocationsHandler := kithttp.NewServer( - ctx, makeListLocationsEndpoint(bs), decodeListLocationsRequest, encodeResponse, diff --git a/examples/shipping/handling/transport.go b/examples/shipping/handling/transport.go index 9c59aa435..065bda1d2 100644 --- a/examples/shipping/handling/transport.go +++ b/examples/shipping/handling/transport.go @@ -17,7 +17,7 @@ import ( ) // MakeHandler returns a handler for the handling service. -func MakeHandler(ctx context.Context, hs Service, logger kitlog.Logger) http.Handler { +func MakeHandler(hs Service, logger kitlog.Logger) http.Handler { r := mux.NewRouter() opts := []kithttp.ServerOption{ @@ -26,7 +26,6 @@ func MakeHandler(ctx context.Context, hs Service, logger kitlog.Logger) http.Han } registerIncidentHandler := kithttp.NewServer( - ctx, makeRegisterIncidentEndpoint(hs), decodeRegisterIncidentRequest, encodeResponse, diff --git a/examples/shipping/main.go b/examples/shipping/main.go index 98adfd2d5..812302810 100644 --- a/examples/shipping/main.go +++ b/examples/shipping/main.go @@ -137,9 +137,9 @@ func main() { mux := http.NewServeMux() - mux.Handle("/booking/v1/", booking.MakeHandler(ctx, bs, httpLogger)) - mux.Handle("/tracking/v1/", tracking.MakeHandler(ctx, ts, httpLogger)) - mux.Handle("/handling/v1/", handling.MakeHandler(ctx, hs, httpLogger)) + mux.Handle("/booking/v1/", booking.MakeHandler(bs, httpLogger)) + mux.Handle("/tracking/v1/", tracking.MakeHandler(ts, httpLogger)) + mux.Handle("/handling/v1/", handling.MakeHandler(hs, httpLogger)) http.Handle("/", accessControl(mux)) http.Handle("/metrics", stdprometheus.Handler()) diff --git a/examples/shipping/tracking/transport.go b/examples/shipping/tracking/transport.go index 497aa36ea..13d17f25c 100644 --- a/examples/shipping/tracking/transport.go +++ b/examples/shipping/tracking/transport.go @@ -15,7 +15,7 @@ import ( ) // MakeHandler returns a handler for the tracking service. -func MakeHandler(ctx context.Context, ts Service, logger kitlog.Logger) http.Handler { +func MakeHandler(ts Service, logger kitlog.Logger) http.Handler { r := mux.NewRouter() opts := []kithttp.ServerOption{ @@ -24,7 +24,6 @@ func MakeHandler(ctx context.Context, ts Service, logger kitlog.Logger) http.Han } trackCargoHandler := kithttp.NewServer( - ctx, makeTrackCargoEndpoint(ts), decodeTrackCargoRequest, encodeResponse, diff --git a/examples/stringsvc1/main.go b/examples/stringsvc1/main.go index 007b884bd..03fc79396 100644 --- a/examples/stringsvc1/main.go +++ b/examples/stringsvc1/main.go @@ -32,18 +32,15 @@ func (stringService) Count(s string) int { } func main() { - ctx := context.Background() svc := stringService{} uppercaseHandler := httptransport.NewServer( - ctx, makeUppercaseEndpoint(svc), decodeUppercaseRequest, encodeResponse, ) countHandler := httptransport.NewServer( - ctx, makeCountEndpoint(svc), decodeCountRequest, encodeResponse, diff --git a/examples/stringsvc2/main.go b/examples/stringsvc2/main.go index 9958fab66..d67d2a549 100644 --- a/examples/stringsvc2/main.go +++ b/examples/stringsvc2/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "net/http" "os" @@ -13,7 +12,6 @@ import ( ) func main() { - ctx := context.Background() logger := log.NewLogfmtLogger(os.Stderr) fieldKeys := []string{"method", "error"} @@ -42,14 +40,12 @@ func main() { svc = instrumentingMiddleware{requestCount, requestLatency, countResult, svc} uppercaseHandler := httptransport.NewServer( - ctx, makeUppercaseEndpoint(svc), decodeUppercaseRequest, encodeResponse, ) countHandler := httptransport.NewServer( - ctx, makeCountEndpoint(svc), decodeCountRequest, encodeResponse, diff --git a/examples/stringsvc3/main.go b/examples/stringsvc3/main.go index 4b869b259..86bb1df95 100644 --- a/examples/stringsvc3/main.go +++ b/examples/stringsvc3/main.go @@ -24,8 +24,6 @@ func main() { logger = log.NewLogfmtLogger(os.Stderr) logger = log.NewContext(logger).With("listen", *listen).With("caller", log.DefaultCaller) - ctx := context.Background() - fieldKeys := []string{"method", "error"} requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "my_group", @@ -48,18 +46,16 @@ func main() { var svc StringService svc = stringService{} - svc = proxyingMiddleware(*proxy, ctx, logger)(svc) + svc = proxyingMiddleware(context.Background(), *proxy, logger)(svc) svc = loggingMiddleware(logger)(svc) svc = instrumentingMiddleware(requestCount, requestLatency, countResult)(svc) uppercaseHandler := httptransport.NewServer( - ctx, makeUppercaseEndpoint(svc), decodeUppercaseRequest, encodeResponse, ) countHandler := httptransport.NewServer( - ctx, makeCountEndpoint(svc), decodeCountRequest, encodeResponse, diff --git a/examples/stringsvc3/proxying.go b/examples/stringsvc3/proxying.go index d7c837f87..8d67d3858 100644 --- a/examples/stringsvc3/proxying.go +++ b/examples/stringsvc3/proxying.go @@ -20,7 +20,7 @@ import ( httptransport "github.com/go-kit/kit/transport/http" ) -func proxyingMiddleware(instances string, ctx context.Context, logger log.Logger) ServiceMiddleware { +func proxyingMiddleware(ctx context.Context, instances string, logger log.Logger) ServiceMiddleware { // If instances is empty, don't proxy. if instances == "" { logger.Log("proxy_to", "none") diff --git a/transport/http/example_test.go b/transport/http/example_test.go index 14153610c..3889354af 100644 --- a/transport/http/example_test.go +++ b/transport/http/example_test.go @@ -9,7 +9,6 @@ import ( func ExamplePopulateRequestContext() { handler := NewServer( - context.Background(), func(ctx context.Context, request interface{}) (response interface{}, err error) { fmt.Println("Method", ctx.Value(ContextKeyRequestMethod).(string)) fmt.Println("RequestPath", ctx.Value(ContextKeyRequestPath).(string)) diff --git a/transport/http/server.go b/transport/http/server.go index de109beff..0bea793f9 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -11,7 +11,6 @@ import ( // Server wraps an endpoint and implements http.Handler. type Server struct { - ctx context.Context e endpoint.Endpoint dec DecodeRequestFunc enc EncodeResponseFunc @@ -25,14 +24,12 @@ type Server struct { // NewServer constructs a new server, which implements http.Server and wraps // the provided endpoint. func NewServer( - ctx context.Context, e endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, ) *Server { s := &Server{ - ctx: ctx, e: e, dec: dec, enc: enc, @@ -85,7 +82,7 @@ func ServerFinalizer(f ServerFinalizerFunc) ServerOption { // ServeHTTP implements http.Handler. func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx := s.ctx + ctx := r.Context() if s.finalizer != nil { iw := &interceptingWriter{w, http.StatusOK, 0} diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 51e311ee8..ddc40c97f 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -16,7 +16,6 @@ import ( func TestServerBadDecode(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, errors.New("dang") }, func(context.Context, http.ResponseWriter, interface{}) error { return nil }, @@ -31,7 +30,6 @@ func TestServerBadDecode(t *testing.T) { func TestServerBadEndpoint(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return struct{}{}, errors.New("dang") }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(context.Context, http.ResponseWriter, interface{}) error { return nil }, @@ -46,7 +44,6 @@ func TestServerBadEndpoint(t *testing.T) { func TestServerBadEncode(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(context.Context, http.ResponseWriter, interface{}) error { return errors.New("dang") }, @@ -68,7 +65,6 @@ func TestServerErrorEncoder(t *testing.T) { return http.StatusInternalServerError } handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return struct{}{}, errTeapot }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(context.Context, http.ResponseWriter, interface{}) error { return nil }, @@ -83,7 +79,7 @@ func TestServerErrorEncoder(t *testing.T) { } func TestServerHappyPath(t *testing.T) { - _, step, response := testServer(t) + step, response := testServer(t) step() resp := <-response defer resp.Body.Close() @@ -93,7 +89,6 @@ func TestServerHappyPath(t *testing.T) { } } - func TestMultipleServerBefore(t *testing.T) { var ( headerKey = "X-Henlo-Lizer" @@ -103,7 +98,6 @@ func TestMultipleServerBefore(t *testing.T) { done = make(chan struct{}) ) handler := httptransport.NewServer( - context.Background(), endpoint.Nop, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil @@ -149,7 +143,6 @@ func TestMultipleServerAfter(t *testing.T) { done = make(chan struct{}) ) handler := httptransport.NewServer( - context.Background(), endpoint.Nop, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil @@ -195,7 +188,6 @@ func TestServerFinalizer(t *testing.T) { done = make(chan struct{}) ) handler := httptransport.NewServer( - context.Background(), endpoint.Nop, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil @@ -245,7 +237,6 @@ func (e enhancedResponse) Headers() http.Header { return http.Header{"X-Edward": func TestEncodeJSONResponse(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return enhancedResponse{Foo: "bar"}, nil }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, httptransport.EncodeJSONResponse, @@ -276,7 +267,6 @@ func (e noContentResponse) StatusCode() int { return http.StatusNoContent } func TestEncodeNoContent(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return noContentResponse{}, nil }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, httptransport.EncodeJSONResponse, @@ -307,7 +297,6 @@ func (e enhancedError) Headers() http.Header { return http.Header{"X-Enh func TestEnhancedError(t *testing.T) { handler := httptransport.NewServer( - context.Background(), func(context.Context, interface{}) (interface{}, error) { return nil, enhancedError{} }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(_ context.Context, w http.ResponseWriter, _ interface{}) error { return nil }, @@ -333,14 +322,12 @@ func TestEnhancedError(t *testing.T) { } } -func testServer(t *testing.T) (cancel, step func(), resp <-chan *http.Response) { +func testServer(t *testing.T) (step func(), resp <-chan *http.Response) { var ( - ctx, cancelfn = context.WithCancel(context.Background()) - stepch = make(chan bool) - endpoint = func(context.Context, interface{}) (interface{}, error) { <-stepch; return struct{}{}, nil } - response = make(chan *http.Response) - handler = httptransport.NewServer( - ctx, + stepch = make(chan bool) + endpoint = func(context.Context, interface{}) (interface{}, error) { <-stepch; return struct{}{}, nil } + response = make(chan *http.Response) + handler = httptransport.NewServer( endpoint, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(context.Context, http.ResponseWriter, interface{}) error { return nil }, @@ -358,5 +345,5 @@ func testServer(t *testing.T) (cancel, step func(), resp <-chan *http.Response) } response <- resp }() - return cancelfn, func() { stepch <- true }, response + return func() { stepch <- true }, response } diff --git a/transport/httprp/server.go b/transport/httprp/server.go index 5c3ad1baf..b0a1ee3cd 100644 --- a/transport/httprp/server.go +++ b/transport/httprp/server.go @@ -14,7 +14,6 @@ type RequestFunc func(context.Context, *http.Request) context.Context // Server is a proxying request handler. type Server struct { - ctx context.Context proxy http.Handler before []RequestFunc errorEncoder func(w http.ResponseWriter, err error) @@ -25,12 +24,10 @@ type Server struct { // If the target's path is "/base" and the incoming request was for "/dir", // the target request will be for /base/dir. func NewServer( - ctx context.Context, baseURL *url.URL, options ...ServerOption, ) *Server { s := &Server{ - ctx: ctx, proxy: httputil.NewSingleHostReverseProxy(baseURL), } for _, option := range options { @@ -50,7 +47,7 @@ func ServerBefore(before ...RequestFunc) ServerOption { // ServeHTTP implements http.Handler. func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx := s.ctx + ctx := r.Context() for _, f := range s.before { ctx = f(ctx, r) diff --git a/transport/httprp/server_test.go b/transport/httprp/server_test.go index 47d6f77f0..fc5357601 100644 --- a/transport/httprp/server_test.go +++ b/transport/httprp/server_test.go @@ -20,7 +20,6 @@ func TestServerHappyPathSingleServer(t *testing.T) { originURL, _ := url.Parse(originServer.URL) handler := httptransport.NewServer( - context.Background(), originURL, ) proxyServer := httptest.NewServer(handler) @@ -55,7 +54,6 @@ func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) { originURL, _ := url.Parse(originServer.URL) handler := httptransport.NewServer( - context.Background(), originURL, httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context { r.Header.Add(headerKey, headerVal) @@ -82,7 +80,6 @@ func TestServerOriginServerNotFoundResponse(t *testing.T) { originURL, _ := url.Parse(originServer.URL) handler := httptransport.NewServer( - context.Background(), originURL, ) proxyServer := httptest.NewServer(handler) @@ -103,7 +100,6 @@ func TestServerOriginServerUnreachable(t *testing.T) { originServer.Close() handler := httptransport.NewServer( - context.Background(), originURL, ) proxyServer := httptest.NewServer(handler) @@ -138,7 +134,6 @@ func TestMultipleServerBefore(t *testing.T) { originURL, _ := url.Parse(originServer.URL) handler := httptransport.NewServer( - context.Background(), originURL, httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context { r.Header.Add(headerKey, headerVal)