From a58bc8e75ce4f82a38040fb1cfb0fb926d2e826f Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 6 Jun 2019 17:55:32 +0100 Subject: [PATCH] add proxy interface and move router packages --- proxy/{router => }/grpc/grpc.go | 39 ++++++++++++++++++---------- proxy/{router => }/grpc/options.go | 0 proxy/{router => }/http/http.go | 0 proxy/{router => }/http/http_test.go | 0 proxy/{router => }/http/options.go | 0 proxy/{router => }/mucp/mucp.go | 0 proxy/{router => }/mucp/options.go | 0 proxy/proxy.go | 4 +-- 8 files changed, 27 insertions(+), 16 deletions(-) rename proxy/{router => }/grpc/grpc.go (85%) rename proxy/{router => }/grpc/options.go (100%) rename proxy/{router => }/http/http.go (100%) rename proxy/{router => }/http/http_test.go (100%) rename proxy/{router => }/http/options.go (100%) rename proxy/{router => }/mucp/mucp.go (100%) rename proxy/{router => }/mucp/options.go (100%) diff --git a/proxy/router/grpc/grpc.go b/proxy/grpc/grpc.go similarity index 85% rename from proxy/router/grpc/grpc.go rename to proxy/grpc/grpc.go index e209f3c3..77cf6457 100644 --- a/proxy/router/grpc/grpc.go +++ b/proxy/grpc/grpc.go @@ -10,14 +10,15 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/bytes" + "github.com/micro/go-micro/init" "github.com/micro/go-micro/server" "github.com/micro/go-micro/service/grpc" ) -// Router will transparently proxy requests to the backend. +// Proxy will transparently proxy requests to the backend. // If no backend is specified it will call a service using the client. -// If the service matches the Name it will use the server.DefaultRouter. -type Router struct { +// If the service matches the Name it will use the server.DefaultProxy. +type Proxy struct { // Name of the local service. In the event it's to be left alone Name string @@ -33,13 +34,16 @@ type Router struct { // The client to use for outbound requests Client client.Client + + // The proxy options + Options init.Options } var ( // The default name of this local service DefaultName = "go.micro.proxy" // The default router - DefaultRouter = &Router{} + DefaultProxy = &Proxy{} ) // read client request and write to server @@ -75,8 +79,8 @@ func readLoop(r server.Request, s client.Stream) error { } } -// ServeRequest honours the server.Router interface -func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error { +// ServeRequest honours the server.Proxy interface +func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error { // set the default name e.g local proxy if p.Name == "" { p.Name = DefaultName @@ -90,7 +94,7 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve // check service route if req.Service() == p.Name { // use the default router - return server.DefaultRouter.ServeRequest(ctx, req, rsp) + return server.DefaultProxy.ServeRequest(ctx, req, rsp) } opts := []client.CallOption{} @@ -165,7 +169,14 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve return nil } -// NewSingleHostRouter returns a router which sends requests to a single backend +// NewProxy returns a new grpc proxy server +func NewProxy(opts ...init.Option) *Proxy { + return &Proxy{ + Options: init.NewOptions(opts...), + } +} + +// NewSingleHostProxy returns a router which sends requests to a single backend // // It is used by setting it in a new micro service to act as a proxy for a backend. // @@ -173,19 +184,19 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve // // Create a new router to the http backend // -// r := NewSingleHostRouter("localhost:10001") +// r := NewSingleHostProxy("localhost:10001") // // // Create your new service // service := micro.NewService( // micro.Name("greeter"), // // Set the router -// http.WithRouter(r), +// http.WithProxy(r), // ) // // // Run the service // service.Run() -func NewSingleHostRouter(url string) *Router { - return &Router{ +func NewSingleHostProxy(url string) *Proxy { + return &Proxy{ Backend: url, } } @@ -216,13 +227,13 @@ func NewSingleHostRouter(url string) *Router { // ) // func NewService(opts ...micro.Option) micro.Service { - router := DefaultRouter + router := DefaultProxy name := DefaultName // prepend router to opts opts = append([]micro.Option{ micro.Name(name), - WithRouter(router), + WithProxy(router), }, opts...) // create the new service diff --git a/proxy/router/grpc/options.go b/proxy/grpc/options.go similarity index 100% rename from proxy/router/grpc/options.go rename to proxy/grpc/options.go diff --git a/proxy/router/http/http.go b/proxy/http/http.go similarity index 100% rename from proxy/router/http/http.go rename to proxy/http/http.go diff --git a/proxy/router/http/http_test.go b/proxy/http/http_test.go similarity index 100% rename from proxy/router/http/http_test.go rename to proxy/http/http_test.go diff --git a/proxy/router/http/options.go b/proxy/http/options.go similarity index 100% rename from proxy/router/http/options.go rename to proxy/http/options.go diff --git a/proxy/router/mucp/mucp.go b/proxy/mucp/mucp.go similarity index 100% rename from proxy/router/mucp/mucp.go rename to proxy/mucp/mucp.go diff --git a/proxy/router/mucp/options.go b/proxy/mucp/options.go similarity index 100% rename from proxy/router/mucp/options.go rename to proxy/mucp/options.go diff --git a/proxy/proxy.go b/proxy/proxy.go index ff03a081..a99041ba 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -4,13 +4,13 @@ package proxy import ( "context" - "github.com/micro/go-micro/init" + "github.com/micro/go-micro/options" "github.com/micro/go-micro/server" ) // Proxy can be used as a proxy server for go-micro services type Proxy interface { - init.Options + options.Options // ServeRequest will serve a request ServeRequest(context.Context, Request, Response) error // run the proxy