|
|
@@ -14,7 +14,12 @@ import (
|
|
|
type Option func(options *Options)
|
|
|
|
|
|
type Options struct {
|
|
|
- serviceApiVersion string
|
|
|
+ serviceApiVersion string
|
|
|
+ globalMiddlewares []api.Handler
|
|
|
+ createMiddlewares []api.Handler
|
|
|
+ deleteMiddlewares []api.Handler
|
|
|
+ queryMiddlewares []api.Handler
|
|
|
+ queryRegisteredServicesMiddlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
func WithServiceApiVersion(serviceApiVersion string) Option {
|
|
|
@@ -23,6 +28,36 @@ func WithServiceApiVersion(serviceApiVersion string) Option {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func WithGlobalMiddlewares(middlewares []api.Handler) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.globalMiddlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithCreateMiddlewares(middlewares []api.Handler) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.createMiddlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithDeleteMiddlewares(middlewares []api.Handler) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.deleteMiddlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithQueryMiddlewares(middlewares []api.Handler) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.queryMiddlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithQueryRegisteredServicesMiddlewares(middlewares []api.Handler) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.queryRegisteredServicesMiddlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
var serviceBaseUrlMap sync.Map
|
|
|
|
|
|
func RegisterService(serviceShortName string, baseUrl string) {
|
|
|
@@ -36,6 +71,11 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
opt(options)
|
|
|
}
|
|
|
|
|
|
+ createMiddlewares := append(options.globalMiddlewares, options.createMiddlewares...)
|
|
|
+ deleteMiddlewares := append(options.globalMiddlewares, options.deleteMiddlewares...)
|
|
|
+ queryMiddlewares := append(options.globalMiddlewares, options.queryMiddlewares...)
|
|
|
+ queryRegisteredServicesMiddlewares := append(options.globalMiddlewares, options.queryRegisteredServicesMiddlewares...)
|
|
|
+
|
|
|
builder := gw.NewBuilder(api.RouterPrefix, "")
|
|
|
|
|
|
// 创建
|
|
|
@@ -81,7 +121,7 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
|
|
|
return serviceUrl, nil
|
|
|
})), nil).
|
|
|
- Build()
|
|
|
+ Build(createMiddlewares...)
|
|
|
|
|
|
// 删除
|
|
|
builder.
|
|
|
@@ -126,7 +166,7 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
|
|
|
return serviceUrl, nil
|
|
|
})), nil).
|
|
|
- Build()
|
|
|
+ Build(deleteMiddlewares...)
|
|
|
|
|
|
// 查询
|
|
|
builder.
|
|
|
@@ -168,7 +208,7 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
|
|
|
return serviceUrl, nil
|
|
|
})), nil).
|
|
|
- Build()
|
|
|
+ Build(queryMiddlewares...)
|
|
|
|
|
|
// 查询注册的服务
|
|
|
builder.
|
|
|
@@ -183,5 +223,5 @@ func BuildGateway(gw *gateway.Gateway, opts ...Option) {
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
"services": serviceShortNames,
|
|
|
})
|
|
|
- })
|
|
|
+ }, queryRegisteredServicesMiddlewares...)
|
|
|
}
|