123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package transport
- import (
- "context"
- "encoding/json"
- "errors"
- "github.com/go-kit/kit/log"
- "github.com/go-kit/kit/transport"
- kithttp "github.com/go-kit/kit/transport/http"
- "github.com/gorilla/mux"
- "github.com/longjoy/micro-go-course/section13/register/endpoint"
- "net/http"
- "os"
- )
- var (
- ErrorBadRequest = errors.New("invalid request parameter")
- )
- // MakeHttpHandler make http handler use mux
- func MakeHttpHandler(ctx context.Context, endpoints *endpoint.RegisterEndpoints) http.Handler {
- r := mux.NewRouter()
- kitLog := log.NewLogfmtLogger(os.Stderr)
- kitLog = log.With(kitLog, "ts", log.DefaultTimestampUTC)
- kitLog = log.With(kitLog, "caller", log.DefaultCaller)
- options := []kithttp.ServerOption{
- kithttp.ServerErrorHandler(transport.NewLogErrorHandler(kitLog)),
- kithttp.ServerErrorEncoder(encodeError),
- }
- r.Methods("GET").Path("/health").Handler(kithttp.NewServer(
- endpoints.HealthCheckEndpoint,
- decodeHealthCheckRequest,
- encodeJSONResponse,
- options...,
- ))
- r.Methods("GET").Path("/discovery/name").Handler(kithttp.NewServer(
- endpoints.DiscoveryEndpoint,
- decodeDiscoveryRequest,
- encodeJSONResponse,
- options...,
- ))
- return r
- }
- func decodeDiscoveryRequest(ctx context.Context, r *http.Request) (interface{}, error) {
- serviceName := r.URL.Query().Get("serviceName")
- if serviceName == ""{
- return nil, ErrorBadRequest
- }
- return endpoint.DiscoveryRequest{
- ServiceName:serviceName,
- }, nil
- }
- func decodeHealthCheckRequest(ctx context.Context, r *http.Request) (interface{}, error) {
- return endpoint.HealthRequest{}, nil
- }
- func encodeJSONResponse(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)
- }
- 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(),
- })
- }
|