package main import ( "context" "encoding/json" "errors" "github.com/go-kit/kit/log" "github.com/go-kit/kit/tracing/zipkin" kithttp "github.com/go-kit/kit/transport/http" "github.com/gorilla/mux" "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/endpoint" gozipkin "github.com/openzipkin/zipkin-go" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) var ( ErrorBadRequest = errors.New("invalid request parameter") ) // MakeHttpHandler make http handler use mux func MakeHttpHandler(ctx context.Context, endpoints endpoint.StringEndpoints, 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.ServerErrorEncoder(kithttp.DefaultErrorEncoder), zipkinServer, } r.Methods("POST").Path("/op/{type}/{a}/{b}").Handler(kithttp.NewServer( endpoints.StringEndpoint, decodeStringRequest, encodeStringResponse, options..., )) r.Path("/metrics").Handler(promhttp.Handler()) // create health check handler r.Methods("GET").Path("/health").Handler(kithttp.NewServer( endpoints.HealthCheckEndpoint, decodeHealthCheckRequest, encodeStringResponse, options..., )) return r } // decodeStringRequest decode request params to struct func decodeStringRequest(_ context.Context, r *http.Request) (interface{}, error) { vars := mux.Vars(r) requestType, ok := vars["type"] if !ok { return nil, ErrorBadRequest } pa, ok := vars["a"] if !ok { return nil, ErrorBadRequest } pb, ok := vars["b"] if !ok { return nil, ErrorBadRequest } return endpoint.StringRequest{ RequestType: requestType, A: pa, B: pb, }, nil } // encodeStringResponse encode response to return func encodeStringResponse(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 endpoint.HealthRequest{}, nil }