binding.go 6.8 KB

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