gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/proxy/proxy.go (about)

     1  // Package proxy is a transparent proxy built on the go-micro/server
     2  package proxy
     3  
     4  import (
     5  	"context"
     6  
     7  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client"
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/config/options"
     9  	"gitee.com/liuxuezhan/go-micro-v1.18.0/router"
    10  	"gitee.com/liuxuezhan/go-micro-v1.18.0/server"
    11  )
    12  
    13  // Proxy can be used as a proxy server for go-micro services
    14  type Proxy interface {
    15  	options.Options
    16  	// ProcessMessage handles inbound messages
    17  	ProcessMessage(context.Context, server.Message) error
    18  	// ServeRequest handles inbound requests
    19  	ServeRequest(context.Context, server.Request, server.Response) error
    20  }
    21  
    22  var (
    23  	DefaultEndpoint = "localhost:9090"
    24  )
    25  
    26  // WithEndpoint sets a proxy endpoint
    27  func WithEndpoint(e string) options.Option {
    28  	return options.WithValue("proxy.endpoint", e)
    29  }
    30  
    31  // WithClient sets the client
    32  func WithClient(c client.Client) options.Option {
    33  	return options.WithValue("proxy.client", c)
    34  }
    35  
    36  // WithRouter specifies the router to use
    37  func WithRouter(r router.Router) options.Option {
    38  	return options.WithValue("proxy.router", r)
    39  }
    40  
    41  // WithLink sets a link for outbound requests
    42  func WithLink(name string, c client.Client) options.Option {
    43  	return func(o *options.Values) error {
    44  		var links map[string]client.Client
    45  		v, ok := o.Get("proxy.links")
    46  		if ok {
    47  			links = v.(map[string]client.Client)
    48  		} else {
    49  			links = map[string]client.Client{}
    50  		}
    51  		links[name] = c
    52  		// save the links
    53  		o.Set("proxy.links", links)
    54  		return nil
    55  	}
    56  }