http_binding.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package http_binding
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/middleware"
  6. "git.sxidc.com/go-tools/api_binding/http_binding/request"
  7. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  8. "git.sxidc.com/go-tools/utils/strutils"
  9. "git.sxidc.com/service-supports/fserr"
  10. "git.sxidc.com/service-supports/fslog"
  11. "github.com/gin-gonic/gin"
  12. "net/http"
  13. "reflect"
  14. "strings"
  15. )
  16. type BusinessFunc[I any, O any] func(c *binding_context.Context, inputModel I) (O, error)
  17. type BindingFunc[O any] func(c *binding_context.Context, request any, sendFunc response.SendFunc[O]) bool
  18. type Binding struct {
  19. routerGroup *gin.RouterGroup
  20. }
  21. // NewBinding 创建版本对应的binding
  22. func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
  23. apiPrefix := urlPrefix + "/api"
  24. if strutils.IsStringNotEmpty(apiVersion) && apiVersion != "root" {
  25. apiPrefix += "/" + apiVersion
  26. }
  27. // 把binding.Middleware转换为gin的中间件函数
  28. ginMiddlewares := make([]gin.HandlerFunc, 0)
  29. for _, m := range middlewares {
  30. innerM := m
  31. ginMiddlewares = append(ginMiddlewares, func(c *gin.Context) {
  32. innerM(&binding_context.Context{Context: c})
  33. })
  34. }
  35. // 返回创建的路由组
  36. return &Binding{routerGroup: routerInstance.Group(apiPrefix, ginMiddlewares...)}
  37. }
  38. type HandleFunc func(c *binding_context.Context)
  39. func (binding *Binding) AddHandler(method string, relativePath string, handleFunctions ...HandleFunc) error {
  40. if strutils.IsStringEmpty(method) {
  41. return errors.New("没有传递方法名")
  42. }
  43. if strutils.IsStringEmpty(relativePath) {
  44. return errors.New("没有传递相对路径")
  45. }
  46. if handleFunctions == nil || len(handleFunctions) == 0 {
  47. return errors.New("没有传递处理函数")
  48. }
  49. routeInfos := routerInstance.Routes()
  50. for _, routeInfo := range routeInfos {
  51. if routeInfo.Method == method && strings.TrimPrefix(routeInfo.Path, binding.routerGroup.BasePath()+"/") == relativePath {
  52. fslog.Error("添加的路径已存在: " + routeInfo.Path)
  53. return nil
  54. }
  55. }
  56. // 给单个路由增加中间件
  57. ginHandleFunctions := make([]gin.HandlerFunc, 0)
  58. for _, handleFunction := range handleFunctions {
  59. innerFunction := handleFunction
  60. ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
  61. innerFunction(&binding_context.Context{Context: c})
  62. })
  63. }
  64. binding.routerGroup.Handle(method, relativePath, ginHandleFunctions...)
  65. return nil
  66. }
  67. func PostBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  68. item.bind(b.routerGroup, http.MethodPost, middlewares...)
  69. }
  70. func DeleteBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  71. item.bind(b.routerGroup, http.MethodDelete, middlewares...)
  72. }
  73. func PutBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  74. item.bind(b.routerGroup, http.MethodPut, middlewares...)
  75. }
  76. func GetBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
  77. item.bind(b.routerGroup, http.MethodGet, middlewares...)
  78. }
  79. func Bind[I any, O any](b *Binding, item *BindItem[I, O], middlewares ...middleware.Func) {
  80. item.bind(b.routerGroup, middlewares...)
  81. }
  82. func Static(b *Binding, item *StaticBindItem) {
  83. item.bind(b.routerGroup)
  84. }
  85. func StaticFile(b *Binding, item *StaticFileBindItem) {
  86. item.bind(b.routerGroup)
  87. }
  88. // SimpleBindItem 路由条目
  89. type SimpleBindItem[I any, O any] struct {
  90. Path string // 请求路径
  91. ResponseFunc response.SendFunc[O] // 响应泛型函数
  92. BusinessFunc BusinessFunc[I, O] // 业务泛型函数
  93. OptionalBindingFunc BindingFunc[O] // 可选的绑定函数
  94. }
  95. func (item *SimpleBindItem[I, O]) bind(routerGroup *gin.RouterGroup, method string, middlewares ...middleware.Func) {
  96. bindingItem := &BindItem[I, O]{
  97. Method: method,
  98. SimpleBindItem: item,
  99. }
  100. bindingItem.bind(routerGroup, middlewares...)
  101. }
  102. // BindItem 路由条目结构
  103. type BindItem[I any, O any] struct {
  104. Method string
  105. *SimpleBindItem[I, O]
  106. }
  107. func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...middleware.Func) {
  108. if strutils.IsStringEmpty(item.Path) {
  109. panic("需要指定路径")
  110. }
  111. if strutils.IsStringEmpty(item.Method) {
  112. panic("需要指定方法")
  113. }
  114. if item.ResponseFunc == nil {
  115. panic("需要指定响应函数")
  116. }
  117. var inputCheckModel I
  118. inputType := reflect.TypeOf(inputCheckModel)
  119. if inputType != nil {
  120. if inputType.Kind() == reflect.Pointer {
  121. panic("输入对象不能使用指针类型")
  122. }
  123. if inputType.Kind() != reflect.Struct {
  124. panic("输入对象必须是结构")
  125. }
  126. }
  127. // 给单个路由增加中间件
  128. ginHandleFunctions := make([]gin.HandlerFunc, 0)
  129. for _, m := range middlewares {
  130. innerM := m
  131. ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
  132. innerM(&binding_context.Context{Context: c})
  133. })
  134. }
  135. ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
  136. bindingContext := &binding_context.Context{Context: c}
  137. var inputModel I
  138. // 请求的结构类型不为any
  139. if inputType != nil {
  140. // 将请求数据解析到inputModel中
  141. if item.OptionalBindingFunc != nil {
  142. ok := item.OptionalBindingFunc(bindingContext, &inputModel, item.ResponseFunc)
  143. if !ok {
  144. return
  145. }
  146. } else {
  147. switch item.Method {
  148. case http.MethodPost:
  149. fallthrough
  150. case http.MethodPut:
  151. ok := request.BindingJson(bindingContext, &inputModel, item.ResponseFunc)
  152. if !ok {
  153. return
  154. }
  155. case http.MethodGet:
  156. fallthrough
  157. case http.MethodDelete:
  158. ok := request.BindingQuery(bindingContext, &inputModel, item.ResponseFunc)
  159. if !ok {
  160. return
  161. }
  162. }
  163. }
  164. }
  165. // 执行业务函数
  166. if item.BusinessFunc != nil {
  167. statusCode := http.StatusOK
  168. outputModel, err := item.BusinessFunc(bindingContext, inputModel)
  169. if err != nil {
  170. statusCode = fserr.ParseCode(err).HttpCode
  171. }
  172. item.ResponseFunc(bindingContext, statusCode, outputModel, err)
  173. return
  174. }
  175. })
  176. // 所有的函数加入到执行链中
  177. routerGroup.Handle(item.Method, item.Path, ginHandleFunctions...)
  178. }
  179. // StaticBindItem 静态路由item
  180. type StaticBindItem struct {
  181. RelativePath string
  182. Root string
  183. WithBasePath bool
  184. }
  185. func (item *StaticBindItem) bind(routerGroup *gin.RouterGroup) {
  186. if item.WithBasePath {
  187. routerGroup.Static(strings.TrimPrefix(item.RelativePath, routerGroup.BasePath()), item.Root)
  188. } else {
  189. routerGroup.Static(item.RelativePath, item.Root)
  190. }
  191. }
  192. type StaticFileBindItem struct {
  193. RelativePath string
  194. FilePath string
  195. WithBasePath bool
  196. }
  197. func (item *StaticFileBindItem) bind(routerGroup *gin.RouterGroup) {
  198. if item.WithBasePath {
  199. routerGroup.StaticFile(strings.TrimPrefix(item.RelativePath, routerGroup.BasePath()), item.FilePath)
  200. } else {
  201. routerGroup.StaticFile(item.RelativePath, item.FilePath)
  202. }
  203. }