http_binding.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package http_binding
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/middleware"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/request"
  6. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  7. "git.sxidc.com/go-tools/api_binding/utils"
  8. "git.sxidc.com/service-supports/fserr"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  11. "reflect"
  12. "strings"
  13. )
  14. type BusinessFunc[I any, O any] func(c *binding_context.Context, inputModel I) (O, error)
  15. type BindingFunc[O any] func(c *binding_context.Context, request any, sendFunc response.SendFunc[O]) bool
  16. type Binding struct {
  17. routerGroup *gin.RouterGroup
  18. }
  19. func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
  20. apiPrefix := urlPrefix + "/api"
  21. if utils.IsStringNotEmpty(apiVersion) && apiVersion != "root" {
  22. apiPrefix += "/" + apiVersion
  23. }
  24. ginMiddlewares := make([]gin.HandlerFunc, 0)
  25. for _, m := range middlewares {
  26. innerM := m
  27. ginMiddlewares = append(ginMiddlewares, func(c *gin.Context) {
  28. innerM(&binding_context.Context{Context: c})
  29. })
  30. }
  31. return &Binding{routerGroup: routerInstance.Group(apiPrefix, ginMiddlewares...)}
  32. }
  33. func PostBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  34. item.bind(b.routerGroup, http.MethodPost, middlewares...)
  35. }
  36. func DeleteBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  37. item.bind(b.routerGroup, http.MethodDelete, middlewares...)
  38. }
  39. func PutBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  40. item.bind(b.routerGroup, http.MethodPut, middlewares...)
  41. }
  42. func GetBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  43. item.bind(b.routerGroup, http.MethodGet, middlewares...)
  44. }
  45. func Bind[I any, O any](b *Binding, item *BindItem[I, O], middlewares ...middleware.Func) {
  46. item.bind(b.routerGroup, middlewares...)
  47. }
  48. func Static(b *Binding, item *StaticBindItem) {
  49. item.bind(b.routerGroup)
  50. }
  51. type SimpleBindItem[I any, O any] struct {
  52. Path string
  53. ResponseFunc response.SendFunc[O]
  54. BusinessFunc BusinessFunc[I, O]
  55. OptionalBindingFunc BindingFunc[O]
  56. }
  57. func (item *SimpleBindItem[I, O]) bind(routerGroup *gin.RouterGroup, method string, middlewares ...middleware.Func) {
  58. bindingItem := &BindItem[I, O]{
  59. Method: method,
  60. SimpleBindItem: item,
  61. }
  62. bindingItem.bind(routerGroup, middlewares...)
  63. }
  64. type BindItem[I any, O any] struct {
  65. Method string
  66. *SimpleBindItem[I, O]
  67. }
  68. func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...middleware.Func) {
  69. if utils.IsStringEmpty(item.Path) {
  70. panic("需要指定路径")
  71. }
  72. if utils.IsStringEmpty(item.Method) {
  73. panic("需要指定方法")
  74. }
  75. if item.ResponseFunc == nil {
  76. panic("需要指定响应函数")
  77. }
  78. var inputModel I
  79. inputType := reflect.TypeOf(inputModel)
  80. if inputType != nil {
  81. if inputType.Kind() == reflect.Pointer {
  82. panic("输入对象不能使用指针类型")
  83. }
  84. if inputType.Kind() != reflect.Struct {
  85. panic("输入对象必须是结构")
  86. }
  87. }
  88. ginHandleFunctions := make([]gin.HandlerFunc, 0)
  89. for _, m := range middlewares {
  90. innerM := m
  91. ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
  92. innerM(&binding_context.Context{Context: c})
  93. })
  94. }
  95. ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
  96. bindingContext := &binding_context.Context{Context: c}
  97. if inputType != nil {
  98. if item.OptionalBindingFunc != nil {
  99. ok := item.OptionalBindingFunc(bindingContext, &inputModel, item.ResponseFunc)
  100. if !ok {
  101. return
  102. }
  103. } else {
  104. switch item.Method {
  105. case http.MethodPost:
  106. fallthrough
  107. case http.MethodPut:
  108. ok := request.BindingJson(bindingContext, &inputModel, item.ResponseFunc)
  109. if !ok {
  110. return
  111. }
  112. case http.MethodGet:
  113. fallthrough
  114. case http.MethodDelete:
  115. ok := request.BindingQuery(bindingContext, &inputModel, item.ResponseFunc)
  116. if !ok {
  117. return
  118. }
  119. }
  120. }
  121. }
  122. if item.BusinessFunc != nil {
  123. statusCode := http.StatusOK
  124. outputModel, err := item.BusinessFunc(bindingContext, inputModel)
  125. if err != nil {
  126. statusCode = fserr.ParseCode(err).HttpCode
  127. }
  128. item.ResponseFunc(bindingContext, statusCode, outputModel, err)
  129. return
  130. }
  131. })
  132. routerGroup.Handle(item.Method, item.Path, ginHandleFunctions...)
  133. }
  134. type StaticBindItem struct {
  135. RelativePath string
  136. Root string
  137. WithBasePath bool
  138. }
  139. func (item *StaticBindItem) bind(routerGroup *gin.RouterGroup) {
  140. if item.WithBasePath {
  141. routerGroup.Static(strings.TrimPrefix(item.RelativePath, routerGroup.BasePath()), item.Root)
  142. } else {
  143. routerGroup.Static(item.RelativePath, item.Root)
  144. }
  145. }