|
@@ -14,7 +14,11 @@ import (
|
|
|
type Option func(options *Options)
|
|
type Option func(options *Options)
|
|
|
|
|
|
|
|
type Options struct {
|
|
type Options struct {
|
|
|
- serviceApiVersion string
|
|
|
|
|
|
|
+ serviceApiVersion string
|
|
|
|
|
+ globalMiddlewares []api.Handler
|
|
|
|
|
+ executeSqlMiddlewares []api.Handler
|
|
|
|
|
+ sqlExecuteLogMiddlewares []api.Handler
|
|
|
|
|
+ queryRegisteredServicesMiddlewares []api.Handler
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func WithServiceApiVersion(serviceApiVersion string) Option {
|
|
func WithServiceApiVersion(serviceApiVersion string) Option {
|
|
@@ -23,6 +27,30 @@ func WithServiceApiVersion(serviceApiVersion string) Option {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func WithGlobalMiddlewares(middlewares []api.Handler) Option {
|
|
|
|
|
+ return func(options *Options) {
|
|
|
|
|
+ options.globalMiddlewares = middlewares
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func WithExecuteSqlMiddlewares(middlewares []api.Handler) Option {
|
|
|
|
|
+ return func(options *Options) {
|
|
|
|
|
+ options.executeSqlMiddlewares = middlewares
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func WithSqlExecuteLogMiddlewares(middlewares []api.Handler) Option {
|
|
|
|
|
+ return func(options *Options) {
|
|
|
|
|
+ options.sqlExecuteLogMiddlewares = middlewares
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func WithQueryRegisteredServicesMiddlewares(middlewares []api.Handler) Option {
|
|
|
|
|
+ return func(options *Options) {
|
|
|
|
|
+ options.queryRegisteredServicesMiddlewares = middlewares
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
var serviceBaseUrlMap sync.Map
|
|
var serviceBaseUrlMap sync.Map
|
|
|
|
|
|
|
|
func RegisterService(serviceShortName string, baseUrl string) {
|
|
func RegisterService(serviceShortName string, baseUrl string) {
|
|
@@ -36,6 +64,10 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
opt(options)
|
|
opt(options)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ executeSqlMiddlewares := append(options.globalMiddlewares, options.executeSqlMiddlewares...)
|
|
|
|
|
+ sqlExecuteLogMiddlewares := append(options.globalMiddlewares, options.sqlExecuteLogMiddlewares...)
|
|
|
|
|
+ queryRegisteredServicesMiddlewares := append(options.globalMiddlewares, options.queryRegisteredServicesMiddlewares...)
|
|
|
|
|
+
|
|
|
builder := gw.NewBuilder(api.RouterPrefix, "")
|
|
builder := gw.NewBuilder(api.RouterPrefix, "")
|
|
|
|
|
|
|
|
builder.
|
|
builder.
|
|
@@ -101,7 +133,7 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
|
|
|
|
|
return jsonBody.Map(), nil
|
|
return jsonBody.Map(), nil
|
|
|
})), nil).
|
|
})), nil).
|
|
|
- Build()
|
|
|
|
|
|
|
+ Build(executeSqlMiddlewares...)
|
|
|
|
|
|
|
|
builder.
|
|
builder.
|
|
|
Url(http.MethodGet, "/sql/execute/log").
|
|
Url(http.MethodGet, "/sql/execute/log").
|
|
@@ -145,7 +177,7 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
gateway.GetRequestWithQueryParamsForm(
|
|
gateway.GetRequestWithQueryParamsForm(
|
|
|
gateway.FormQueryParamsWithTenantIDAndUserIDFunc("", "executorId")),
|
|
gateway.FormQueryParamsWithTenantIDAndUserIDFunc("", "executorId")),
|
|
|
), nil).
|
|
), nil).
|
|
|
- Build()
|
|
|
|
|
|
|
+ Build(sqlExecuteLogMiddlewares...)
|
|
|
|
|
|
|
|
// 查询注册的服务
|
|
// 查询注册的服务
|
|
|
builder.
|
|
builder.
|
|
@@ -160,5 +192,5 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
"services": serviceShortNames,
|
|
"services": serviceShortNames,
|
|
|
})
|
|
})
|
|
|
- })
|
|
|
|
|
|
|
+ }, queryRegisteredServicesMiddlewares...)
|
|
|
}
|
|
}
|