forked from microservices-demo/catalogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
187 lines (171 loc) · 5.98 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
package catalogue
// transport.go contains the binding from endpoints to a concrete transport.
// In our case we just use a REST-y HTTP transport.
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-kit/kit/circuitbreaker"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/tracing/opentracing"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sony/gobreaker"
"golang.org/x/net/context"
)
// MakeHTTPHandler mounts the endpoints into a REST-y HTTP handler.
func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger log.Logger, tracer stdopentracing.Tracer) *mux.Router {
r := mux.NewRouter().StrictSlash(false)
options := []httptransport.ServerOption{
httptransport.ServerErrorLogger(logger),
httptransport.ServerErrorEncoder(encodeError),
}
// GET /catalogue List
// GET /catalogue/size Count
// GET /catalogue/{id} Get
// GET /tags Tags
// GET /health Health Check
r.Methods("GET").Path("/catalogue").Handler(httptransport.NewServer(
ctx,
circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "List",
Timeout: 30 * time.Second,
}))(e.ListEndpoint),
decodeListRequest,
encodeListResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue", logger)))...,
))
r.Methods("GET").Path("/catalogue/size").Handler(httptransport.NewServer(
ctx,
circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "Count",
Timeout: 30 * time.Second,
}))(e.CountEndpoint),
decodeCountRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue/size", logger)))...,
))
r.Methods("GET").Path("/catalogue/{id}").Handler(httptransport.NewServer(
ctx,
circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "Get",
Timeout: 30 * time.Second,
}))(e.GetEndpoint),
decodeGetRequest,
encodeGetResponse, // special case, this one can have an error
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue/{id}", logger)))...,
))
r.Methods("GET").Path("/tags").Handler(httptransport.NewServer(
ctx,
circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "Tags",
Timeout: 30 * time.Second,
}))(e.TagsEndpoint),
decodeTagsRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /tags", logger)))...,
))
r.Methods("GET").PathPrefix("/catalogue/images/").Handler(http.StripPrefix(
"/catalogue/images/",
http.FileServer(http.Dir(imagePath)),
))
r.Methods("GET").PathPrefix("/health").Handler(httptransport.NewServer(
ctx,
circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "Health",
Timeout: 30 * time.Second,
}))(e.HealthEndpoint),
decodeHealthRequest,
encodeHealthResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /health", logger)))...,
))
r.Handle("/metrics", promhttp.Handler())
return r
}
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
code := http.StatusInternalServerError
switch err {
case ErrNotFound:
code = http.StatusNotFound
}
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
"status_code": code,
"status_text": http.StatusText(code),
})
}
func decodeListRequest(_ context.Context, r *http.Request) (interface{}, error) {
pageNum := 1
if page := r.FormValue("page"); page != "" {
pageNum, _ = strconv.Atoi(page)
}
pageSize := 10
if size := r.FormValue("size"); size != "" {
pageSize, _ = strconv.Atoi(size)
}
order := "id"
if sort := r.FormValue("sort"); sort != "" {
order = strings.ToLower(sort)
}
tags := []string{}
if tagsval := r.FormValue("tags"); tagsval != "" {
tags = strings.Split(tagsval, ",")
}
return listRequest{
Tags: tags,
Order: order,
PageNum: pageNum,
PageSize: pageSize,
}, nil
}
// encodeListResponse is distinct from the generic encodeResponse because our
// clients expect that we will encode the slice (array) of socks directly,
// without the wrapping response object.
func encodeListResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
resp := response.(listResponse)
return encodeResponse(ctx, w, resp.Socks)
}
func decodeCountRequest(_ context.Context, r *http.Request) (interface{}, error) {
tags := []string{}
if tagsval := r.FormValue("tags"); tagsval != "" {
tags = strings.Split(tagsval, ",")
}
return countRequest{
Tags: tags,
}, nil
}
func decodeGetRequest(_ context.Context, r *http.Request) (interface{}, error) {
return getRequest{
ID: mux.Vars(r)["id"],
}, nil
}
// encodeGetResponse is distinct from the generic encodeResponse because we need
// to special-case when the getResponse object contains a non-nil error.
func encodeGetResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
resp := response.(getResponse)
if resp.Err != nil {
encodeError(ctx, resp.Err, w)
return nil
}
return encodeResponse(ctx, w, resp.Sock)
}
func decodeTagsRequest(_ context.Context, r *http.Request) (interface{}, error) {
return struct{}{}, nil
}
func decodeHealthRequest(_ context.Context, r *http.Request) (interface{}, error) {
return struct{}{}, nil
}
func encodeHealthResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return encodeResponse(ctx, w, response.(healthResponse))
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
// All of our response objects are JSON serializable, so we just do that.
w.Header().Set("Content-Type", "application/json; charset=utf-8")
return json.NewEncoder(w).Encode(response)
}