binding.go 7.0 KB

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