mucp

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package mucp transparently forwards the incoming request using a go-micro client.

Index

Constants

This section is empty.

Variables

View Source
var (
	// The default name of this local service
	DefaultName = "go.micro.proxy"
	// The default router
	DefaultRouter = &Router{}
)

Functions

func NewService

func NewService(opts ...micro.Option) micro.Service

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 WithBackend

func WithBackend(url string) micro.Option

WithBackend provides an option to set the proxy backend url

func WithRouter

func WithRouter(r server.Router) micro.Option

WithRouter provides an option to set the proxy router

Types

type Router

type Router 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

	// 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
}

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.

func NewSingleHostRouter

func NewSingleHostRouter(url string) *Router

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 (*Router) ServeRequest

func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error

ServeRequest honours the server.Router interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL