sql_executor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 RegisterService(serviceShortName string, baseUrl string) {
  22. serviceBaseUrlMap.Store(serviceShortName, baseUrl)
  23. }
  24. func BuildGateway(builder *gateway.Builder, opts ...Option) {
  25. options := new(Options)
  26. for _, opt := range opts {
  27. opt(options)
  28. }
  29. builder.
  30. Url(http.MethodPost, "/sql/execute").
  31. Post(gateway.NewPostRequest("",
  32. gateway.PostRequestWithUrlTransferFunc(
  33. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  34. jsonBody, err := c.GetJsonBody()
  35. if err != nil {
  36. return "", err
  37. }
  38. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  39. if !ok {
  40. return "", fserr.New("没有传递服务名缩写或服务名缩写不是string类型")
  41. }
  42. jsonBody.Delete("serviceShortName")
  43. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  44. if !loaded {
  45. return "", fserr.New("没有注册对应的服务: " + serviceShortName)
  46. }
  47. var serviceUrl string
  48. if strutils.IsStringEmpty(options.serviceApiVersion) {
  49. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute")
  50. if err != nil {
  51. return "", err
  52. }
  53. serviceUrl = innerServiceUrl
  54. } else {
  55. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute")
  56. if err != nil {
  57. return "", err
  58. }
  59. serviceUrl = innerServiceUrl
  60. }
  61. return serviceUrl, nil
  62. }),
  63. gateway.PostRequestWithBodyForm(
  64. func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
  65. err := gateway.AddJsonBodyTenantIDAndUserID(c, "", "executorId")
  66. if err != nil {
  67. return nil, err
  68. }
  69. jsonBody, err := c.GetJsonBody()
  70. if err != nil {
  71. return nil, err
  72. }
  73. userInfo := c.GetUserInfo()
  74. if userInfo == nil {
  75. jsonBody.Set("executorName", "guest")
  76. } else {
  77. jsonBody.Set("executorName", userInfo.GetName)
  78. }
  79. return jsonBody.Map(), nil
  80. })), nil).
  81. Build()
  82. builder.
  83. Url(http.MethodGet, "/sql/execute/log").
  84. Get(gateway.NewGetRequest("",
  85. gateway.GetRequestWithUrlTransferFunc(
  86. func(c *api.Context, _ string, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (string, error) {
  87. queryParams := c.GetQueryParams()
  88. serviceShortName := queryParams.Get("serviceShortName")
  89. if strutils.IsStringEmpty(serviceShortName) {
  90. return "", fserr.New("没有传递服务名缩写")
  91. }
  92. queryParams.Delete("serviceShortName")
  93. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  94. if !loaded {
  95. return "", fserr.New("没有注册对应的服务: " + serviceShortName)
  96. }
  97. var serviceUrl string
  98. if strutils.IsStringEmpty(options.serviceApiVersion) {
  99. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/configuration/values")
  100. if err != nil {
  101. return "", err
  102. }
  103. serviceUrl = innerServiceUrl
  104. } else {
  105. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/configuration/values")
  106. if err != nil {
  107. return "", err
  108. }
  109. serviceUrl = innerServiceUrl
  110. }
  111. return serviceUrl, nil
  112. }),
  113. gateway.GetRequestWithQueryParamsForm(
  114. gateway.FormQueryParamsWithTenantIDAndUserIDFunc("", "executorId")),
  115. ), nil).
  116. Build()
  117. }