From 03700ae6c0154651ddd9430ebe391c6234310bb3 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 16 Dec 2019 14:55:47 +0000 Subject: [PATCH] Replace proxy options --- proxy/grpc/grpc.go | 26 ++++++++--------------- proxy/http/http.go | 20 ++++++++---------- proxy/mucp/mucp.go | 50 +++++++++++++++++---------------------------- proxy/options.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ proxy/proxy.go | 36 -------------------------------- 5 files changed, 88 insertions(+), 95 deletions(-) create mode 100644 proxy/options.go diff --git a/proxy/grpc/grpc.go b/proxy/grpc/grpc.go index 571822ea..2a401c0c 100644 --- a/proxy/grpc/grpc.go +++ b/proxy/grpc/grpc.go @@ -9,7 +9,6 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/client/grpc" "github.com/micro/go-micro/codec" - "github.com/micro/go-micro/config/options" "github.com/micro/go-micro/proxy" "github.com/micro/go-micro/server" ) @@ -19,7 +18,7 @@ import ( // If the service matches the Name it will use the server.DefaultRouter. type Proxy struct { // The proxy options - options.Options + options proxy.Options // Endpoint specified the fixed endpoint to call. Endpoint string @@ -138,22 +137,15 @@ func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server } // NewProxy returns a new grpc proxy server -func NewProxy(opts ...options.Option) proxy.Proxy { +func NewProxy(opts ...proxy.Option) proxy.Proxy { + var options proxy.Options + for _, o := range opts { + o(&options) + } + p := new(Proxy) - p.Options = options.NewOptions(opts...) - p.Options.Init(options.WithString("grpc")) - - // get endpoint - ep, ok := p.Options.Values().Get("proxy.endpoint") - if ok { - p.Endpoint = ep.(string) - } - - // get client - c, ok := p.Options.Values().Get("proxy.client") - if ok { - p.Client = c.(client.Client) - } + p.Endpoint = options.Endpoint + p.Client = options.Client return p } diff --git a/proxy/http/http.go b/proxy/http/http.go index 64b4384b..9b8b9188 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -10,7 +10,6 @@ import ( "net/url" "path" - "github.com/micro/go-micro/config/options" "github.com/micro/go-micro/errors" "github.com/micro/go-micro/proxy" "github.com/micro/go-micro/server" @@ -18,7 +17,7 @@ import ( // Proxy will proxy rpc requests as http POST requests. It is a server.Proxy type Proxy struct { - options.Options + options proxy.Options // The http backend to call Endpoint string @@ -197,16 +196,15 @@ func NewSingleHostProxy(url string) proxy.Proxy { } // NewProxy returns a new proxy which will route using a http client -func NewProxy(opts ...options.Option) proxy.Proxy { - p := new(Proxy) - p.Options = options.NewOptions(opts...) - p.Options.Init(options.WithString("http")) - - // get endpoint - ep, ok := p.Options.Values().Get("proxy.endpoint") - if ok { - p.Endpoint = ep.(string) +func NewProxy(opts ...proxy.Option) proxy.Proxy { + var options proxy.Options + for _, o := range opts { + o(&options) } + p := new(Proxy) + p.Endpoint = options.Endpoint + p.options = options + return p } diff --git a/proxy/mucp/mucp.go b/proxy/mucp/mucp.go index d565d47a..98a3b7b6 100644 --- a/proxy/mucp/mucp.go +++ b/proxy/mucp/mucp.go @@ -14,7 +14,6 @@ import ( "github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/bytes" - "github.com/micro/go-micro/config/options" "github.com/micro/go-micro/errors" "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/proxy" @@ -27,7 +26,7 @@ import ( // If no endpoint is specified it will call a service using the client. type Proxy struct { // embed options - options.Options + options proxy.Options // Endpoint specifies the fixed service endpoint to call. Endpoint string @@ -525,54 +524,43 @@ func (p *Proxy) serveRequest(ctx context.Context, link client.Client, service, e // NewSingleHostProxy returns a proxy which sends requests to a single backend func NewSingleHostProxy(endpoint string) *Proxy { return &Proxy{ - Options: options.NewOptions(), Endpoint: endpoint, } } // NewProxy returns a new proxy which will route based on mucp headers -func NewProxy(opts ...options.Option) proxy.Proxy { +func NewProxy(opts ...proxy.Option) proxy.Proxy { + var options proxy.Options + for _, o := range opts { + o(&options) + } + p := new(Proxy) p.Links = map[string]client.Client{} - p.Options = options.NewOptions(opts...) - p.Options.Init(options.WithString("mucp")) + p.Routes = make(map[string]map[uint64]router.Route) + p.options = options // get endpoint - ep, ok := p.Options.Values().Get("proxy.endpoint") - if ok { - p.Endpoint = ep.(string) - } - - // get client - c, ok := p.Options.Values().Get("proxy.client") - if ok { - p.Client = c.(client.Client) - } + p.Endpoint = options.Endpoint + // set the client + p.Client = options.Client + // get router + p.Router = options.Router // set the default client if p.Client == nil { p.Client = client.DefaultClient } - // get client - links, ok := p.Options.Values().Get("proxy.links") - if ok { - p.Links = links.(map[string]client.Client) - } - - // get router - r, ok := p.Options.Values().Get("proxy.router") - if ok { - p.Router = r.(router.Router) - } - // create default router and start it if p.Router == nil { p.Router = router.DefaultRouter } - - // routes cache - p.Routes = make(map[string]map[uint64]router.Route) + // set the links + if options.Links != nil { + // get client + p.Links = options.Links + } go func() { // continuously attempt to watch routes diff --git a/proxy/options.go b/proxy/options.go new file mode 100644 index 00000000..20ca9c4d --- /dev/null +++ b/proxy/options.go @@ -0,0 +1,51 @@ +// Package proxy is a transparent proxy built on the go-micro/server +package proxy + +import ( + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/router" +) + +type Options struct { + // Specific endpoint to always call + Endpoint string + // The default client to use + Client client.Client + // The default router to use + Router router.Router + // Extra links for different clients + Links map[string]client.Client +} + +type Option func(o *Options) + +// WithEndpoint sets a proxy endpoint +func WithEndpoint(e string) Option { + return func(o *Options) { + o.Endpoint = e + } +} + +// WithClient sets the client +func WithClient(c client.Client) Option { + return func(o *Options) { + o.Client = c + } +} + +// WithRouter specifies the router to use +func WithRouter(r router.Router) Option { + return func(o *Options) { + o.Router = r + } +} + +// WithLink sets a link for outbound requests +func WithLink(name string, c client.Client) Option { + return func(o *Options) { + if o.Links == nil { + o.Links = make(map[string]client.Client) + } + o.Links[name] = c + } +} diff --git a/proxy/proxy.go b/proxy/proxy.go index e08926e9..7fabae76 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -4,15 +4,11 @@ package proxy import ( "context" - "github.com/micro/go-micro/client" - "github.com/micro/go-micro/config/options" - "github.com/micro/go-micro/router" "github.com/micro/go-micro/server" ) // Proxy can be used as a proxy server for go-micro services type Proxy interface { - options.Options // ProcessMessage handles inbound messages ProcessMessage(context.Context, server.Message) error // ServeRequest handles inbound requests @@ -22,35 +18,3 @@ type Proxy interface { var ( DefaultEndpoint = "localhost:9090" ) - -// WithEndpoint sets a proxy endpoint -func WithEndpoint(e string) options.Option { - return options.WithValue("proxy.endpoint", e) -} - -// WithClient sets the client -func WithClient(c client.Client) options.Option { - return options.WithValue("proxy.client", c) -} - -// WithRouter specifies the router to use -func WithRouter(r router.Router) options.Option { - return options.WithValue("proxy.router", r) -} - -// WithLink sets a link for outbound requests -func WithLink(name string, c client.Client) options.Option { - return func(o *options.Values) error { - var links map[string]client.Client - v, ok := o.Get("proxy.links") - if ok { - links = v.(map[string]client.Client) - } else { - links = map[string]client.Client{} - } - links[name] = c - // save the links - o.Set("proxy.links", links) - return nil - } -}