forked from go-kit/kit
/
transport.go
195 lines (168 loc) · 4.99 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package booking
import (
"context"
"encoding/json"
"errors"
"net/http"
"time"
"github.com/gorilla/mux"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/transport"
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.ServerErrorHandler(transport.NewLogErrorHandler(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) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch err {
case cargo.ErrUnknown:
w.WriteHeader(http.StatusNotFound)
case ErrInvalidArgument:
w.WriteHeader(http.StatusBadRequest)
default:
w.WriteHeader(http.StatusInternalServerError)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
}