Replace proxy options

This commit is contained in:
Asim Aslam 2019-12-16 14:55:47 +00:00
parent a1ddfa827e
commit 03700ae6c0
5 changed files with 88 additions and 95 deletions

View File

@ -9,7 +9,6 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/grpc" "github.com/micro/go-micro/client/grpc"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/proxy" "github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
) )
@ -19,7 +18,7 @@ import (
// If the service matches the Name it will use the server.DefaultRouter. // If the service matches the Name it will use the server.DefaultRouter.
type Proxy struct { type Proxy struct {
// The proxy options // The proxy options
options.Options options proxy.Options
// Endpoint specified the fixed endpoint to call. // Endpoint specified the fixed endpoint to call.
Endpoint string 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 // 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 := new(Proxy)
p.Options = options.NewOptions(opts...) p.Endpoint = options.Endpoint
p.Options.Init(options.WithString("grpc")) p.Client = options.Client
// 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)
}
return p return p
} }

View File

@ -10,7 +10,6 @@ import (
"net/url" "net/url"
"path" "path"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/errors" "github.com/micro/go-micro/errors"
"github.com/micro/go-micro/proxy" "github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/server" "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 // Proxy will proxy rpc requests as http POST requests. It is a server.Proxy
type Proxy struct { type Proxy struct {
options.Options options proxy.Options
// The http backend to call // The http backend to call
Endpoint string Endpoint string
@ -197,16 +196,15 @@ func NewSingleHostProxy(url string) proxy.Proxy {
} }
// NewProxy returns a new proxy which will route using a http client // NewProxy returns a new proxy which will route using a http client
func NewProxy(opts ...options.Option) proxy.Proxy { func NewProxy(opts ...proxy.Option) proxy.Proxy {
p := new(Proxy) var options proxy.Options
p.Options = options.NewOptions(opts...) for _, o := range opts {
p.Options.Init(options.WithString("http")) o(&options)
// get endpoint
ep, ok := p.Options.Values().Get("proxy.endpoint")
if ok {
p.Endpoint = ep.(string)
} }
p := new(Proxy)
p.Endpoint = options.Endpoint
p.options = options
return p return p
} }

View File

@ -14,7 +14,6 @@ import (
"github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes" "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/errors"
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/proxy" "github.com/micro/go-micro/proxy"
@ -27,7 +26,7 @@ import (
// If no endpoint is specified it will call a service using the client. // If no endpoint is specified it will call a service using the client.
type Proxy struct { type Proxy struct {
// embed options // embed options
options.Options options proxy.Options
// Endpoint specifies the fixed service endpoint to call. // Endpoint specifies the fixed service endpoint to call.
Endpoint string 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 // NewSingleHostProxy returns a proxy which sends requests to a single backend
func NewSingleHostProxy(endpoint string) *Proxy { func NewSingleHostProxy(endpoint string) *Proxy {
return &Proxy{ return &Proxy{
Options: options.NewOptions(),
Endpoint: endpoint, Endpoint: endpoint,
} }
} }
// NewProxy returns a new proxy which will route based on mucp headers // 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 := new(Proxy)
p.Links = map[string]client.Client{} p.Links = map[string]client.Client{}
p.Options = options.NewOptions(opts...) p.Routes = make(map[string]map[uint64]router.Route)
p.Options.Init(options.WithString("mucp")) p.options = options
// get endpoint // get endpoint
ep, ok := p.Options.Values().Get("proxy.endpoint") p.Endpoint = options.Endpoint
if ok { // set the client
p.Endpoint = ep.(string) p.Client = options.Client
} // get router
p.Router = options.Router
// get client
c, ok := p.Options.Values().Get("proxy.client")
if ok {
p.Client = c.(client.Client)
}
// set the default client // set the default client
if p.Client == nil { if p.Client == nil {
p.Client = client.DefaultClient 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 // create default router and start it
if p.Router == nil { if p.Router == nil {
p.Router = router.DefaultRouter p.Router = router.DefaultRouter
} }
// set the links
// routes cache if options.Links != nil {
p.Routes = make(map[string]map[uint64]router.Route) // get client
p.Links = options.Links
}
go func() { go func() {
// continuously attempt to watch routes // continuously attempt to watch routes

51
proxy/options.go Normal file
View 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
}
}

View File

@ -4,15 +4,11 @@ package proxy
import ( import (
"context" "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" "github.com/micro/go-micro/server"
) )
// Proxy can be used as a proxy server for go-micro services // Proxy can be used as a proxy server for go-micro services
type Proxy interface { type Proxy interface {
options.Options
// ProcessMessage handles inbound messages // ProcessMessage handles inbound messages
ProcessMessage(context.Context, server.Message) error ProcessMessage(context.Context, server.Message) error
// ServeRequest handles inbound requests // ServeRequest handles inbound requests
@ -22,35 +18,3 @@ type Proxy interface {
var ( var (
DefaultEndpoint = "localhost:9090" 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
}
}