Moved to google.golang.org/genproto/googleapis/api/annotations
Fixes #52
This commit is contained in:
139
vendor/github.com/go-kit/kit/examples/shipping/booking/endpoint.go
generated
vendored
Normal file
139
vendor/github.com/go-kit/kit/examples/shipping/booking/endpoint.go
generated
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
package booking
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
|
||||
"github.com/go-kit/kit/examples/shipping/cargo"
|
||||
"github.com/go-kit/kit/examples/shipping/location"
|
||||
)
|
||||
|
||||
type bookCargoRequest struct {
|
||||
Origin location.UNLocode
|
||||
Destination location.UNLocode
|
||||
ArrivalDeadline time.Time
|
||||
}
|
||||
|
||||
type bookCargoResponse struct {
|
||||
ID cargo.TrackingID `json:"tracking_id,omitempty"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r bookCargoResponse) error() error { return r.Err }
|
||||
|
||||
func makeBookCargoEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(bookCargoRequest)
|
||||
id, err := s.BookNewCargo(req.Origin, req.Destination, req.ArrivalDeadline)
|
||||
return bookCargoResponse{ID: id, Err: err}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type loadCargoRequest struct {
|
||||
ID cargo.TrackingID
|
||||
}
|
||||
|
||||
type loadCargoResponse struct {
|
||||
Cargo *Cargo `json:"cargo,omitempty"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r loadCargoResponse) error() error { return r.Err }
|
||||
|
||||
func makeLoadCargoEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(loadCargoRequest)
|
||||
c, err := s.LoadCargo(req.ID)
|
||||
return loadCargoResponse{Cargo: &c, Err: err}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type requestRoutesRequest struct {
|
||||
ID cargo.TrackingID
|
||||
}
|
||||
|
||||
type requestRoutesResponse struct {
|
||||
Routes []cargo.Itinerary `json:"routes,omitempty"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r requestRoutesResponse) error() error { return r.Err }
|
||||
|
||||
func makeRequestRoutesEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(requestRoutesRequest)
|
||||
itin := s.RequestPossibleRoutesForCargo(req.ID)
|
||||
return requestRoutesResponse{Routes: itin, Err: nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type assignToRouteRequest struct {
|
||||
ID cargo.TrackingID
|
||||
Itinerary cargo.Itinerary
|
||||
}
|
||||
|
||||
type assignToRouteResponse struct {
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r assignToRouteResponse) error() error { return r.Err }
|
||||
|
||||
func makeAssignToRouteEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(assignToRouteRequest)
|
||||
err := s.AssignCargoToRoute(req.ID, req.Itinerary)
|
||||
return assignToRouteResponse{Err: err}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type changeDestinationRequest struct {
|
||||
ID cargo.TrackingID
|
||||
Destination location.UNLocode
|
||||
}
|
||||
|
||||
type changeDestinationResponse struct {
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r changeDestinationResponse) error() error { return r.Err }
|
||||
|
||||
func makeChangeDestinationEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(changeDestinationRequest)
|
||||
err := s.ChangeDestination(req.ID, req.Destination)
|
||||
return changeDestinationResponse{Err: err}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type listCargosRequest struct{}
|
||||
|
||||
type listCargosResponse struct {
|
||||
Cargos []Cargo `json:"cargos,omitempty"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r listCargosResponse) error() error { return r.Err }
|
||||
|
||||
func makeListCargosEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
_ = request.(listCargosRequest)
|
||||
return listCargosResponse{Cargos: s.Cargos(), Err: nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type listLocationsRequest struct {
|
||||
}
|
||||
|
||||
type listLocationsResponse struct {
|
||||
Locations []Location `json:"locations,omitempty"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func makeListLocationsEndpoint(s Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
_ = request.(listLocationsRequest)
|
||||
return listLocationsResponse{Locations: s.Locations(), Err: nil}, nil
|
||||
}
|
||||
}
|
88
vendor/github.com/go-kit/kit/examples/shipping/booking/instrumenting.go
generated
vendored
Normal file
88
vendor/github.com/go-kit/kit/examples/shipping/booking/instrumenting.go
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
package booking
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/metrics"
|
||||
|
||||
"github.com/go-kit/kit/examples/shipping/cargo"
|
||||
"github.com/go-kit/kit/examples/shipping/location"
|
||||
)
|
||||
|
||||
type instrumentingService struct {
|
||||
requestCount metrics.Counter
|
||||
requestLatency metrics.Histogram
|
||||
Service
|
||||
}
|
||||
|
||||
// NewInstrumentingService returns an instance of an instrumenting Service.
|
||||
func NewInstrumentingService(counter metrics.Counter, latency metrics.Histogram, s Service) Service {
|
||||
return &instrumentingService{
|
||||
requestCount: counter,
|
||||
requestLatency: latency,
|
||||
Service: s,
|
||||
}
|
||||
}
|
||||
|
||||
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, deadline)
|
||||
}
|
||||
|
||||
func (s *instrumentingService) LoadCargo(id cargo.TrackingID) (c Cargo, err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "load").Add(1)
|
||||
s.requestLatency.With("method", "load").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.LoadCargo(id)
|
||||
}
|
||||
|
||||
func (s *instrumentingService) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "request_routes").Add(1)
|
||||
s.requestLatency.With("method", "request_routes").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.RequestPossibleRoutesForCargo(id)
|
||||
}
|
||||
|
||||
func (s *instrumentingService) AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "assign_to_route").Add(1)
|
||||
s.requestLatency.With("method", "assign_to_route").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.AssignCargoToRoute(id, itinerary)
|
||||
}
|
||||
|
||||
func (s *instrumentingService) ChangeDestination(id cargo.TrackingID, l location.UNLocode) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "change_destination").Add(1)
|
||||
s.requestLatency.With("method", "change_destination").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.ChangeDestination(id, l)
|
||||
}
|
||||
|
||||
func (s *instrumentingService) Cargos() []Cargo {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "list_cargos").Add(1)
|
||||
s.requestLatency.With("method", "list_cargos").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.Cargos()
|
||||
}
|
||||
|
||||
func (s *instrumentingService) Locations() []Location {
|
||||
defer func(begin time.Time) {
|
||||
s.requestCount.With("method", "list_locations").Add(1)
|
||||
s.requestLatency.With("method", "list_locations").Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
|
||||
return s.Service.Locations()
|
||||
}
|
102
vendor/github.com/go-kit/kit/examples/shipping/booking/logging.go
generated
vendored
Normal file
102
vendor/github.com/go-kit/kit/examples/shipping/booking/logging.go
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
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"
|
||||
)
|
||||
|
||||
type loggingService struct {
|
||||
logger log.Logger
|
||||
Service
|
||||
}
|
||||
|
||||
// NewLoggingService returns a new instance of a logging Service.
|
||||
func NewLoggingService(logger log.Logger, s Service) Service {
|
||||
return &loggingService{logger, s}
|
||||
}
|
||||
|
||||
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", deadline,
|
||||
"took", time.Since(begin),
|
||||
"err", err,
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.BookNewCargo(origin, destination, deadline)
|
||||
}
|
||||
|
||||
func (s *loggingService) LoadCargo(id cargo.TrackingID) (c Cargo, err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "load",
|
||||
"tracking_id", id,
|
||||
"took", time.Since(begin),
|
||||
"err", err,
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.LoadCargo(id)
|
||||
}
|
||||
|
||||
func (s *loggingService) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "request_routes",
|
||||
"tracking_id", id,
|
||||
"took", time.Since(begin),
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.RequestPossibleRoutesForCargo(id)
|
||||
}
|
||||
|
||||
func (s *loggingService) AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "assign_to_route",
|
||||
"tracking_id", id,
|
||||
"took", time.Since(begin),
|
||||
"err", err,
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.AssignCargoToRoute(id, itinerary)
|
||||
}
|
||||
|
||||
func (s *loggingService) ChangeDestination(id cargo.TrackingID, l location.UNLocode) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "change_destination",
|
||||
"tracking_id", id,
|
||||
"destination", l,
|
||||
"took", time.Since(begin),
|
||||
"err", err,
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.ChangeDestination(id, l)
|
||||
}
|
||||
|
||||
func (s *loggingService) Cargos() []Cargo {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "list_cargos",
|
||||
"took", time.Since(begin),
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.Cargos()
|
||||
}
|
||||
|
||||
func (s *loggingService) Locations() []Location {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Log(
|
||||
"method", "list_locations",
|
||||
"took", time.Since(begin),
|
||||
)
|
||||
}(time.Now())
|
||||
return s.Service.Locations()
|
||||
}
|
197
vendor/github.com/go-kit/kit/examples/shipping/booking/service.go
generated
vendored
Normal file
197
vendor/github.com/go-kit/kit/examples/shipping/booking/service.go
generated
vendored
Normal file
@@ -0,0 +1,197 @@
|
||||
// Package booking provides the use-case of booking a cargo. Used by views
|
||||
// facing an administrator.
|
||||
package booking
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/examples/shipping/cargo"
|
||||
"github.com/go-kit/kit/examples/shipping/location"
|
||||
"github.com/go-kit/kit/examples/shipping/routing"
|
||||
)
|
||||
|
||||
// ErrInvalidArgument is returned when one or more arguments are invalid.
|
||||
var ErrInvalidArgument = errors.New("invalid argument")
|
||||
|
||||
// Service is the interface that provides booking methods.
|
||||
type Service interface {
|
||||
// BookNewCargo registers a new cargo in the tracking system, not yet
|
||||
// routed.
|
||||
BookNewCargo(origin location.UNLocode, destination location.UNLocode, deadline time.Time) (cargo.TrackingID, error)
|
||||
|
||||
// LoadCargo returns a read model of a cargo.
|
||||
LoadCargo(id cargo.TrackingID) (Cargo, error)
|
||||
|
||||
// RequestPossibleRoutesForCargo requests a list of itineraries describing
|
||||
// possible routes for this cargo.
|
||||
RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary
|
||||
|
||||
// AssignCargoToRoute assigns a cargo to the route specified by the
|
||||
// itinerary.
|
||||
AssignCargoToRoute(id cargo.TrackingID, itinerary cargo.Itinerary) error
|
||||
|
||||
// ChangeDestination changes the destination of a cargo.
|
||||
ChangeDestination(id cargo.TrackingID, destination location.UNLocode) error
|
||||
|
||||
// Cargos returns a list of all cargos that have been booked.
|
||||
Cargos() []Cargo
|
||||
|
||||
// Locations returns a list of registered locations.
|
||||
Locations() []Location
|
||||
}
|
||||
|
||||
type service struct {
|
||||
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.cargos.Find(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.AssignToRoute(itinerary)
|
||||
|
||||
return s.cargos.Store(c)
|
||||
}
|
||||
|
||||
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: deadline,
|
||||
}
|
||||
|
||||
c := cargo.New(id, rs)
|
||||
|
||||
if err := s.cargos.Store(c); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return c.TrackingID, nil
|
||||
}
|
||||
|
||||
func (s *service) LoadCargo(id cargo.TrackingID) (Cargo, error) {
|
||||
if id == "" {
|
||||
return Cargo{}, ErrInvalidArgument
|
||||
}
|
||||
|
||||
c, err := s.cargos.Find(id)
|
||||
if err != nil {
|
||||
return Cargo{}, err
|
||||
}
|
||||
|
||||
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.cargos.Find(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l, err := s.locations.Find(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.SpecifyNewRoute(cargo.RouteSpecification{
|
||||
Origin: c.Origin,
|
||||
Destination: l.UNLocode,
|
||||
ArrivalDeadline: c.RouteSpecification.ArrivalDeadline,
|
||||
})
|
||||
|
||||
if err := s.cargos.Store(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) RequestPossibleRoutesForCargo(id cargo.TrackingID) []cargo.Itinerary {
|
||||
if id == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
c, err := s.cargos.Find(id)
|
||||
if err != nil {
|
||||
return []cargo.Itinerary{}
|
||||
}
|
||||
|
||||
return s.routingService.FetchRoutesForSpecification(c.RouteSpecification)
|
||||
}
|
||||
|
||||
func (s *service) Cargos() []Cargo {
|
||||
var result []Cargo
|
||||
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.locations.FindAll() {
|
||||
result = append(result, Location{
|
||||
UNLocode: string(v.UNLocode),
|
||||
Name: v.Name,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// NewService creates a booking service with necessary dependencies.
|
||||
func NewService(cargos cargo.Repository, locations location.Repository, events cargo.HandlingEventRepository, rs routing.Service) Service {
|
||||
return &service{
|
||||
cargos: cargos,
|
||||
locations: locations,
|
||||
handlingEvents: events,
|
||||
routingService: rs,
|
||||
}
|
||||
}
|
||||
|
||||
// Location is a read model for booking views.
|
||||
type Location struct {
|
||||
UNLocode string `json:"locode"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Cargo is a read model for booking views.
|
||||
type Cargo struct {
|
||||
ArrivalDeadline time.Time `json:"arrival_deadline"`
|
||||
Destination string `json:"destination"`
|
||||
Legs []cargo.Leg `json:"legs,omitempty"`
|
||||
Misrouted bool `json:"misrouted"`
|
||||
Origin string `json:"origin"`
|
||||
Routed bool `json:"routed"`
|
||||
TrackingID string `json:"tracking_id"`
|
||||
}
|
||||
|
||||
func assemble(c *cargo.Cargo, events cargo.HandlingEventRepository) Cargo {
|
||||
return Cargo{
|
||||
TrackingID: string(c.TrackingID),
|
||||
Origin: string(c.Origin),
|
||||
Destination: string(c.RouteSpecification.Destination),
|
||||
Misrouted: c.Delivery.RoutingStatus == cargo.Misrouted,
|
||||
Routed: !c.Itinerary.IsEmpty(),
|
||||
ArrivalDeadline: c.RouteSpecification.ArrivalDeadline,
|
||||
Legs: c.Itinerary.Legs,
|
||||
}
|
||||
}
|
194
vendor/github.com/go-kit/kit/examples/shipping/booking/transport.go
generated
vendored
Normal file
194
vendor/github.com/go-kit/kit/examples/shipping/booking/transport.go
generated
vendored
Normal file
@@ -0,0 +1,194 @@
|
||||
package booking
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
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.
|
||||
func MakeHandler(bs Service, logger kitlog.Logger) http.Handler {
|
||||
opts := []kithttp.ServerOption{
|
||||
kithttp.ServerErrorLogger(logger),
|
||||
kithttp.ServerErrorEncoder(encodeError),
|
||||
}
|
||||
|
||||
bookCargoHandler := kithttp.NewServer(
|
||||
makeBookCargoEndpoint(bs),
|
||||
decodeBookCargoRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
loadCargoHandler := kithttp.NewServer(
|
||||
makeLoadCargoEndpoint(bs),
|
||||
decodeLoadCargoRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
requestRoutesHandler := kithttp.NewServer(
|
||||
makeRequestRoutesEndpoint(bs),
|
||||
decodeRequestRoutesRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
assignToRouteHandler := kithttp.NewServer(
|
||||
makeAssignToRouteEndpoint(bs),
|
||||
decodeAssignToRouteRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
changeDestinationHandler := kithttp.NewServer(
|
||||
makeChangeDestinationEndpoint(bs),
|
||||
decodeChangeDestinationRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
listCargosHandler := kithttp.NewServer(
|
||||
makeListCargosEndpoint(bs),
|
||||
decodeListCargosRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
listLocationsHandler := kithttp.NewServer(
|
||||
makeListLocationsEndpoint(bs),
|
||||
decodeListLocationsRequest,
|
||||
encodeResponse,
|
||||
opts...,
|
||||
)
|
||||
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.Handle("/booking/v1/cargos", bookCargoHandler).Methods("POST")
|
||||
r.Handle("/booking/v1/cargos", listCargosHandler).Methods("GET")
|
||||
r.Handle("/booking/v1/cargos/{id}", loadCargoHandler).Methods("GET")
|
||||
r.Handle("/booking/v1/cargos/{id}/request_routes", requestRoutesHandler).Methods("GET")
|
||||
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")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
var errBadRoute = errors.New("bad route")
|
||||
|
||||
func decodeBookCargoRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
var body struct {
|
||||
Origin string `json:"origin"`
|
||||
Destination string `json:"destination"`
|
||||
ArrivalDeadline time.Time `json:"arrival_deadline"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bookCargoRequest{
|
||||
Origin: location.UNLocode(body.Origin),
|
||||
Destination: location.UNLocode(body.Destination),
|
||||
ArrivalDeadline: body.ArrivalDeadline,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func decodeLoadCargoRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
vars := mux.Vars(r)
|
||||
id, ok := vars["id"]
|
||||
if !ok {
|
||||
return nil, errBadRoute
|
||||
}
|
||||
return loadCargoRequest{ID: cargo.TrackingID(id)}, nil
|
||||
}
|
||||
|
||||
func decodeRequestRoutesRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
vars := mux.Vars(r)
|
||||
id, ok := vars["id"]
|
||||
if !ok {
|
||||
return nil, errBadRoute
|
||||
}
|
||||
return requestRoutesRequest{ID: cargo.TrackingID(id)}, nil
|
||||
}
|
||||
|
||||
func decodeAssignToRouteRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
vars := mux.Vars(r)
|
||||
id, ok := vars["id"]
|
||||
if !ok {
|
||||
return nil, errBadRoute
|
||||
}
|
||||
|
||||
var itinerary cargo.Itinerary
|
||||
if err := json.NewDecoder(r.Body).Decode(&itinerary); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return assignToRouteRequest{
|
||||
ID: cargo.TrackingID(id),
|
||||
Itinerary: itinerary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func decodeChangeDestinationRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
vars := mux.Vars(r)
|
||||
id, ok := vars["id"]
|
||||
if !ok {
|
||||
return nil, errBadRoute
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Destination string `json:"destination"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return changeDestinationRequest{
|
||||
ID: cargo.TrackingID(id),
|
||||
Destination: location.UNLocode(body.Destination),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func decodeListCargosRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
return listCargosRequest{}, nil
|
||||
}
|
||||
|
||||
func decodeListLocationsRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
||||
return listLocationsRequest{}, nil
|
||||
}
|
||||
|
||||
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
if e, ok := response.(errorer); ok && e.error() != nil {
|
||||
encodeError(ctx, e.error(), w)
|
||||
return nil
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
type errorer interface {
|
||||
error() error
|
||||
}
|
||||
|
||||
// encode errors from business-logic
|
||||
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
switch err {
|
||||
case cargo.ErrUnknown:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case ErrInvalidArgument:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
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(),
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user