Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/shipping/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The application consists of three application services, `booking`, `handling` an

There are also a few pure domain packages that contain some intricate business-logic. They provide domain objects and services that are used by each application service to provide interesting use-cases for the user.

`repository` contains in-memory implementations for the repositories found in the domain packages.
`inmem` contains in-memory implementations for the repositories found in the domain packages.

The `routing` package provides a _domain service_ that is used to query an external application for possible routes.

Expand Down
1 change: 1 addition & 0 deletions examples/shipping/booking/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"golang.org/x/net/context"

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

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
)
Expand Down
10 changes: 5 additions & 5 deletions examples/shipping/booking/instrumenting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ type instrumentingService struct {
}

// NewInstrumentingService returns an instance of an instrumenting Service.
func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.Histogram, s Service) Service {
func NewInstrumentingService(counter metrics.Counter, latency metrics.Histogram, s Service) Service {
return &instrumentingService{
requestCount: requestCount,
requestLatency: requestLatency,
requestCount: counter,
requestLatency: latency,
Service: s,
}
}

func (s *instrumentingService) BookNewCargo(origin, destination location.UNLocode, arrivalDeadline time.Time) (cargo.TrackingID, error) {
func (s *instrumentingService) BookNewCargo(origin, destination location.UNLocode, deadline time.Time) (cargo.TrackingID, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "book").Add(1)
s.requestLatency.With("method", "book").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.BookNewCargo(origin, destination, arrivalDeadline)
return s.Service.BookNewCargo(origin, destination, deadline)
}

func (s *instrumentingService) LoadCargo(id cargo.TrackingID) (c Cargo, err error) {
Expand Down
9 changes: 5 additions & 4 deletions examples/shipping/booking/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package booking
import (
"time"

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

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/log"
)

type loggingService struct {
Expand All @@ -18,18 +19,18 @@ func NewLoggingService(logger log.Logger, s Service) Service {
return &loggingService{logger, s}
}

func (s *loggingService) BookNewCargo(origin location.UNLocode, destination location.UNLocode, arrivalDeadline time.Time) (id cargo.TrackingID, err error) {
func (s *loggingService) BookNewCargo(origin location.UNLocode, destination location.UNLocode, deadline time.Time) (id cargo.TrackingID, err error) {
defer func(begin time.Time) {
s.logger.Log(
"method", "book",
"origin", origin,
"destination", destination,
"arrival_deadline", arrivalDeadline,
"arrival_deadline", deadline,
"took", time.Since(begin),
"err", err,
)
}(time.Now())
return s.Service.BookNewCargo(origin, destination, arrivalDeadline)
return s.Service.BookNewCargo(origin, destination, deadline)
}

func (s *loggingService) LoadCargo(id cargo.TrackingID) (c Cargo, err error) {
Expand Down
68 changes: 32 additions & 36 deletions examples/shipping/booking/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ var ErrInvalidArgument = errors.New("invalid argument")
type Service interface {
// BookNewCargo registers a new cargo in the tracking system, not yet
// routed.
BookNewCargo(origin location.UNLocode, destination location.UNLocode, arrivalDeadline time.Time) (cargo.TrackingID, error)
BookNewCargo(origin location.UNLocode, destination location.UNLocode, deadline time.Time) (cargo.TrackingID, error)

// LoadCargo returns a read model of a cargo.
LoadCargo(trackingID cargo.TrackingID) (Cargo, error)
LoadCargo(id cargo.TrackingID) (Cargo, error)

// RequestPossibleRoutesForCargo requests a list of itineraries describing
// possible routes for this cargo.
RequestPossibleRoutesForCargo(trackingID cargo.TrackingID) []cargo.Itinerary
RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary

// AssignCargoToRoute assigns a cargo to the route specified by the
// itinerary.
AssignCargoToRoute(trackingID cargo.TrackingID, itinerary cargo.Itinerary) error
AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) error

// ChangeDestination changes the destination of a cargo.
ChangeDestination(trackingID cargo.TrackingID, unLocode location.UNLocode) error
ChangeDestination(id cargo.TrackingID, destination location.UNLocode) error

// Cargos returns a list of all cargos that have been booked.
Cargos() []Cargo
Expand All @@ -42,76 +42,72 @@ type Service interface {
}

type service struct {
cargoRepository cargo.Repository
locationRepository location.Repository
routingService routing.Service
handlingEventRepository cargo.HandlingEventRepository
cargos cargo.Repository
locations location.Repository
handlingEvents cargo.HandlingEventRepository
routingService routing.Service
}

func (s *service) AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) error {
if id == "" || len(itinerary.Legs) == 0 {
return ErrInvalidArgument
}

c, err := s.cargoRepository.Find(id)
c, err := s.cargos.Find(id)
if err != nil {
return err
}

c.AssignToRoute(itinerary)

if err := s.cargoRepository.Store(c); err != nil {
return err
}

return nil
return s.cargos.Store(c)
}

func (s *service) BookNewCargo(origin, destination location.UNLocode, arrivalDeadline time.Time) (cargo.TrackingID, error) {
if origin == "" || destination == "" || arrivalDeadline.IsZero() {
func (s *service) BookNewCargo(origin, destination location.UNLocode, deadline time.Time) (cargo.TrackingID, error) {
if origin == "" || destination == "" || deadline.IsZero() {
return "", ErrInvalidArgument
}

id := cargo.NextTrackingID()
rs := cargo.RouteSpecification{
Origin: origin,
Destination: destination,
ArrivalDeadline: arrivalDeadline,
ArrivalDeadline: deadline,
}

c := cargo.New(id, rs)

if err := s.cargoRepository.Store(c); err != nil {
if err := s.cargos.Store(c); err != nil {
return "", err
}

return c.TrackingID, nil
}

func (s *service) LoadCargo(trackingID cargo.TrackingID) (Cargo, error) {
if trackingID == "" {
func (s *service) LoadCargo(id cargo.TrackingID) (Cargo, error) {
if id == "" {
return Cargo{}, ErrInvalidArgument
}

c, err := s.cargoRepository.Find(trackingID)
c, err := s.cargos.Find(id)
if err != nil {
return Cargo{}, err
}

return assemble(c, s.handlingEventRepository), nil
return assemble(c, s.handlingEvents), nil
}

func (s *service) ChangeDestination(id cargo.TrackingID, destination location.UNLocode) error {
if id == "" || destination == "" {
return ErrInvalidArgument
}

c, err := s.cargoRepository.Find(id)
c, err := s.cargos.Find(id)
if err != nil {
return err
}

l, err := s.locationRepository.Find(destination)
l, err := s.locations.Find(destination)
if err != nil {
return err
}
Expand All @@ -122,7 +118,7 @@ func (s *service) ChangeDestination(id cargo.TrackingID, destination location.UN
ArrivalDeadline: c.RouteSpecification.ArrivalDeadline,
})

if err := s.cargoRepository.Store(c); err != nil {
if err := s.cargos.Store(c); err != nil {
return err
}

Expand All @@ -134,7 +130,7 @@ func (s *service) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Iti
return nil
}

c, err := s.cargoRepository.Find(id)
c, err := s.cargos.Find(id)
if err != nil {
return []cargo.Itinerary{}
}
Expand All @@ -144,15 +140,15 @@ func (s *service) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Iti

func (s *service) Cargos() []Cargo {
var result []Cargo
for _, c := range s.cargoRepository.FindAll() {
result = append(result, assemble(c, s.handlingEventRepository))
for _, c := range s.cargos.FindAll() {
result = append(result, assemble(c, s.handlingEvents))
}
return result
}

func (s *service) Locations() []Location {
var result []Location
for _, v := range s.locationRepository.FindAll() {
for _, v := range s.locations.FindAll() {
result = append(result, Location{
UNLocode: string(v.UNLocode),
Name: v.Name,
Expand All @@ -162,12 +158,12 @@ func (s *service) Locations() []Location {
}

// NewService creates a booking service with necessary dependencies.
func NewService(cr cargo.Repository, lr location.Repository, her cargo.HandlingEventRepository, rs routing.Service) Service {
func NewService(cargos cargo.Repository, locations location.Repository, events cargo.HandlingEventRepository, rs routing.Service) Service {
return &service{
cargoRepository: cr,
locationRepository: lr,
handlingEventRepository: her,
routingService: rs,
cargos: cargos,
locations: locations,
handlingEvents: events,
routingService: rs,
}
}

Expand All @@ -188,7 +184,7 @@ type Cargo struct {
TrackingID string `json:"tracking_id"`
}

func assemble(c *cargo.Cargo, her cargo.HandlingEventRepository) Cargo {
func assemble(c *cargo.Cargo, events cargo.HandlingEventRepository) Cargo {
return Cargo{
TrackingID: string(c.TrackingID),
Origin: string(c.Origin),
Expand Down
6 changes: 3 additions & 3 deletions examples/shipping/booking/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/gorilla/mux"
"golang.org/x/net/context"

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
kitlog "github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
)

// MakeHandler returns a handler for the booking service.
Expand Down Expand Up @@ -81,7 +82,6 @@ func MakeHandler(ctx context.Context, bs Service, logger kitlog.Logger) http.Han
r.Handle("/booking/v1/cargos/{id}/assign_to_route", assignToRouteHandler).Methods("POST")
r.Handle("/booking/v1/cargos/{id}/change_destination", changeDestinationHandler).Methods("POST")
r.Handle("/booking/v1/locations", listLocationsHandler).Methods("GET")
r.Handle("/booking/v1/docs", http.StripPrefix("/booking/v1/docs", http.FileServer(http.Dir("booking/docs"))))

return r
}
Expand Down
2 changes: 1 addition & 1 deletion examples/shipping/cargo/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func New(id TrackingID, rs RouteSpecification) *Cargo {
// Repository provides access a cargo store.
type Repository interface {
Store(cargo *Cargo) error
Find(trackingID TrackingID) (*Cargo, error)
Find(id TrackingID) (*Cargo, error)
FindAll() []*Cargo
}

Expand Down
6 changes: 3 additions & 3 deletions examples/shipping/cargo/handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ type HandlingEventFactory struct {
}

// CreateHandlingEvent creates a validated handling event.
func (f *HandlingEventFactory) CreateHandlingEvent(registrationTime time.Time, completionTime time.Time, trackingID TrackingID,
func (f *HandlingEventFactory) CreateHandlingEvent(registered time.Time, completed time.Time, id TrackingID,
voyageNumber voyage.Number, unLocode location.UNLocode, eventType HandlingEventType) (HandlingEvent, error) {

if _, err := f.CargoRepository.Find(trackingID); err != nil {
if _, err := f.CargoRepository.Find(id); err != nil {
return HandlingEvent{}, err
}

Expand All @@ -111,7 +111,7 @@ func (f *HandlingEventFactory) CreateHandlingEvent(registrationTime time.Time, c
}

return HandlingEvent{
TrackingID: trackingID,
TrackingID: id,
Activity: HandlingActivity{
Type: eventType,
Location: unLocode,
Expand Down
1 change: 1 addition & 0 deletions examples/shipping/handling/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"golang.org/x/net/context"

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

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/examples/shipping/voyage"
Expand Down
10 changes: 5 additions & 5 deletions examples/shipping/handling/instrumenting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ type instrumentingService struct {
}

// NewInstrumentingService returns an instance of an instrumenting Service.
func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.Histogram, s Service) Service {
func NewInstrumentingService(counter metrics.Counter, latency metrics.Histogram, s Service) Service {
return &instrumentingService{
requestCount: requestCount,
requestLatency: requestLatency,
requestCount: counter,
requestLatency: latency,
Service: s,
}
}

func (s *instrumentingService) RegisterHandlingEvent(completionTime time.Time, trackingID cargo.TrackingID, voyage voyage.Number,
func (s *instrumentingService) RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
loc location.UNLocode, eventType cargo.HandlingEventType) error {

defer func(begin time.Time) {
s.requestCount.With("method", "register_incident").Add(1)
s.requestLatency.With("method", "register_incident").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.RegisterHandlingEvent(completionTime, trackingID, voyage, loc, eventType)
return s.Service.RegisterHandlingEvent(completed, id, voyageNumber, loc, eventType)
}
11 changes: 6 additions & 5 deletions examples/shipping/handling/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package handling
import (
"time"

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

"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/examples/shipping/voyage"
"github.com/go-kit/kit/log"
)

type loggingService struct {
Expand All @@ -19,19 +20,19 @@ func NewLoggingService(logger log.Logger, s Service) Service {
return &loggingService{logger, s}
}

func (s *loggingService) RegisterHandlingEvent(completionTime time.Time, trackingID cargo.TrackingID, voyageNumber voyage.Number,
func (s *loggingService) RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
unLocode location.UNLocode, eventType cargo.HandlingEventType) (err error) {
defer func(begin time.Time) {
s.logger.Log(
"method", "register_incident",
"tracking_id", trackingID,
"tracking_id", id,
"location", unLocode,
"voyage", voyageNumber,
"event_type", eventType,
"completion_time", completionTime,
"completion_time", completed,
"took", time.Since(begin),
"err", err,
)
}(time.Now())
return s.Service.RegisterHandlingEvent(completionTime, trackingID, voyageNumber, unLocode, eventType)
return s.Service.RegisterHandlingEvent(completed, id, voyageNumber, unLocode, eventType)
}
Loading