sql_executor.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "github.com/gin-gonic/gin"
  7. "github.com/pkg/errors"
  8. "net/http"
  9. "net/url"
  10. "sync"
  11. )
  12. type Option func(options *Options)
  13. type Options struct {
  14. serviceApiVersion string
  15. }
  16. func WithServiceApiVersion(serviceApiVersion string) Option {
  17. return func(options *Options) {
  18. options.serviceApiVersion = serviceApiVersion
  19. }
  20. }
  21. var serviceBaseUrlMap sync.Map
  22. func RegisterService(serviceShortName string, baseUrl string) {
  23. serviceBaseUrlMap.Store(serviceShortName, baseUrl)
  24. }
  25. func BuildGateway(gw *gateway.Gateway, opts ...Option) {
  26. options := new(Options)
  27. for _, opt := range opts {
  28. opt(options)
  29. }
  30. builder := gw.NewBuilder(api.RouterPrefix, "")
  31. builder.
  32. Url(http.MethodPost, "/sql/execute").
  33. Post(gateway.NewPostRequest("",
  34. gateway.PostRequestWithUrlTransferFunc(
  35. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  36. jsonBody, err := c.GetJsonBody()
  37. if err != nil {
  38. return "", err
  39. }
  40. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  41. if !ok {
  42. return "", errors.New("没有传递服务名缩写或服务名缩写不是string类型")
  43. }
  44. jsonBody.Delete("serviceShortName")
  45. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  46. if !loaded {
  47. return "", errors.New("没有注册对应的服务: " + serviceShortName)
  48. }
  49. var serviceUrl string
  50. if strutils.IsStringEmpty(options.serviceApiVersion) {
  51. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute")
  52. if err != nil {
  53. return "", err
  54. }
  55. serviceUrl = innerServiceUrl
  56. } else {
  57. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute")
  58. if err != nil {
  59. return "", err
  60. }
  61. serviceUrl = innerServiceUrl
  62. }
  63. return serviceUrl, nil
  64. }),
  65. gateway.PostRequestWithBodyForm(
  66. func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
  67. err := gateway.AddJsonBodyTenantIDAndUserID(c, "", "executorId")
  68. if err != nil {
  69. return nil, err
  70. }
  71. jsonBody, err := c.GetJsonBody()
  72. if err != nil {
  73. return nil, err
  74. }
  75. userInfo := c.GetUserInfo()
  76. if userInfo == nil {
  77. jsonBody.Set("executorName", "guest")
  78. } else {
  79. jsonBody.Set("executorName", userInfo.GetName)
  80. }
  81. return jsonBody.Map(), nil
  82. })), nil).
  83. Build()
  84. builder.
  85. Url(http.MethodGet, "/sql/execute/log").
  86. Get(gateway.NewGetRequest("",
  87. gateway.GetRequestWithUrlTransferFunc(
  88. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  89. queryParams := c.GetQueryParams()
  90. serviceShortName := queryParams.Get("serviceShortName")
  91. if strutils.IsStringEmpty(serviceShortName) {
  92. return "", errors.New("没有传递服务名缩写")
  93. }
  94. queryParams.Delete("serviceShortName")
  95. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  96. if !loaded {
  97. return "", errors.New("没有注册对应的服务: " + serviceShortName)
  98. }
  99. var serviceUrl string
  100. if strutils.IsStringEmpty(options.serviceApiVersion) {
  101. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute/log")
  102. if err != nil {
  103. return "", err
  104. }
  105. serviceUrl = innerServiceUrl
  106. } else {
  107. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute/log")
  108. if err != nil {
  109. return "", err
  110. }
  111. serviceUrl = innerServiceUrl
  112. }
  113. return serviceUrl, nil
  114. }),
  115. gateway.GetRequestWithQueryParamsForm(
  116. gateway.FormQueryParamsWithTenantIDAndUserIDFunc("", "executorId")),
  117. ), nil).
  118. Build()
  119. // 查询注册的服务
  120. builder.
  121. Url(http.MethodGet, "/sql/execute/registered/services").
  122. Local(func(c *api.Context) {
  123. serviceShortNames := make([]string, 0)
  124. serviceBaseUrlMap.Range(func(key any, value any) bool {
  125. serviceShortNames = append(serviceShortNames, key.(string))
  126. return true
  127. })
  128. c.JSON(http.StatusOK, gin.H{
  129. "services": serviceShortNames,
  130. })
  131. })
  132. }