2019-06-06 09:50:25 +01:00
|
|
|
// Package proxy is a transparent proxy built on the go-micro/server
|
|
|
|
package proxy
|
|
|
|
|
2019-06-06 17:49:41 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-06-07 13:42:39 +01:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-06-12 12:45:42 +01:00
|
|
|
"github.com/micro/go-micro/config/options"
|
2019-08-05 17:44:33 +01:00
|
|
|
"github.com/micro/go-micro/router"
|
2019-06-06 17:49:41 +01:00
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Proxy can be used as a proxy server for go-micro services
|
|
|
|
type Proxy interface {
|
2019-06-06 17:55:32 +01:00
|
|
|
options.Options
|
2019-08-23 14:05:11 +01:00
|
|
|
// SendRequest honours the client.Router interface
|
|
|
|
SendRequest(context.Context, client.Request, client.Response) error
|
2019-06-07 13:42:39 +01:00
|
|
|
// ServeRequest honours the server.Router interface
|
2019-06-06 17:58:21 +01:00
|
|
|
ServeRequest(context.Context, server.Request, server.Response) error
|
2019-06-06 17:49:41 +01:00
|
|
|
}
|
2019-06-07 13:42:39 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-06-26 16:12:57 +01:00
|
|
|
|
|
|
|
// WithRouter specifies the router to use
|
|
|
|
func WithRouter(r router.Router) options.Option {
|
|
|
|
return options.WithValue("proxy.router", r)
|
|
|
|
}
|
2019-08-23 14:05:11 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|