sql_executor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package sql_executor
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/gateway"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "git.sxidc.com/service-supports/fserr"
  7. "net/http"
  8. "net/url"
  9. "sync"
  10. )
  11. type Option func(options *Options)
  12. type Options struct {
  13. serviceApiVersion string
  14. }
  15. func WithServiceApiVersion(serviceApiVersion string) Option {
  16. return func(options *Options) {
  17. options.serviceApiVersion = serviceApiVersion
  18. }
  19. }
  20. var serviceBaseUrlMap sync.Map
  21. func BuildGateway(builder *gateway.Builder, opts ...Option) {
  22. options := new(Options)
  23. for _, opt := range opts {
  24. opt(options)
  25. }
  26. builder.
  27. Url(http.MethodPost, "/sql/execute").
  28. Post(gateway.NewPostRequest("",
  29. gateway.PostRequestWithUrlTransferFunc(
  30. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  31. jsonBody, err := c.GetJsonBody()
  32. if err != nil {
  33. return "", err
  34. }
  35. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  36. if !ok {
  37. return "", fserr.New("没有传递服务名缩写或服务名缩写不是string类型")
  38. }
  39. jsonBody.Delete("serviceShortName")
  40. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  41. if !loaded {
  42. return "", fserr.New("没有注册对应的服务: " + serviceShortName)
  43. }
  44. var serviceUrl string
  45. if strutils.IsStringEmpty(options.serviceApiVersion) {
  46. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute")
  47. if err != nil {
  48. return "", err
  49. }
  50. serviceUrl = innerServiceUrl
  51. } else {
  52. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute")
  53. if err != nil {
  54. return "", err
  55. }
  56. serviceUrl = innerServiceUrl
  57. }
  58. return serviceUrl, nil
  59. }),
  60. gateway.PostRequestWithBodyForm(
  61. func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
  62. err := gateway.AddJsonBodyTenantIDAndUserID(c, "", "executorId")
  63. if err != nil {
  64. return nil, err
  65. }
  66. jsonBody, err := c.GetJsonBody()
  67. if err != nil {
  68. return nil, err
  69. }
  70. userInfo := c.GetUserInfo()
  71. if userInfo == nil {
  72. jsonBody.Set("executorName", "guest")
  73. } else {
  74. jsonBody.Set("executorName", userInfo.GetName)
  75. }
  76. return jsonBody.Map(), nil
  77. })), nil).
  78. Build()
  79. builder.
  80. Url(http.MethodGet, "/sql/execute/log").
  81. Get(gateway.NewGetRequest("",
  82. gateway.GetRequestWithUrlTransferFunc(
  83. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  84. queryParams := c.GetQueryParams()
  85. serviceShortName := queryParams.Get("serviceShortName")
  86. if strutils.IsStringEmpty(serviceShortName) {
  87. return "", fserr.New("没有传递服务名缩写")
  88. }
  89. queryParams.Delete("serviceShortName")
  90. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  91. if !loaded {
  92. return "", fserr.New("没有注册对应的服务: " + serviceShortName)
  93. }
  94. var serviceUrl string
  95. if strutils.IsStringEmpty(options.serviceApiVersion) {
  96. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/configuration/values")
  97. if err != nil {
  98. return "", err
  99. }
  100. serviceUrl = innerServiceUrl
  101. } else {
  102. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/configuration/values")
  103. if err != nil {
  104. return "", err
  105. }
  106. serviceUrl = innerServiceUrl
  107. }
  108. return serviceUrl, nil
  109. }),
  110. gateway.GetRequestWithQueryParamsForm(
  111. gateway.FormQueryParamsWithTenantIDAndUserIDFunc("", "executorId")),
  112. ), nil).
  113. Build()
  114. }