123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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
- }
|