2020-11-23 16:18:47 +03:00
|
|
|
// Package proxy is a transparent proxy built on the micro/server
|
2019-12-16 14:55:47 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2019-12-16 14:55:47 +00:00
|
|
|
)
|
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
// Options for proxy
|
2019-12-16 14:55:47 +00:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Client for communication
|
2019-12-16 14:55:47 +00:00
|
|
|
Client client.Client
|
2021-03-06 19:45:13 +03:00
|
|
|
// Router for routing
|
2019-12-16 14:55:47 +00:00
|
|
|
Router router.Router
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2021-03-06 19:45:13 +03:00
|
|
|
// Meter used for metrics
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter meter.Meter
|
2021-03-06 19:45:13 +03:00
|
|
|
// Links holds the communication links
|
|
|
|
Links map[string]client.Client
|
|
|
|
// Endpoint holds the destination address
|
|
|
|
Endpoint string
|
2019-12-16 14:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
// Option func signature
|
2019-12-16 14:55:47 +00:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewOptions returns new options struct that filled by opts
|
2021-01-22 23:32:33 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:55:47 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// WithLogger specifies the logger to use
|
|
|
|
func WithLogger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// WithMeter specifies the meter to use
|
|
|
|
func WithMeter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:55:47 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:32:33 +03:00
|
|
|
|
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|