Moved to google.golang.org/genproto/googleapis/api/annotations

Fixes #52
This commit is contained in:
Valerio Gheri
2017-03-31 18:01:58 +02:00
parent 024c5a4e4e
commit c40779224f
2037 changed files with 831329 additions and 1854 deletions

View File

@@ -0,0 +1,34 @@
package handling
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"
"github.com/go-kit/kit/examples/shipping/voyage"
)
type registerIncidentRequest struct {
ID cargo.TrackingID
Location location.UNLocode
Voyage voyage.Number
EventType cargo.HandlingEventType
CompletionTime time.Time
}
type registerIncidentResponse struct {
Err error `json:"error,omitempty"`
}
func (r registerIncidentResponse) error() error { return r.Err }
func makeRegisterIncidentEndpoint(hs Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(registerIncidentRequest)
err := hs.RegisterHandlingEvent(req.CompletionTime, req.ID, req.Voyage, req.Location, req.EventType)
return registerIncidentResponse{Err: err}, nil
}
}

View File

@@ -0,0 +1,37 @@
package handling
import (
"time"
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/examples/shipping/voyage"
)
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) 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(completed, id, voyageNumber, loc, eventType)
}

View File

@@ -0,0 +1,38 @@
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"
)
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) 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", id,
"location", unLocode,
"voyage", voyageNumber,
"event_type", eventType,
"completion_time", completed,
"took", time.Since(begin),
"err", err,
)
}(time.Now())
return s.Service.RegisterHandlingEvent(completed, id, voyageNumber, unLocode, eventType)
}

View File

@@ -0,0 +1,76 @@
// Package handling provides the use-case for registering incidents. Used by
// views facing the people handling the cargo along its route.
package handling
import (
"errors"
"time"
"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/inspection"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/examples/shipping/voyage"
)
// ErrInvalidArgument is returned when one or more arguments are invalid.
var ErrInvalidArgument = errors.New("invalid argument")
// EventHandler provides a means of subscribing to registered handling events.
type EventHandler interface {
CargoWasHandled(cargo.HandlingEvent)
}
// Service provides handling operations.
type Service interface {
// RegisterHandlingEvent registers a handling event in the system, and
// notifies interested parties that a cargo has been handled.
RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
unLocode location.UNLocode, eventType cargo.HandlingEventType) error
}
type service struct {
handlingEventRepository cargo.HandlingEventRepository
handlingEventFactory cargo.HandlingEventFactory
handlingEventHandler EventHandler
}
func (s *service) RegisterHandlingEvent(completed time.Time, id cargo.TrackingID, voyageNumber voyage.Number,
loc location.UNLocode, eventType cargo.HandlingEventType) error {
if completed.IsZero() || id == "" || loc == "" || eventType == cargo.NotHandled {
return ErrInvalidArgument
}
e, err := s.handlingEventFactory.CreateHandlingEvent(time.Now(), completed, id, voyageNumber, loc, eventType)
if err != nil {
return err
}
s.handlingEventRepository.Store(e)
s.handlingEventHandler.CargoWasHandled(e)
return nil
}
// NewService creates a handling event service with necessary dependencies.
func NewService(r cargo.HandlingEventRepository, f cargo.HandlingEventFactory, h EventHandler) Service {
return &service{
handlingEventRepository: r,
handlingEventFactory: f,
handlingEventHandler: h,
}
}
type handlingEventHandler struct {
InspectionService inspection.Service
}
func (h *handlingEventHandler) CargoWasHandled(event cargo.HandlingEvent) {
h.InspectionService.InspectCargo(event.TrackingID)
}
// NewEventHandler returns a new instance of a EventHandler.
func NewEventHandler(s inspection.Service) EventHandler {
return &handlingEventHandler{
InspectionService: s,
}
}

View File

@@ -0,0 +1,100 @@
package handling
import (
"context"
"encoding/json"
"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"
"github.com/go-kit/kit/examples/shipping/voyage"
)
// MakeHandler returns a handler for the handling service.
func MakeHandler(hs Service, logger kitlog.Logger) http.Handler {
r := mux.NewRouter()
opts := []kithttp.ServerOption{
kithttp.ServerErrorLogger(logger),
kithttp.ServerErrorEncoder(encodeError),
}
registerIncidentHandler := kithttp.NewServer(
makeRegisterIncidentEndpoint(hs),
decodeRegisterIncidentRequest,
encodeResponse,
opts...,
)
r.Handle("/handling/v1/incidents", registerIncidentHandler).Methods("POST")
return r
}
func decodeRegisterIncidentRequest(_ context.Context, r *http.Request) (interface{}, error) {
var body struct {
CompletionTime time.Time `json:"completion_time"`
TrackingID string `json:"tracking_id"`
VoyageNumber string `json:"voyage"`
Location string `json:"location"`
EventType string `json:"event_type"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
return nil, err
}
return registerIncidentRequest{
CompletionTime: body.CompletionTime,
ID: cargo.TrackingID(body.TrackingID),
Voyage: voyage.Number(body.VoyageNumber),
Location: location.UNLocode(body.Location),
EventType: stringToEventType(body.EventType),
}, nil
}
func stringToEventType(s string) cargo.HandlingEventType {
types := map[string]cargo.HandlingEventType{
cargo.Receive.String(): cargo.Receive,
cargo.Load.String(): cargo.Load,
cargo.Unload.String(): cargo.Unload,
cargo.Customs.String(): cargo.Customs,
cargo.Claim.String(): cargo.Claim,
}
return types[s]
}
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(),
})
}