Fixup the proxy, strip down go.mod. Win bigly

This commit is contained in:
Asim Aslam
2019-06-07 13:42:39 +01:00
parent d65393a799
commit 9e23855c37
11 changed files with 116 additions and 555 deletions

View File

@@ -6,46 +6,29 @@ import (
"io"
"strings"
"github.com/micro/go-micro"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/grpc"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/server"
"github.com/micro/go-micro/service/grpc"
)
// 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 Proxy struct {
// Name of the local service. In the event it's to be left alone
Name string
// Backend is a single backend to route to
// If backend is of the form address:port it will call the address.
// Otherwise it will use it as the service name to call.
Backend string
// The proxy options
options.Options
// Endpoint specified the fixed endpoint to call.
// In the event you proxy to a fixed backend this lets you
// call a single endpoint
Endpoint string
// The client to use for outbound requests
Client client.Client
// The proxy options
Options options.Options
}
var (
// The default name of this local service
DefaultName = "go.micro.proxy"
// The default router
DefaultProxy = &Proxy{}
)
// read client request and write to server
func readLoop(r server.Request, s client.Stream) error {
// request to backend server
@@ -81,20 +64,9 @@ func readLoop(r server.Request, s client.Stream) 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
}
// set default client
if p.Client == nil {
p.Client = client.DefaultClient
}
// check service route
if req.Service() == p.Name {
// use the default router
return server.DefaultRouter.ServeRequest(ctx, req, rsp)
p.Client = grpc.NewClient()
}
opts := []client.CallOption{}
@@ -104,21 +76,16 @@ func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server
endpoint := req.Endpoint()
// call a specific backend
if len(p.Backend) > 0 {
if len(p.Endpoint) > 0 {
// address:port
if parts := strings.Split(p.Backend, ":"); len(parts) > 0 {
opts = append(opts, client.WithAddress(p.Backend))
if parts := strings.Split(p.Endpoint, ":"); len(parts) > 1 {
opts = append(opts, client.WithAddress(p.Endpoint))
// use as service name
} else {
service = p.Backend
service = p.Endpoint
}
}
// call a specific endpoint
if len(p.Endpoint) > 0 {
endpoint = p.Endpoint
}
// read initial request
body, err := req.Read()
if err != nil {
@@ -170,77 +137,29 @@ 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 {
return &Proxy{
Options: options.NewOptions(opts...),
func NewProxy(opts ...options.Option) proxy.Proxy {
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)
}
return p
}
// 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.
//
// Usage:
//
// Create a new router to the http backend
//
// r := NewSingleHostProxy("localhost:10001")
//
// // Create your new service
// service := micro.NewService(
// micro.Name("greeter"),
// // Set the router
// http.WithProxy(r),
// )
//
// // Run the service
// service.Run()
func NewSingleHostProxy(url string) *Proxy {
return &Proxy{
Backend: url,
Endpoint: url,
}
}
// NewService returns a new proxy. It acts as a micro service proxy.
// Any request on the transport is routed to via the client to a service.
// In the event a backend is specified then it routes to that backend.
// The name of the backend can be a local address:port or a service name.
//
// Usage:
//
// New micro proxy routes via micro client to any service
//
// proxy := NewService()
//
// OR with address:port routes to local service
//
// service := NewService(
// // Sets the default http endpoint
// proxy.WithBackend("localhost:10001"),
// )
//
// OR with service name routes to a fixed backend service
//
// service := NewService(
// // Sets the backend service
// proxy.WithBackend("greeter"),
// )
//
func NewService(opts ...micro.Option) micro.Service {
router := DefaultProxy
name := DefaultName
// prepend router to opts
opts = append([]micro.Option{
micro.Name(name),
WithRouter(router),
}, opts...)
// create the new service
service := grpc.NewService(opts...)
// set router name
router.Name = service.Server().Options().Name
return service
}

View File

@@ -1,32 +0,0 @@
package grpc
import (
"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
)
// WithBackend provides an option to set the proxy backend url
func WithBackend(url string) micro.Option {
return func(o *micro.Options) {
// get the router
r := o.Server.Options().Router
// not set
if r == nil {
r = DefaultProxy
o.Server.Init(server.WithRouter(r))
}
// check its a proxy router
if proxyRouter, ok := r.(*Proxy); ok {
proxyRouter.Backend = url
}
}
}
// WithRouter provides an option to set the proxy router
func WithRouter(r server.Router) micro.Option {
return func(o *micro.Options) {
o.Server.Init(server.WithRouter(r))
}
}

View File

@@ -10,27 +10,23 @@ import (
"net/url"
"path"
"github.com/micro/go-micro"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/server"
)
// Router will proxy rpc requests as http POST requests. It is a server.Router
type Router struct {
// Proxy will proxy rpc requests as http POST requests. It is a server.Proxy
type Proxy struct {
options.Options
// The http backend to call
Backend string
Endpoint string
// first request
first bool
}
var (
// The default backend
DefaultBackend = "http://localhost:9090"
// The default router
DefaultRouter = &Router{}
)
func getMethod(hdr map[string]string) string {
switch hdr["Micro-Method"] {
case "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH":
@@ -49,9 +45,9 @@ func getEndpoint(hdr map[string]string) string {
}
// ServeRequest honours the server.Router interface
func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
if p.Backend == "" {
p.Backend = DefaultBackend
func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
if p.Endpoint == "" {
p.Endpoint = proxy.DefaultEndpoint
}
for {
@@ -75,10 +71,10 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
// set the endpoint
if len(endpoint) == 0 {
endpoint = p.Backend
endpoint = p.Endpoint
} else {
// add endpoint to backend
u, err := url.Parse(p.Backend)
u, err := url.Parse(p.Endpoint)
if err != nil {
return errors.InternalServerError(req.Service(), err.Error())
}
@@ -130,48 +126,24 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
return nil
}
// NewSingleHostRouter returns a router which sends requests to a single http backend
//
// It is used by setting it in a new micro service to act as a proxy for a http backend.
//
// Usage:
//
// Create a new router to the http backend
//
// r := NewSingleHostRouter("http://localhost:10001")
//
// // Create your new service
// service := micro.NewService(
// micro.Name("greeter"),
// // Set the router
// http.WithRouter(r),
// )
//
// // Run the service
// service.Run()
func NewSingleHostRouter(url string) *Router {
return &Router{
Backend: url,
// NewSingleHostProxy returns a router which sends requests to a single http backend
func NewSingleHostProxy(url string) proxy.Proxy {
return &Proxy{
Endpoint: url,
}
}
// NewService returns a new http proxy. It acts as a micro service proxy.
// Any request on the transport is routed to a fixed http backend.
//
// Usage:
//
// service := NewService(
// micro.Name("greeter"),
// // Sets the default http endpoint
// http.WithBackend("http://localhost:10001"),
// )
//
func NewService(opts ...micro.Option) micro.Service {
// prepend router to opts
opts = append([]micro.Option{
WithRouter(DefaultRouter),
}, opts...)
// 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"))
// create the new service
return micro.NewService(opts...)
// get endpoint
ep, ok := p.Options.Values().Get("proxy.endpoint")
if ok {
p.Endpoint = ep.(string)
}
return p
}

View File

@@ -20,7 +20,7 @@ func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"hello": "world"}`))
}
func TestHTTPRouter(t *testing.T) {
func TestHTTPProxy(t *testing.T) {
c, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
@@ -47,7 +47,7 @@ func TestHTTPRouter(t *testing.T) {
http.Handle("/", new(testHandler))
// new proxy
p := NewSingleHostRouter(url)
p := NewSingleHostProxy(url)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -91,32 +91,3 @@ func TestHTTPRouter(t *testing.T) {
}
}
}
func TestHTTPRouterOptions(t *testing.T) {
// test endpoint
service := NewService(
WithBackend("http://foo.bar"),
)
r := service.Server().Options().Router
httpRouter, ok := r.(*Router)
if !ok {
t.Fatal("Expected http router to be installed")
}
if httpRouter.Backend != "http://foo.bar" {
t.Fatalf("Expected endpoint http://foo.bar got %v", httpRouter.Backend)
}
// test router
service = NewService(
WithRouter(&Router{Backend: "http://foo2.bar"}),
)
r = service.Server().Options().Router
httpRouter, ok = r.(*Router)
if !ok {
t.Fatal("Expected http router to be installed")
}
if httpRouter.Backend != "http://foo2.bar" {
t.Fatalf("Expected endpoint http://foo2.bar got %v", httpRouter.Backend)
}
}

View File

@@ -1,32 +0,0 @@
package http
import (
"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
)
// WithBackend provides an option to set the http backend url
func WithBackend(url string) micro.Option {
return func(o *micro.Options) {
// get the router
r := o.Server.Options().Router
// not set
if r == nil {
r = DefaultRouter
o.Server.Init(server.WithRouter(r))
}
// check its a http router
if httpRouter, ok := r.(*Router); ok {
httpRouter.Backend = url
}
}
}
// WithRouter provides an option to set the http router
func WithRouter(r server.Router) micro.Option {
return func(o *micro.Options) {
o.Server.Init(server.WithRouter(r))
}
}

View File

@@ -6,41 +6,27 @@ import (
"io"
"strings"
"github.com/micro/go-micro"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/server"
)
// Router 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 {
// Name of the local service. In the event it's to be left alone
Name string
// Proxy will transparently proxy requests to an endpoint.
// If no endpoint is specified it will call a service using the client.
type Proxy struct {
// embed options
options.Options
// Backend is a single backend to route to
// If backend is of the form address:port it will call the address.
// Otherwise it will use it as the service name to call.
Backend string
// Endpoint specified the fixed endpoint to call.
// In the event you proxy to a fixed backend this lets you
// call a single endpoint
// Endpoint specified the fixed service endpoint to call.
Endpoint string
// The client to use for outbound requests
Client client.Client
}
var (
// The default name of this local service
DefaultName = "go.micro.proxy"
// The default router
DefaultRouter = &Router{}
)
// read client request and write to server
func readLoop(r server.Request, s client.Stream) error {
// request to backend server
@@ -75,23 +61,12 @@ 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 {
// set the default name e.g local proxy
if p.Name == "" {
p.Name = DefaultName
}
func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
// set default client
if p.Client == nil {
p.Client = client.DefaultClient
}
// check service route
if req.Service() == p.Name {
// use the default router
return server.DefaultRouter.ServeRequest(ctx, req, rsp)
}
opts := []client.CallOption{}
// service name
@@ -99,21 +74,16 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
endpoint := req.Endpoint()
// call a specific backend
if len(p.Backend) > 0 {
if len(p.Endpoint) > 0 {
// address:port
if parts := strings.Split(p.Backend, ":"); len(parts) > 0 {
opts = append(opts, client.WithAddress(p.Backend))
if parts := strings.Split(p.Endpoint, ":"); len(parts) > 1 {
opts = append(opts, client.WithAddress(p.Endpoint))
// use as service name
} else {
service = p.Backend
service = p.Endpoint
}
}
// call a specific endpoint
if len(p.Endpoint) > 0 {
endpoint = p.Endpoint
}
// read initial request
body, err := req.Read()
if err != nil {
@@ -164,71 +134,31 @@ 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
//
// It is used by setting it in a new micro service to act as a proxy for a backend.
//
// Usage:
//
// Create a new router to the http backend
//
// r := NewSingleHostRouter("localhost:10001")
//
// // Create your new service
// service := micro.NewService(
// micro.Name("greeter"),
// // Set the router
// http.WithRouter(r),
// )
//
// // Run the service
// service.Run()
func NewSingleHostRouter(url string) *Router {
return &Router{
Backend: url,
// NewSingleHostProxy returns a proxy which sends requests to a single backend
func NewSingleHostProxy(endpoint string) *Proxy {
return &Proxy{
Options: options.NewOptions(),
Endpoint: endpoint,
}
}
// NewService returns a new proxy. It acts as a micro service proxy.
// Any request on the transport is routed to via the client to a service.
// In the event a backend is specified then it routes to that backend.
// The name of the backend can be a local address:port or a service name.
//
// Usage:
//
// New micro proxy routes via micro client to any service
//
// proxy := NewService()
//
// OR with address:port routes to local service
//
// service := NewService(
// // Sets the default http endpoint
// proxy.WithBackend("localhost:10001"),
// )
//
// OR with service name routes to a fixed backend service
//
// service := NewService(
// // Sets the backend service
// proxy.WithBackend("greeter"),
// )
//
func NewService(opts ...micro.Option) micro.Service {
router := DefaultRouter
name := DefaultName
// NewProxy returns a new proxy which will route based on mucp headers
func NewProxy(opts ...options.Option) proxy.Proxy {
p := new(Proxy)
p.Options = options.NewOptions(opts...)
p.Options.Init(options.WithString("mucp"))
// prepend router to opts
opts = append([]micro.Option{
micro.Name(name),
WithRouter(router),
}, opts...)
// get endpoint
ep, ok := p.Options.Values().Get("proxy.endpoint")
if ok {
p.Endpoint = ep.(string)
}
// create the new service
service := micro.NewService(opts...)
// get client
c, ok := p.Options.Values().Get("proxy.client")
if ok {
p.Client = c.(client.Client)
}
// set router name
router.Name = service.Server().Options().Name
return service
return p
}

View File

@@ -1,32 +0,0 @@
package mucp
import (
"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
)
// WithBackend provides an option to set the proxy backend url
func WithBackend(url string) micro.Option {
return func(o *micro.Options) {
// get the router
r := o.Server.Options().Router
// not set
if r == nil {
r = DefaultRouter
o.Server.Init(server.WithRouter(r))
}
// check its a proxy router
if proxyRouter, ok := r.(*Router); ok {
proxyRouter.Backend = url
}
}
}
// WithRouter provides an option to set the proxy router
func WithRouter(r server.Router) micro.Option {
return func(o *micro.Options) {
o.Server.Init(server.WithRouter(r))
}
}

View File

@@ -4,6 +4,7 @@ package proxy
import (
"context"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/server"
)
@@ -11,6 +12,20 @@ import (
// Proxy can be used as a proxy server for go-micro services
type Proxy interface {
options.Options
// ServeRequest will serve a request
// ServeRequest honours the server.Router interface
ServeRequest(context.Context, server.Request, server.Response) error
}
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)
}