builder_params.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  5. "github.com/pkg/errors"
  6. "net/http"
  7. )
  8. type builderParams struct {
  9. httpMethod string
  10. relativePath string
  11. requestItems []*builderRequestItem
  12. globalRequestResponseCallback GlobalRequestResponseCallback
  13. responseSuccessCallback ResponseSuccessCallback
  14. responseErrorCallback ResponseErrorCallback
  15. }
  16. func newBuilderParams() *builderParams {
  17. return &builderParams{
  18. httpMethod: "",
  19. relativePath: "",
  20. requestItems: make([]*builderRequestItem, 0),
  21. globalRequestResponseCallback: nil,
  22. responseSuccessCallback: func(c *api.Context, historyRequests []BuilderRequest, customResultMap map[string]any) {
  23. c.Status(http.StatusOK)
  24. if historyRequests != nil && len(historyRequests) != 0 {
  25. _, err := c.Writer.Write(historyRequests[len(historyRequests)-1].Response().Body())
  26. if err != nil {
  27. logger.GetInstance().Error(errors.New(err.Error()))
  28. c.AbortWithStatus(http.StatusInternalServerError)
  29. return
  30. }
  31. }
  32. c.Writer.Flush()
  33. },
  34. responseErrorCallback: func(c *api.Context, err error) {
  35. if err == nil {
  36. return
  37. }
  38. resp := make(map[string]any)
  39. logger.GetInstance().Error(err)
  40. resp["success"] = false
  41. resp["errCode"] = http.StatusOK
  42. resp["msg"] = err.Error()
  43. c.JSON(http.StatusOK, resp)
  44. },
  45. }
  46. }
  47. func (params *builderParams) copy() *builderParams {
  48. return &builderParams{
  49. httpMethod: params.httpMethod,
  50. relativePath: params.relativePath,
  51. requestItems: params.requestItems,
  52. globalRequestResponseCallback: params.globalRequestResponseCallback,
  53. responseSuccessCallback: params.responseSuccessCallback,
  54. responseErrorCallback: params.responseErrorCallback,
  55. }
  56. }