Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Update with changes from gokit (thank you!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Olsson committed Aug 1, 2017
1 parent c521f89 commit 4e3e03a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 52 deletions.
4 changes: 2 additions & 2 deletions booking/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
17 changes: 4 additions & 13 deletions booking/transport.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
})
Expand Down
5 changes: 2 additions & 3 deletions handling/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -26,7 +26,6 @@ func MakeHandler(ctx context.Context, hs Service, logger kitlog.Logger) http.Han
}

registerIncidentHandler := kithttp.NewServer(
ctx,
makeRegisterIncidentEndpoint(hs),
decodeRegisterIncidentRequest,
encodeResponse,
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
})
Expand Down
34 changes: 11 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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() {
Expand Down Expand Up @@ -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...)
}
5 changes: 2 additions & 3 deletions tracking/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -24,7 +24,6 @@ func MakeHandler(ctx context.Context, ts Service, logger kitlog.Logger) http.Han
}

trackCargoHandler := kithttp.NewServer(
ctx,
makeTrackCargoEndpoint(ts),
decodeTrackCargoRequest,
encodeResponse,
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
})
Expand Down
10 changes: 2 additions & 8 deletions tracking/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"testing"
"time"

"context"

"github.com/go-kit/kit/log"

"github.com/marcusolsson/goddd/cargo"
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4e3e03a

Please sign in to comment.