http.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package transport
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "github.com/go-kit/kit/log"
  7. "github.com/go-kit/kit/transport"
  8. kithttp "github.com/go-kit/kit/transport/http"
  9. "github.com/gorilla/mux"
  10. "github.com/longjoy/micro-go-course/section32/endpoint"
  11. "github.com/longjoy/micro-go-course/section32/service"
  12. "github.com/prometheus/client_golang/prometheus/promhttp"
  13. "net/http"
  14. )
  15. var (
  16. ErrorBadRequest = errors.New("invalid request parameter")
  17. ErrorGrantTypeRequest = errors.New("invalid request grant type")
  18. ErrorTokenRequest = errors.New("invalid request token")
  19. ErrInvalidClientRequest = errors.New("invalid client message")
  20. )
  21. // MakeHttpHandler make http handler use mux
  22. func MakeHttpHandler(ctx context.Context, endpoints endpoint.OAuth2Endpoints, tokenService service.ResourceServerTokenService, logger log.Logger) http.Handler {
  23. r := mux.NewRouter()
  24. options := []kithttp.ServerOption{
  25. kithttp.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
  26. kithttp.ServerErrorEncoder(encodeError),
  27. }
  28. r.Path("/metrics").Handler(promhttp.Handler())
  29. oauth2AuthorizationOptions := []kithttp.ServerOption{
  30. kithttp.ServerBefore(makeOAuth2AuthorizationContext(tokenService, logger)),
  31. kithttp.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
  32. kithttp.ServerErrorEncoder(encodeError),
  33. }
  34. r.Methods("Get").Path("/index").Handler(kithttp.NewServer(
  35. endpoints.IndexEndpoint,
  36. decodeIndexRequest,
  37. encodeJsonResponse,
  38. oauth2AuthorizationOptions...,
  39. ))
  40. r.Methods("Get").Path("/sample").Handler(kithttp.NewServer(
  41. endpoints.SampleEndpoint,
  42. decodeSampleRequest,
  43. encodeJsonResponse,
  44. oauth2AuthorizationOptions...,
  45. ))
  46. r.Methods("Get").Path("/admin").Handler(kithttp.NewServer(
  47. endpoints.AdminEndpoint,
  48. decodeAdminRequest,
  49. encodeJsonResponse,
  50. oauth2AuthorizationOptions...,
  51. ))
  52. // create health check handler
  53. r.Methods("GET").Path("/health").Handler(kithttp.NewServer(
  54. endpoints.HealthCheckEndpoint,
  55. decodeHealthCheckRequest,
  56. encodeJsonResponse,
  57. options...,
  58. ))
  59. return r
  60. }
  61. func makeOAuth2AuthorizationContext(tokenService service.ResourceServerTokenService, logger log.Logger) kithttp.RequestFunc {
  62. return func(ctx context.Context, r *http.Request) context.Context {
  63. // 获取访问令牌
  64. accessTokenValue := r.Header.Get("Authorization")
  65. var err error
  66. if accessTokenValue != ""{
  67. // 获取令牌对应的用户信息和客户端信息
  68. oauth2Details, err := tokenService.GetOAuth2DetailsByAccessToken(accessTokenValue)
  69. if err != nil{
  70. return context.WithValue(ctx, endpoint.OAuth2ErrorKey, err)
  71. }
  72. return context.WithValue(ctx, endpoint.OAuth2DetailsKey, oauth2Details)
  73. }else {
  74. err = ErrorTokenRequest
  75. }
  76. return context.WithValue(ctx, endpoint.OAuth2ErrorKey, err)
  77. }
  78. }
  79. func decodeIndexRequest(ctx context.Context, r *http.Request)(interface{}, error) {
  80. return &endpoint.IndexRequest{}, nil
  81. }
  82. func decodeSampleRequest(ctx context.Context, r *http.Request) (interface{}, error) {
  83. return &endpoint.SampleRequest{}, nil
  84. }
  85. func decodeAdminRequest(ctx context.Context, r *http.Request) (interface{}, error) {
  86. return &endpoint.AdminRequest{}, nil
  87. }
  88. func encodeJsonResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
  89. w.Header().Set("Content-Type", "application/json;charset=utf-8")
  90. return json.NewEncoder(w).Encode(response)
  91. }
  92. // decodeHealthCheckRequest decode request
  93. func decodeHealthCheckRequest(ctx context.Context, r *http.Request) (interface{}, error) {
  94. return endpoint.HealthRequest{}, nil
  95. }
  96. // encode errors from business-logic
  97. func encodeError(_ context.Context, err error, w http.ResponseWriter) {
  98. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  99. switch err {
  100. default:
  101. w.WriteHeader(http.StatusInternalServerError)
  102. }
  103. json.NewEncoder(w).Encode(map[string]interface{}{
  104. "error": err.Error(),
  105. })
  106. }