sql_executor.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. globalMiddlewares []gateway.Handler
  16. executeSqlMiddlewares []gateway.Handler
  17. sqlExecuteLogMiddlewares []gateway.Handler
  18. queryRegisteredServicesMiddlewares []gateway.Handler
  19. }
  20. func WithServiceApiVersion(serviceApiVersion string) Option {
  21. return func(options *Options) {
  22. options.serviceApiVersion = serviceApiVersion
  23. }
  24. }
  25. func WithGlobalMiddlewares(middlewares ...gateway.Handler) Option {
  26. return func(options *Options) {
  27. options.globalMiddlewares = middlewares
  28. }
  29. }
  30. func WithExecuteSqlMiddlewares(middlewares ...gateway.Handler) Option {
  31. return func(options *Options) {
  32. options.executeSqlMiddlewares = middlewares
  33. }
  34. }
  35. func WithSqlExecuteLogMiddlewares(middlewares ...gateway.Handler) Option {
  36. return func(options *Options) {
  37. options.sqlExecuteLogMiddlewares = middlewares
  38. }
  39. }
  40. func WithQueryRegisteredServicesMiddlewares(middlewares ...gateway.Handler) Option {
  41. return func(options *Options) {
  42. options.queryRegisteredServicesMiddlewares = middlewares
  43. }
  44. }
  45. var serviceBaseUrlMap sync.Map
  46. func RegisterService(serviceShortName string, baseUrl string) {
  47. serviceBaseUrlMap.Store(serviceShortName, baseUrl)
  48. }
  49. func BuildGateway(gw *gateway.Gateway, opts ...Option) {
  50. options := new(Options)
  51. for _, opt := range opts {
  52. opt(options)
  53. }
  54. executeSqlMiddlewares := append(options.globalMiddlewares, options.executeSqlMiddlewares...)
  55. sqlExecuteLogMiddlewares := append(options.globalMiddlewares, options.sqlExecuteLogMiddlewares...)
  56. queryRegisteredServicesMiddlewares := append(options.globalMiddlewares, options.queryRegisteredServicesMiddlewares...)
  57. builder := gw.NewBuilder(api.RouterPrefix, "")
  58. builder.
  59. PostRouteWithUserIDCommon("/sql/execute", "executorId",
  60. func(requestBuilder *gateway.RequestBuilder) {
  61. jsonBody, err := requestBuilder.Context().GetJsonBody()
  62. if err != nil {
  63. requestBuilder.ResponseError(err)
  64. return
  65. }
  66. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  67. if !ok {
  68. requestBuilder.ResponseError(errors.New("没有传递服务名缩写或服务名缩写不是string类型"))
  69. return
  70. }
  71. jsonBody.Delete("serviceShortName")
  72. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  73. if !loaded {
  74. requestBuilder.ResponseError(errors.New("没有注册对应的服务: " + serviceShortName))
  75. return
  76. }
  77. var serviceUrl string
  78. if strutils.IsStringEmpty(options.serviceApiVersion) {
  79. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute")
  80. if err != nil {
  81. requestBuilder.ResponseError(err)
  82. return
  83. }
  84. serviceUrl = innerServiceUrl
  85. } else {
  86. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute")
  87. if err != nil {
  88. requestBuilder.ResponseError(err)
  89. return
  90. }
  91. serviceUrl = innerServiceUrl
  92. }
  93. userInfo := requestBuilder.Context().GetUserInfo()
  94. if userInfo == nil {
  95. jsonBody.Set("executorName", "guest")
  96. } else {
  97. jsonBody.Set("executorName", userInfo.GetName)
  98. }
  99. requestBuilder.
  100. Post(&gateway.PostRequest{
  101. Url: serviceUrl,
  102. }).
  103. Request()
  104. }, executeSqlMiddlewares...)
  105. builder.
  106. GetRouteWithUserIDCommon("/sql/execute/log", "executorId",
  107. func(requestBuilder *gateway.RequestBuilder) {
  108. queryParams := requestBuilder.Context().GetQueryParams()
  109. serviceShortName := queryParams.Get("serviceShortName")
  110. if strutils.IsStringEmpty(serviceShortName) {
  111. requestBuilder.ResponseError(errors.New("没有传递服务名缩写"))
  112. return
  113. }
  114. queryParams.Delete("serviceShortName")
  115. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  116. if !loaded {
  117. requestBuilder.ResponseError(errors.New("没有注册对应的服务: " + serviceShortName))
  118. return
  119. }
  120. var serviceUrl string
  121. if strutils.IsStringEmpty(options.serviceApiVersion) {
  122. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/sql/execute/log")
  123. if err != nil {
  124. requestBuilder.ResponseError(err)
  125. return
  126. }
  127. serviceUrl = innerServiceUrl
  128. } else {
  129. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/sql/execute/log")
  130. if err != nil {
  131. requestBuilder.ResponseError(err)
  132. return
  133. }
  134. serviceUrl = innerServiceUrl
  135. }
  136. requestBuilder.
  137. Get(&gateway.GetRequest{
  138. Url: serviceUrl,
  139. }).
  140. Request()
  141. }, sqlExecuteLogMiddlewares...)
  142. // 查询注册的服务
  143. builder.
  144. GetRoute("/sql/execute/registered/services",
  145. func(requestBuilder *gateway.RequestBuilder) {
  146. serviceShortNames := make([]string, 0)
  147. serviceBaseUrlMap.Range(func(key any, value any) bool {
  148. serviceShortNames = append(serviceShortNames, key.(string))
  149. return true
  150. })
  151. requestBuilder.Context().JSON(http.StatusOK, gin.H{
  152. "services": serviceShortNames,
  153. })
  154. }, queryRegisteredServicesMiddlewares...)
  155. }