From ec9c73905bedc801e362bba500894da7a07225ab Mon Sep 17 00:00:00 2001 From: kaokyi Date: Fri, 22 Jun 2018 17:02:23 +0800 Subject: [PATCH] remove option --- examples/gateway/main.go | 3 +- examples/server/main.go | 2 +- gateway/gateway.go | 16 ++-- gateway/invoker.go | 20 +++-- gateway/table.go | 2 +- named/mode.go | 2 +- option/option.go | 159 --------------------------------------- 7 files changed, 27 insertions(+), 177 deletions(-) delete mode 100644 option/option.go diff --git a/examples/gateway/main.go b/examples/gateway/main.go index 27dee13..7f644b9 100644 --- a/examples/gateway/main.go +++ b/examples/gateway/main.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "github.com/hprose/hprose-golang/rpc" + "github.com/vlorc/hprose-gateway-core/option" "github.com/vlorc/hprose-gateway-core/source" _ "github.com/vlorc/hprose-gateway-plugins/session" _ "github.com/vlorc/hprose-gateway-protocol/forward" @@ -12,7 +13,7 @@ import ( _ "github.com/vlorc/hprose-gateway-protocol/restful" "github.com/vlorc/hprose-gateway/gateway" "github.com/vlorc/hprose-gateway/named" - "github.com/vlorc/hprose-gateway/option" + "net/http" ) diff --git a/examples/server/main.go b/examples/server/main.go index ed78742..8623b51 100644 --- a/examples/server/main.go +++ b/examples/server/main.go @@ -5,7 +5,7 @@ import ( "flag" "github.com/hprose/hprose-golang/rpc" "github.com/vlorc/hprose-gateway-core/etcd" - "github.com/vlorc/hprose-gateway-core/types" + "github.com/vlorc/hprose-gateway-types" "log" "net" ) diff --git a/gateway/gateway.go b/gateway/gateway.go index c2fa10c..40b1e5f 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -3,8 +3,9 @@ package gateway import ( "github.com/hprose/hprose-golang/rpc" "github.com/vlorc/hprose-gateway-core/invoker" - "github.com/vlorc/hprose-gateway/option" + "github.com/vlorc/hprose-gateway-core/option" "reflect" + "sort" ) type HproseGateway struct { @@ -12,12 +13,15 @@ type HproseGateway struct { } func NewGateway(o ...func(*option.GatewayOption)) *HproseGateway { - opt := &option.GatewayOption{} + opt := option.NewDefault() opt.Plugins = invoker.NewInvoker(NewInvoker(opt)) - for _, v := range o { - v(opt) - } - go opt.Resolver.Watch("*", opt.Water) + opt = option.NewOptionWith(opt, o...) + sort.Sort(opt.Plugins) + go opt.Resolver.Watch(opt.Prefix, opt.Water) + return NewGatewayWith(opt) +} + +func NewGatewayWith(opt *option.GatewayOption) *HproseGateway { return &HproseGateway{opt: opt} } diff --git a/gateway/invoker.go b/gateway/invoker.go index 338987c..0356257 100644 --- a/gateway/invoker.go +++ b/gateway/invoker.go @@ -2,8 +2,8 @@ package gateway import ( "context" - "github.com/vlorc/hprose-gateway-core/types" - "github.com/vlorc/hprose-gateway/option" + "github.com/vlorc/hprose-gateway-core/option" + "github.com/vlorc/hprose-gateway-types" "go.uber.org/zap" "reflect" ) @@ -14,20 +14,24 @@ func NewInvoker(opt *option.GatewayOption) types.Plugin { return (*Invoker)(opt) } -func (g *Invoker) Close() error { +func (i *Invoker) Level() int { + return 0 +} + +func (i *Invoker) Close() error { return nil } -func (g *Invoker) Name() string { +func (i *Invoker) Name() string { return "Invoker" } -func (g *Invoker) Handler(_ types.InvokeHandler, ctx context.Context, method string, args []reflect.Value) ([]reflect.Value, error) { +func (i *Invoker) Handler(_ types.InvokeHandler, ctx context.Context, method string, args []reflect.Value) ([]reflect.Value, error) { app_id, _ := ctx.Value("appid").(string) - client_id := g.Router.Resolver(method, app_id).Next(g.Context, method, args) - result, err := g.Manager.Resolver(client_id).Endpoint().Invoke(g.Context, method, args) + client_id := i.Router.Resolver(method, app_id).Next(i.Context, method, args) + result, err := i.Manager.Resolver(client_id).Endpoint().Invoke(i.Context, method, args) - g.Log().Debug("Proxy", + i.Log().Debug("Proxy", zap.String("path", method), zap.String("app_id", app_id), zap.String("client_id", client_id), diff --git a/gateway/table.go b/gateway/table.go index 7caad93..a4afdb8 100644 --- a/gateway/table.go +++ b/gateway/table.go @@ -3,7 +3,7 @@ package gateway import ( "bytes" "fmt" - "github.com/vlorc/hprose-gateway-core/types" + "github.com/vlorc/hprose-gateway-types" "io" "reflect" "strconv" diff --git a/named/mode.go b/named/mode.go index 5bd2bd2..358879a 100644 --- a/named/mode.go +++ b/named/mode.go @@ -1,7 +1,7 @@ package named import ( - "github.com/vlorc/hprose-gateway-core/types" + "github.com/vlorc/hprose-gateway-types" "strings" ) diff --git a/option/option.go b/option/option.go deleted file mode 100644 index 5f662de..0000000 --- a/option/option.go +++ /dev/null @@ -1,159 +0,0 @@ -package option - -import ( - "context" - "github.com/vlorc/hprose-gateway-core/etcd" - "github.com/vlorc/hprose-gateway-core/invoker" - "github.com/vlorc/hprose-gateway-core/plugin" - "github.com/vlorc/hprose-gateway-core/router" - "github.com/vlorc/hprose-gateway-core/source" - "github.com/vlorc/hprose-gateway-core/types" - "github.com/vlorc/hprose-gateway-core/water" - "go.uber.org/zap" -) - -type GatewayOption struct { - Router types.NamedRouter - Context context.Context - Resolver types.NamedResolver - Water types.NamedWatcher - Manager types.SourceManger - Named types.NamedMode - Balancer string - Error error - Log func() *zap.Logger - Plugins invoker.Invoker -} - -func Resolver(resolver types.NamedResolver) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Resolver = resolver - } -} - -func Manager(manager types.SourceManger) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Manager = manager - } -} - -func EtcdResolver(url, prefix string) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Resolver = etcd.NewEtcdResolver(etcd.NewLazyClient(etcd.NewClient(url)), opt.Context, prefix) - } -} - -func WithValue(key string, val interface{}) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Context = context.WithValue(opt.Context, key, val) - } -} - -func Context(ctx context.Context, env ...interface{}) func(*GatewayOption) { - return func(opt *GatewayOption) { - ctx = context.WithValue(ctx, "option", opt) - for i, l := 0, len(env)/2; i < l; i++ { - ctx = context.WithValue(ctx, env[i*2+0], env[i*2+1]) - } - opt.Context = ctx - } -} - -func Error(err error) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Error = err - } -} - -func Logger(log func() *zap.Logger) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Log = log - } -} - -func LoggerAuto(debug bool) func(*GatewayOption) { - return func(opt *GatewayOption) { - var log *zap.Logger - var err error - if debug { - log, err = zap.NewDevelopment() - } else { - log, err = zap.NewProduction() - } - if nil != err { - panic(err) - } - opt.Log = func() *zap.Logger { - return log - } - } -} - -func Balancer(name string) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Balancer = name - } -} - -func Named(mode types.NamedMode) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Named = mode - } -} - -func RouterAuto() func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Router = router.NewNamedRouter(opt.Balancer, opt.Manager, opt.Named) - } -} - -func WaterAuto(out ...types.NamedWatcher) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Water = water.NewSnapshotWater( - water.NewChannelWater(100), - opt.Router, - opt.Manager, - source.NewErrorSource(opt.Error), - out...) - } -} - -func Water(water types.NamedWatcher) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Water = water - } -} - -func Router(router types.NamedRouter) func(*GatewayOption) { - return func(opt *GatewayOption) { - opt.Router = router - } -} - -func pluginAppend(opt *GatewayOption, info types.Describe) { - factory := plugin.Query(info.Name) - opt.Log().Debug("Plugin", zap.String("name", info.Name), zap.Bool("query", nil != factory)) - if nil == factory { - return - } - bean := factory.Instance(opt.Context, info.Param) - opt.Log().Debug("Plugin", zap.String("name", info.Name), zap.Bool("instance", nil != bean)) - if nil == bean { - return - } - opt.Plugins = append(opt.Plugins, bean) -} - -func Plugin(name string, param map[string]string) func(*GatewayOption) { - return func(opt *GatewayOption) { - pluginAppend(opt, types.Describe{Name: name, Param: param}) - } -} - -func Plugins(args ...types.Describe) func(*GatewayOption) { - return func(opt *GatewayOption) { - for i := range args { - pluginAppend(opt, args[i]) - } - } -}