configuration.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package configuration
  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. createMiddlewares []gateway.Handler
  17. deleteMiddlewares []gateway.Handler
  18. queryMiddlewares []gateway.Handler
  19. queryRegisteredServicesMiddlewares []gateway.Handler
  20. }
  21. func WithServiceApiVersion(serviceApiVersion string) Option {
  22. return func(options *Options) {
  23. options.serviceApiVersion = serviceApiVersion
  24. }
  25. }
  26. func WithGlobalMiddlewares(middlewares ...gateway.Handler) Option {
  27. return func(options *Options) {
  28. options.globalMiddlewares = middlewares
  29. }
  30. }
  31. func WithCreateMiddlewares(middlewares ...gateway.Handler) Option {
  32. return func(options *Options) {
  33. options.createMiddlewares = middlewares
  34. }
  35. }
  36. func WithDeleteMiddlewares(middlewares ...gateway.Handler) Option {
  37. return func(options *Options) {
  38. options.deleteMiddlewares = middlewares
  39. }
  40. }
  41. func WithQueryMiddlewares(middlewares ...gateway.Handler) Option {
  42. return func(options *Options) {
  43. options.queryMiddlewares = middlewares
  44. }
  45. }
  46. func WithQueryRegisteredServicesMiddlewares(middlewares ...gateway.Handler) Option {
  47. return func(options *Options) {
  48. options.queryRegisteredServicesMiddlewares = middlewares
  49. }
  50. }
  51. var serviceBaseUrlMap sync.Map
  52. func RegisterService(serviceShortName string, baseUrl string) {
  53. serviceBaseUrlMap.Store(serviceShortName, baseUrl)
  54. }
  55. func BuildGateway(gw *gateway.Gateway, opts ...Option) {
  56. options := new(Options)
  57. for _, opt := range opts {
  58. opt(options)
  59. }
  60. createMiddlewares := append(options.globalMiddlewares, options.createMiddlewares...)
  61. deleteMiddlewares := append(options.globalMiddlewares, options.deleteMiddlewares...)
  62. queryMiddlewares := append(options.globalMiddlewares, options.queryMiddlewares...)
  63. queryRegisteredServicesMiddlewares := append(options.globalMiddlewares, options.queryRegisteredServicesMiddlewares...)
  64. builder := gw.NewBuilder(api.RouterPrefix, "")
  65. // 创建
  66. builder.
  67. PostRoute("/configuration/create",
  68. func(requestBuilder *gateway.RequestBuilder) {
  69. jsonBody, err := requestBuilder.ApiContext().GetJsonBody()
  70. if err != nil {
  71. requestBuilder.ResponseError(err)
  72. return
  73. }
  74. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  75. if !ok {
  76. requestBuilder.ResponseError(errors.New("没有传递服务名缩写或服务名缩写不是string类型"))
  77. return
  78. }
  79. jsonBody.Delete("serviceShortName")
  80. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  81. if !loaded {
  82. requestBuilder.ResponseError(errors.New("没有注册对应的服务: " + serviceShortName))
  83. return
  84. }
  85. var serviceUrl string
  86. if strutils.IsStringEmpty(options.serviceApiVersion) {
  87. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/configuration/create")
  88. if err != nil {
  89. requestBuilder.ResponseError(err)
  90. return
  91. }
  92. serviceUrl = innerServiceUrl
  93. } else {
  94. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/configuration/create")
  95. if err != nil {
  96. requestBuilder.ResponseError(err)
  97. return
  98. }
  99. serviceUrl = innerServiceUrl
  100. }
  101. requestBuilder.
  102. Post(&gateway.PostRequest{
  103. Url: serviceUrl,
  104. }).
  105. Request()
  106. }, createMiddlewares...)
  107. // 删除
  108. builder.
  109. PostRoute("/configuration/delete",
  110. func(requestBuilder *gateway.RequestBuilder) {
  111. jsonBody, err := requestBuilder.ApiContext().GetJsonBody()
  112. if err != nil {
  113. requestBuilder.ResponseError(err)
  114. return
  115. }
  116. serviceShortName, ok := jsonBody.Get("serviceShortName").(string)
  117. if !ok {
  118. requestBuilder.ResponseError(errors.New("没有传递服务名缩写或服务名缩写不是string类型"))
  119. return
  120. }
  121. jsonBody.Delete("serviceShortName")
  122. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  123. if !loaded {
  124. requestBuilder.ResponseError(errors.New("没有注册对应的服务: " + serviceShortName))
  125. return
  126. }
  127. var serviceUrl string
  128. if strutils.IsStringEmpty(options.serviceApiVersion) {
  129. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/configuration/delete")
  130. if err != nil {
  131. requestBuilder.ResponseError(err)
  132. return
  133. }
  134. serviceUrl = innerServiceUrl
  135. } else {
  136. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/configuration/delete")
  137. if err != nil {
  138. requestBuilder.ResponseError(err)
  139. return
  140. }
  141. serviceUrl = innerServiceUrl
  142. }
  143. requestBuilder.
  144. Post(&gateway.PostRequest{
  145. Url: serviceUrl,
  146. }).
  147. Request()
  148. }, deleteMiddlewares...)
  149. // 查询
  150. builder.
  151. GetRoute("/configuration/values",
  152. func(requestBuilder *gateway.RequestBuilder) {
  153. queryParams := requestBuilder.ApiContext().GetQueryParams()
  154. serviceShortName := queryParams.Get("serviceShortName")
  155. if strutils.IsStringEmpty(serviceShortName) {
  156. requestBuilder.ResponseError(errors.New("没有传递服务名缩写"))
  157. return
  158. }
  159. queryParams.Delete("serviceShortName")
  160. serviceBaseUrl, loaded := serviceBaseUrlMap.Load(serviceShortName)
  161. if !loaded {
  162. requestBuilder.ResponseError(errors.New("没有注册对应的服务: " + serviceShortName))
  163. return
  164. }
  165. var serviceUrl string
  166. if strutils.IsStringEmpty(options.serviceApiVersion) {
  167. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", "/configuration/values")
  168. if err != nil {
  169. requestBuilder.ResponseError(err)
  170. return
  171. }
  172. serviceUrl = innerServiceUrl
  173. } else {
  174. innerServiceUrl, err := url.JoinPath(serviceBaseUrl.(string), serviceShortName, "/api", options.serviceApiVersion, "/configuration/values")
  175. if err != nil {
  176. requestBuilder.ResponseError(err)
  177. return
  178. }
  179. serviceUrl = innerServiceUrl
  180. }
  181. requestBuilder.
  182. Get(&gateway.GetRequest{
  183. Url: serviceUrl,
  184. }).
  185. Request()
  186. }, queryMiddlewares...)
  187. // 查询注册的服务
  188. builder.
  189. GetRoute("/configuration/registered/services", func(requestBuilder *gateway.RequestBuilder) {
  190. serviceShortNames := make([]string, 0)
  191. serviceBaseUrlMap.Range(func(key any, value any) bool {
  192. serviceShortNames = append(serviceShortNames, key.(string))
  193. return true
  194. })
  195. requestBuilder.ApiContext().JSON(http.StatusOK, gin.H{
  196. "services": serviceShortNames,
  197. })
  198. }, queryRegisteredServicesMiddlewares...)
  199. }