Replace proxy options
This commit is contained in:
		| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										51
									
								
								proxy/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								proxy/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| 	} | ||||
| } | ||||
| @@ -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 | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user