binding.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package binding
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/domain"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "git.sxidc.com/service-supports/fserr"
  7. "net/http"
  8. "strings"
  9. )
  10. func PostBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
  11. item.bind(router, http.MethodPost, middlewares...)
  12. }
  13. func DeleteBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
  14. item.bind(router, http.MethodDelete, middlewares...)
  15. }
  16. func PutBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
  17. item.bind(router, http.MethodPut, middlewares...)
  18. }
  19. func GetBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
  20. item.bind(router, http.MethodGet, middlewares...)
  21. }
  22. func Bind[O any](router api.Router, item *BindItem[O], middlewares ...api.Handler) {
  23. item.bind(router, middlewares...)
  24. }
  25. func Static(router api.Router, item *StaticBindItem) {
  26. item.bind(router)
  27. }
  28. func StaticFile(router api.Router, item *StaticFileBindItem) {
  29. item.bind(router)
  30. }
  31. type DTOBindFunc func(c *api.Context, dto DTO) error
  32. type FormDomainObjectsFunc func(c *api.Context, dto DTO) ([]domain.Object, error)
  33. type ServiceFunc[O any] func(c *api.Context, dto DTO, objects []domain.Object) (O, error)
  34. type ResponseFunc[O any] func(c *api.Context, statusCode int, data O, err error)
  35. // SimpleBindItem 路由条目
  36. type SimpleBindItem[O any] struct {
  37. // URL相对路径
  38. Path string
  39. // 使用的dto,非必传,当dto为nil时,说明该接口没有参数
  40. DTO DTO
  41. // 可选的dto绑定函数
  42. // 非必传,POST和PUT请求默认为JsonBody,DELETE默认为PathParams,Get默认为QueryParams
  43. // 额外还提供了一些转化函数:
  44. // MultipartForm: 绑定multipart form
  45. // Form: 绑定form body
  46. // XMLBody: 绑定XML body
  47. DTOBindFunc DTOBindFunc
  48. // 通过DTO构造使用的领域对象,之后在ServiceFunc中会按照构造实体的顺序进行回调
  49. // 非必传,如果为nil,则说明没有领域对象
  50. // 与Objects字段二选一使用,如果都指定,会按照该字段处理
  51. FormDomainObjectsFunc FormDomainObjectsFunc
  52. // 使用的领域对象,当使用Tag对实体进行标注后,可以直接通过该字段给定实体,之后在ServiceFunc中会按照给定实体的顺序进行回调
  53. // 非必传,如果为nil或长度为0,则说明没有领域对象
  54. // 与FormObjectsFunc字段二选一使用,如果都指定,会按照FormObjectsFunc字段处理
  55. Objects []domain.Object
  56. // 应用服务泛型函数
  57. // 非必传,为nil时相当于使用了SmartUI
  58. ServiceFunc ServiceFunc[O]
  59. // 响应泛型函数,如果不响应,需要使用NoResponse零值占位
  60. ResponseFunc ResponseFunc[O]
  61. }
  62. func (item *SimpleBindItem[O]) bind(router api.Router, method string, middlewares ...api.Handler) {
  63. bindingItem := &BindItem[O]{
  64. Method: method,
  65. SimpleBindItem: item,
  66. }
  67. bindingItem.bind(router, middlewares...)
  68. }
  69. // BindItem 路由条目结构
  70. type BindItem[O any] struct {
  71. Method string
  72. *SimpleBindItem[O]
  73. }
  74. func (item *BindItem[O]) bind(router api.Router, middlewares ...api.Handler) {
  75. if strutils.IsStringEmpty(item.Path) {
  76. panic("需要指定路径")
  77. }
  78. if strutils.IsStringEmpty(item.Method) {
  79. panic("需要指定方法")
  80. }
  81. if item.ResponseFunc == nil {
  82. panic("需要指定响应函数")
  83. }
  84. // 给单个路由增加中间件
  85. handlers := []api.Handler{
  86. func(c *api.Context) {
  87. dto := item.DTO
  88. // 有请求数据
  89. if dto != nil {
  90. // 将请求数据解析到dto中
  91. if item.DTOBindFunc != nil {
  92. err := item.DTOBindFunc(c, dto)
  93. if err != nil {
  94. var outputZero O
  95. item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
  96. return
  97. }
  98. } else {
  99. switch item.Method {
  100. case http.MethodPost:
  101. fallthrough
  102. case http.MethodPut:
  103. err := JsonBody(c, dto)
  104. if err != nil {
  105. var outputZero O
  106. item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
  107. return
  108. }
  109. case http.MethodGet:
  110. err := QueryParams(c, dto)
  111. if err != nil {
  112. var outputZero O
  113. item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
  114. return
  115. }
  116. case http.MethodDelete:
  117. err := PathParams(c, dto)
  118. if err != nil {
  119. var outputZero O
  120. item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
  121. return
  122. }
  123. }
  124. }
  125. }
  126. // 进行领域对象转化
  127. var domainObjects []domain.Object
  128. if item.FormDomainObjectsFunc != nil {
  129. innerDomainObjects, err := item.FormDomainObjectsFunc(c, dto)
  130. if err != nil {
  131. var outputZero O
  132. item.ResponseFunc(c, http.StatusOK, outputZero, err)
  133. return
  134. }
  135. domainObjects = innerDomainObjects
  136. } else {
  137. // TODO Tag处理
  138. }
  139. // 执行业务函数
  140. if item.ServiceFunc != nil {
  141. statusCode := http.StatusOK
  142. outputModel, err := item.ServiceFunc(c, dto, domainObjects)
  143. if err != nil {
  144. statusCode = fserr.ParseCode(err).HttpCode
  145. }
  146. item.ResponseFunc(c, statusCode, outputModel, err)
  147. return
  148. }
  149. },
  150. }
  151. handlers = append(handlers, middlewares...)
  152. // 所有的函数加入到执行链中
  153. router.AddRoute(item.Method, item.Path, handlers...)
  154. }
  155. // StaticBindItem 静态路由item
  156. type StaticBindItem struct {
  157. RelativePath string
  158. Root string
  159. WithBasePath bool
  160. }
  161. func (item *StaticBindItem) bind(router api.Router) {
  162. if item.WithBasePath {
  163. router.Static(strings.TrimPrefix(item.RelativePath, router.BasePath()), item.Root)
  164. } else {
  165. router.Static(item.RelativePath, item.Root)
  166. }
  167. }
  168. type StaticFileBindItem struct {
  169. RelativePath string
  170. FilePath string
  171. WithBasePath bool
  172. }
  173. func (item *StaticFileBindItem) bind(router api.Router) {
  174. if item.WithBasePath {
  175. router.StaticFile(strings.TrimPrefix(item.RelativePath, router.BasePath()), item.FilePath)
  176. } else {
  177. router.StaticFile(item.RelativePath, item.FilePath)
  178. }
  179. }