-
Notifications
You must be signed in to change notification settings - Fork 185
/
http.go
123 lines (105 loc) · 3.5 KB
/
http.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
package transport
import (
"context"
"encoding/json"
"errors"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/tracing/zipkin"
"github.com/go-kit/kit/transport"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
endpts "github.com/longjoy/micro-go-book/ch13-seckill/sk-admin/endpoint"
"github.com/longjoy/micro-go-book/ch13-seckill/sk-admin/model"
gozipkin "github.com/openzipkin/zipkin-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"os"
)
var (
ErrorBadRequest = errors.New("invalid request parameter")
)
// MakeHttpHandler make http handler use mux
func MakeHttpHandler(ctx context.Context, endpoints endpts.SkAdminEndpoints, zipkinTracer *gozipkin.Tracer, logger log.Logger) http.Handler {
r := mux.NewRouter()
zipkinServer := zipkin.HTTPServerTrace(zipkinTracer, zipkin.Name("http-transport"))
options := []kithttp.ServerOption{
//kithttp.ServerErrorLogger(logger),
kithttp.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
//kithttp.ServerErrorEncoder(kithttp.DefaultErrorEncoder),
kithttp.ServerErrorEncoder(encodeError),
zipkinServer,
}
r.Methods("GET").Path("/product/list").Handler(kithttp.NewServer(
endpoints.GetProductEndpoint,
decodeGetListRequest,
encodeResponse,
options...,
))
r.Methods("POST").Path("/product/create").Handler(kithttp.NewServer(
endpoints.CreateProductEndpoint,
decodeCreateProductCheckRequest,
encodeResponse,
options...,
))
r.Methods("POST").Path("/activity/create").Handler(kithttp.NewServer(
endpoints.CreateActivityEndpoint,
decodeCreateActivityCheckRequest,
encodeResponse,
options...,
))
r.Methods("GET").Path("/activity/list").Handler(kithttp.NewServer(
endpoints.GetActivityEndpoint,
decodeGetListRequest,
encodeResponse,
options...,
))
r.Path("/metrics").Handler(promhttp.Handler())
// create health check handler
r.Methods("GET").Path("/health").Handler(kithttp.NewServer(
endpoints.HealthCheckEndpoint,
decodeHealthCheckRequest,
encodeResponse,
options...,
))
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
return loggedRouter
}
// decodeUserRequest decode request params to struct
func decodeGetListRequest(_ context.Context, r *http.Request) (interface{}, error) {
return endpts.GetListRequest{}, nil
}
// 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 {
default:
w.WriteHeader(http.StatusInternalServerError)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
}
// encodeArithmeticResponse encode response to return
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
return json.NewEncoder(w).Encode(response)
}
// decodeHealthCheckRequest decode request
func decodeHealthCheckRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return endpts.HealthRequest{}, nil
}
func decodeCreateProductCheckRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var product model.Product
if err := json.NewDecoder(r.Body).Decode(&product); err != nil {
return nil, err
}
return product, nil
}
func decodeCreateActivityCheckRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var activity model.Activity
if err := json.NewDecoder(r.Body).Decode(&activity); err != nil {
return nil, err
}
return activity, nil
}