You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0KB

  1. package transports
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/go-kit/kit/tracing/zipkin"
  6. kithttp "github.com/go-kit/kit/transport/http"
  7. "github.com/gorilla/mux"
  8. gozipkin "github.com/openzipkin/zipkin-go"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. "github.com/tal-tech/go-zero/core/logx"
  11. "job_risk_third/internal/endpoints"
  12. "job_risk_third/internal/proto"
  13. "job_risk_third/pkg/utils"
  14. "net/http"
  15. )
  16. // MakeHttpHandler make http handler use mux
  17. func MakeHttpHandler(_ context.Context, endpoint endpoints.JobRiskThirdEndpoint, zipkinTracer *gozipkin.Tracer) http.Handler {
  18. r := mux.NewRouter()
  19. zipkinServer := zipkin.HTTPServerTrace(zipkinTracer, zipkin.Name("http-transport"))
  20. options := []kithttp.ServerOption{
  21. kithttp.ServerErrorEncoder(MyErrorEncoder),
  22. zipkinServer,
  23. }
  24. r.Methods("POST").Path("/api/v1/push/feature_deal").Handler(kithttp.NewServer(
  25. endpoint.PushFeatureDealEndpoint,
  26. decodePushFeatureDealRequest,
  27. encodeResponse,
  28. options...,
  29. ))
  30. r.Methods("POST").Path("/api/v1/push/user_info").Handler(kithttp.NewServer(
  31. endpoint.PushUserInfoEndpoint,
  32. decodePushUserInfoRequest,
  33. encodeResponse,
  34. options...,
  35. ))
  36. r.Methods("POST").Path("/api/v1/push/feature_info").Handler(kithttp.NewServer(
  37. endpoint.PushFeatureInfoEndpoint,
  38. decodePushFeatureInfoRequest,
  39. encodeResponse,
  40. options...,
  41. ))
  42. // create health check handler
  43. r.Methods("GET").Path("/health").Handler(kithttp.NewServer(
  44. endpoint.HealthCheckEndpoint,
  45. decodeHealthCheckRequest,
  46. encodeResponse,
  47. options...,
  48. ))
  49. r.Path("/metrics").Handler(promhttp.Handler())
  50. return r
  51. }
  52. // decodeHealthCheckRequest decode request
  53. func decodeHealthCheckRequest(_ context.Context, _ *http.Request) (interface{}, error) {
  54. return proto.HealthRequest{}, nil
  55. }
  56. func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
  57. w.Header().Set("Content-Type", "application/json;charset=utf-8")
  58. return json.NewEncoder(w).Encode(response)
  59. }
  60. func decodePushFeatureDealRequest(_ context.Context, r *http.Request) (interface{}, error) {
  61. if err := utils.Validation(r); err != nil {
  62. return nil, err
  63. }
  64. body := utils.Param(r, "__json_param__")
  65. var item proto.PushFeatureDealRequest
  66. err := json.Unmarshal([]byte(body), &item)
  67. if err != nil {
  68. logx.Error(err)
  69. return nil, utils.ErrorBadRequest
  70. }
  71. return item, nil
  72. }
  73. func decodePushUserInfoRequest(_ context.Context, r *http.Request) (interface{}, error) {
  74. if err := utils.Validation(r); err != nil {
  75. return nil, err
  76. }
  77. body := utils.Param(r, "__json_param__")
  78. var item proto.PushUserInfoRequest
  79. err := json.Unmarshal([]byte(body), &item)
  80. if err != nil {
  81. logx.Error(err)
  82. return nil, utils.ErrorBadRequest
  83. }
  84. return item, nil
  85. }
  86. func decodePushFeatureInfoRequest(_ context.Context, r *http.Request) (interface{}, error) {
  87. if err := utils.Validation(r); err != nil {
  88. return nil, err
  89. }
  90. body := utils.Param(r, "__json_param__")
  91. var item proto.PushFeatureInfoRequest
  92. err := json.Unmarshal([]byte(body), &item)
  93. if err != nil {
  94. logx.Error(err)
  95. return nil, utils.ErrorBadRequest
  96. }
  97. return item, nil
  98. }