endpoints.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package endpoint
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/go-kit/kit/endpoint"
  6. "github.com/go-kit/kit/log"
  7. . "github.com/longjoy/micro-go-course/section32/model"
  8. "github.com/longjoy/micro-go-course/section32/service"
  9. )
  10. // CalculateEndpoint define endpoint
  11. type OAuth2Endpoints struct {
  12. IndexEndpoint endpoint.Endpoint
  13. SampleEndpoint endpoint.Endpoint
  14. AdminEndpoint endpoint.Endpoint
  15. HealthCheckEndpoint endpoint.Endpoint
  16. }
  17. func MakeOAuth2AuthorizationMiddleware(logger log.Logger) endpoint.Middleware {
  18. return func(next endpoint.Endpoint) endpoint.Endpoint {
  19. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  20. if err, ok := ctx.Value(OAuth2ErrorKey).(error); ok{
  21. return nil, err
  22. }
  23. if _, ok := ctx.Value(OAuth2DetailsKey).(*OAuth2Details); !ok{
  24. return nil, ErrInvalidUserRequest
  25. }
  26. return next(ctx, request)
  27. }
  28. }
  29. }
  30. func MakeAuthorityAuthorizationMiddleware(authority string, logger log.Logger) endpoint.Middleware {
  31. return func(next endpoint.Endpoint) endpoint.Endpoint {
  32. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  33. if err, ok := ctx.Value(OAuth2ErrorKey).(error); ok{
  34. return nil, err
  35. }
  36. if details, ok := ctx.Value(OAuth2DetailsKey).(*OAuth2Details); !ok{
  37. return nil, ErrInvalidClientRequest
  38. }else {
  39. for _, value := range details.User.Authorities{
  40. if value == authority{
  41. return next(ctx, request)
  42. }
  43. }
  44. return nil, ErrNotPermit
  45. }
  46. }
  47. }
  48. }
  49. const (
  50. OAuth2DetailsKey = "OAuth2Details"
  51. OAuth2ErrorKey = "OAuth2Error"
  52. )
  53. var (
  54. ErrInvalidClientRequest = errors.New("invalid client message")
  55. ErrInvalidUserRequest = errors.New("invalid user message")
  56. ErrNotPermit = errors.New("not permit")
  57. )
  58. type IndexRequest struct {
  59. }
  60. type IndexResponse struct {
  61. Result string `json:"result"`
  62. Error string `json:"error"`
  63. }
  64. type SampleRequest struct {
  65. }
  66. type SampleResponse struct {
  67. Result string `json:"result"`
  68. Error string `json:"error"`
  69. }
  70. type AdminRequest struct {
  71. }
  72. type AdminResponse struct {
  73. Result string `json:"result"`
  74. Error string `json:"error"`
  75. }
  76. func MakeIndexEndpoint(svc service.Service) endpoint.Endpoint {
  77. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  78. result := svc.Index()
  79. return &SampleResponse{
  80. Result:result,
  81. }, nil
  82. }
  83. }
  84. func MakeSampleEndpoint(svc service.Service) endpoint.Endpoint {
  85. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  86. result := svc.Sample(ctx.Value(OAuth2DetailsKey).(*OAuth2Details).User.Username)
  87. return &SampleResponse{
  88. Result:result,
  89. }, nil
  90. }
  91. }
  92. func MakeAdminEndpoint(svc service.Service) endpoint.Endpoint {
  93. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  94. result := svc.Admin(ctx.Value(OAuth2DetailsKey).(*OAuth2Details).User.Username)
  95. return &AdminResponse{
  96. Result:result,
  97. }, nil
  98. }
  99. }
  100. // HealthRequest 健康检查请求结构
  101. type HealthRequest struct{}
  102. // HealthResponse 健康检查响应结构
  103. type HealthResponse struct {
  104. Status bool `json:"status"`
  105. }
  106. // MakeHealthCheckEndpoint 创建健康检查Endpoint
  107. func MakeHealthCheckEndpoint(svc service.Service) endpoint.Endpoint {
  108. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  109. status := svc.HealthCheck()
  110. return HealthResponse{
  111. Status:status,
  112. }, nil
  113. }
  114. }