Fixup the proxy, strip down go.mod. Win bigly
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
@@ -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))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user