OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / handling / transport.go
1 package handling
2
3 import (
4         "context"
5         "encoding/json"
6         "net/http"
7         "time"
8
9         "github.com/gorilla/mux"
10
11         kitlog "github.com/go-kit/kit/log"
12         kithttp "github.com/go-kit/kit/transport/http"
13
14         "github.com/go-kit/kit/examples/shipping/cargo"
15         "github.com/go-kit/kit/examples/shipping/location"
16         "github.com/go-kit/kit/examples/shipping/voyage"
17 )
18
19 // MakeHandler returns a handler for the handling service.
20 func MakeHandler(hs Service, logger kitlog.Logger) http.Handler {
21         r := mux.NewRouter()
22
23         opts := []kithttp.ServerOption{
24                 kithttp.ServerErrorLogger(logger),
25                 kithttp.ServerErrorEncoder(encodeError),
26         }
27
28         registerIncidentHandler := kithttp.NewServer(
29                 makeRegisterIncidentEndpoint(hs),
30                 decodeRegisterIncidentRequest,
31                 encodeResponse,
32                 opts...,
33         )
34
35         r.Handle("/handling/v1/incidents", registerIncidentHandler).Methods("POST")
36
37         return r
38 }
39
40 func decodeRegisterIncidentRequest(_ context.Context, r *http.Request) (interface{}, error) {
41         var body struct {
42                 CompletionTime time.Time `json:"completion_time"`
43                 TrackingID     string    `json:"tracking_id"`
44                 VoyageNumber   string    `json:"voyage"`
45                 Location       string    `json:"location"`
46                 EventType      string    `json:"event_type"`
47         }
48
49         if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
50                 return nil, err
51         }
52
53         return registerIncidentRequest{
54                 CompletionTime: body.CompletionTime,
55                 ID:             cargo.TrackingID(body.TrackingID),
56                 Voyage:         voyage.Number(body.VoyageNumber),
57                 Location:       location.UNLocode(body.Location),
58                 EventType:      stringToEventType(body.EventType),
59         }, nil
60 }
61
62 func stringToEventType(s string) cargo.HandlingEventType {
63         types := map[string]cargo.HandlingEventType{
64                 cargo.Receive.String(): cargo.Receive,
65                 cargo.Load.String():    cargo.Load,
66                 cargo.Unload.String():  cargo.Unload,
67                 cargo.Customs.String(): cargo.Customs,
68                 cargo.Claim.String():   cargo.Claim,
69         }
70         return types[s]
71 }
72
73 func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
74         if e, ok := response.(errorer); ok && e.error() != nil {
75                 encodeError(ctx, e.error(), w)
76                 return nil
77         }
78         w.Header().Set("Content-Type", "application/json; charset=utf-8")
79         return json.NewEncoder(w).Encode(response)
80 }
81
82 type errorer interface {
83         error() error
84 }
85
86 // encode errors from business-logic
87 func encodeError(_ context.Context, err error, w http.ResponseWriter) {
88         w.Header().Set("Content-Type", "application/json; charset=utf-8")
89         switch err {
90         case cargo.ErrUnknown:
91                 w.WriteHeader(http.StatusNotFound)
92         case ErrInvalidArgument:
93                 w.WriteHeader(http.StatusBadRequest)
94         default:
95                 w.WriteHeader(http.StatusInternalServerError)
96         }
97         json.NewEncoder(w).Encode(map[string]interface{}{
98                 "error": err.Error(),
99         })
100 }