diff --git a/booking/endpoint.go b/booking/endpoint.go index 5e49f9f..f4b4f55 100644 --- a/booking/endpoint.go +++ b/booking/endpoint.go @@ -43,10 +43,10 @@ type loadCargoResponse struct { func (r loadCargoResponse) error() error { return r.Err } -func makeLoadCargoEndpoint(bs Service) endpoint.Endpoint { +func makeLoadCargoEndpoint(s Service) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (interface{}, error) { req := request.(loadCargoRequest) - c, err := bs.LoadCargo(req.ID) + c, err := s.LoadCargo(req.ID) return loadCargoResponse{Cargo: &c, Err: err}, nil } } diff --git a/booking/transport.go b/booking/transport.go index b96b276..c048488 100644 --- a/booking/transport.go +++ b/booking/transport.go @@ -1,73 +1,64 @@ package booking import ( + "context" "encoding/json" "errors" "net/http" "time" - "context" - - "github.com/gorilla/mux" - kitlog "github.com/go-kit/kit/log" kithttp "github.com/go-kit/kit/transport/http" + "github.com/gorilla/mux" "github.com/marcusolsson/goddd/cargo" "github.com/marcusolsson/goddd/location" ) // 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, @@ -188,6 +179,7 @@ type errorer interface { // encode errors from business-logic func encodeError(_ context.Context, err error, w http.ResponseWriter) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") switch err { case cargo.ErrUnknown: w.WriteHeader(http.StatusNotFound) @@ -196,7 +188,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) { default: w.WriteHeader(http.StatusInternalServerError) } - w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]interface{}{ "error": err.Error(), }) diff --git a/handling/transport.go b/handling/transport.go index f221fcd..38a4c42 100644 --- a/handling/transport.go +++ b/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, @@ -87,6 +86,7 @@ type errorer interface { // encode errors from business-logic func encodeError(_ context.Context, err error, w http.ResponseWriter) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") switch err { case cargo.ErrUnknown: w.WriteHeader(http.StatusNotFound) @@ -95,7 +95,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) { default: w.WriteHeader(http.StatusInternalServerError) } - w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]interface{}{ "error": err.Error(), }) diff --git a/main.go b/main.go index 65f1f14..0e5bb96 100644 --- a/main.go +++ b/main.go @@ -7,11 +7,11 @@ import ( "net/http" "os" "os/signal" - "sync" "syscall" "time" stdprometheus "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "gopkg.in/mgo.v2" "github.com/go-kit/kit/log" @@ -55,9 +55,8 @@ func main() { flag.Parse() var logger log.Logger - logger = log.NewLogfmtLogger(os.Stderr) - logger = &serializedLogger{Logger: logger} - logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC) + logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) + logger = log.With(logger, "ts", log.DefaultTimestampUTC) // Setup repositories var ( @@ -109,7 +108,7 @@ func main() { var bs booking.Service bs = booking.NewService(cargos, locations, handlingEvents, rs) - bs = booking.NewLoggingService(log.NewContext(logger).With("component", "booking"), bs) + bs = booking.NewLoggingService(log.With(logger, "component", "booking"), bs) bs = booking.NewInstrumentingService( kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "api", @@ -128,7 +127,7 @@ func main() { var ts tracking.Service ts = tracking.NewService(cargos, handlingEvents) - ts = tracking.NewLoggingService(log.NewContext(logger).With("component", "tracking"), ts) + ts = tracking.NewLoggingService(log.With(logger, "component", "tracking"), ts) ts = tracking.NewInstrumentingService( kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "api", @@ -147,7 +146,7 @@ func main() { var hs handling.Service hs = handling.NewService(handlingEvents, handlingEventFactory, handlingEventHandler) - hs = handling.NewLoggingService(log.NewContext(logger).With("component", "handling"), hs) + hs = handling.NewLoggingService(log.With(logger, "component", "handling"), hs) hs = handling.NewInstrumentingService( kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "api", @@ -164,16 +163,16 @@ func main() { hs, ) - httpLogger := log.NewContext(logger).With("component", "http") + httpLogger := log.With(logger, "component", "http") 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()) + http.Handle("/metrics", promhttp.Handler()) errs := make(chan error, 2) go func() { @@ -230,14 +229,3 @@ func storeTestData(r cargo.Repository) { panic(err) } } - -type serializedLogger struct { - mtx sync.Mutex - log.Logger -} - -func (l *serializedLogger) Log(keyvals ...interface{}) error { - l.mtx.Lock() - defer l.mtx.Unlock() - return l.Logger.Log(keyvals...) -} diff --git a/tracking/transport.go b/tracking/transport.go index 2b4a3a6..ca33f20 100644 --- a/tracking/transport.go +++ b/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, @@ -61,6 +60,7 @@ type errorer interface { // encode errors from business-logic func encodeError(_ context.Context, err error, w http.ResponseWriter) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") switch err { case cargo.ErrUnknown: w.WriteHeader(http.StatusNotFound) @@ -69,7 +69,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) { default: w.WriteHeader(http.StatusInternalServerError) } - w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]interface{}{ "error": err.Error(), }) diff --git a/tracking/transport_test.go b/tracking/transport_test.go index 130d020..79e987c 100644 --- a/tracking/transport_test.go +++ b/tracking/transport_test.go @@ -9,8 +9,6 @@ import ( "testing" "time" - "context" - "github.com/go-kit/kit/log" "github.com/marcusolsson/goddd/cargo" @@ -35,11 +33,9 @@ func TestTrackCargo(t *testing.T) { cargos.Store(c) - ctx := context.Background() - logger := log.NewLogfmtLogger(ioutil.Discard) - h := MakeHandler(ctx, s, logger) + h := MakeHandler(s, logger) req, _ := http.NewRequest("GET", "http://example.com/tracking/v1/cargos/TEST", nil) rec := httptest.NewRecorder() @@ -91,11 +87,9 @@ func TestTrackUnknownCargo(t *testing.T) { s := NewService(&cargos, &events) - ctx := context.Background() - logger := log.NewLogfmtLogger(ioutil.Discard) - h := MakeHandler(ctx, s, logger) + h := MakeHandler(s, logger) req, _ := http.NewRequest("GET", "http://example.com/tracking/v1/cargos/not_found", nil) rec := httptest.NewRecorder()