http

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package http provides a vine rpc to http proxy

Package http provides a vine rpc to http proxy

Index

Constants

This section is empty.

Variables

View Source
var (
	// The default backend
	DefaultBackend = "http://localhost:9090"
	// The default router
	DefaultRouter = &Router{}
)

Functions

func NewProxy

func NewProxy(opts ...proxy.Option) proxy.Proxy

NewProxy returns a new proxy which will route using a http client

func NewService

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

NewService returns a new http proxy. It acts as a vine service and proxies to a http backend. Routes are dynamically set e.g Foo.Bar routes to /foo/bar. The default backend is http://localhost:9090. Optionally specify the backend endpoint url or the router. Also choose to register specific endpoints.

Usage:

svc := NewService(
	service.Name("greeter"),
	// Sets the default http endpoint
	http.WithBackend("http://localhost:10001"),
 )

Set fixed backend endpoints

// register an endpoint
http.RegisterEndpoint("Hello.World", "/helloworld")

svc := NewService(
	service.Name("greeter"),
	// Set the http endpoint
	http.WithBackend("http://localhost:10001"),
 )

func NewSingleHostProxy

func NewSingleHostProxy(url string) proxy.Proxy

NewSingleHostProxy returns a router which sends requests to a single http backend

func RegisterEndpoint

func RegisterEndpoint(rpcEp string, httpEp string) error

RegisterEndpoint registers a http endpoint against an RPC endpoint

RegisterEndpoint("Foo.Bar", "/foo/bar")
RegisterEndpoint("Greeter.Hello", "/helloworld")
RegisterEndpoint("Greeter.Hello", "http://localhost:8080/")

func WithBackend

func WithBackend(url string) vine.Option

WithBackend provides an option to set the http backend url

func WithRouter

func WithRouter(r server.Router) vine.Option

WithRouter provides an option to set the http router

Types

type Proxy

type Proxy struct {

	// The http backend to call
	Endpoint string
	// contains filtered or unexported fields
}

Proxy will proxy rpc requests as http POST requests. It is a server.Proxy

func (*Proxy) ProcessMessage

func (p *Proxy) ProcessMessage(ctx context.Context, msg server.Message) error

ProcessMessage handles incoming asynchronous messages

func (*Proxy) ServeRequest

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

ServeRequest honours the server.Router interface

func (*Proxy) String

func (p *Proxy) String() string

type Resolver

type Resolver struct{}

Resolver resolves rpc to http. It explicity maps Foo.Bar to /foo/bar

func (*Resolver) Resolve

func (r *Resolver) Resolve(ep string) string

Foo.Bar becomes /foo/bar

type Router

type Router struct {
	// Converts RPC Foo.Bar to /foo/bar
	Resolver *Resolver
	// The http backend to call
	Backend string
	// contains filtered or unexported fields
}

Router will proxy rpc requests as http POST requests. It is a server.Router

func NewSingleHostRouter

func NewSingleHostRouter(url string) *Router

NewSingleHostRouter returns a router which sends requests a single http backend

It is used by setting it in a new vine service to act as a proxy for a http backend.

Usage:

Create a new router to the http backend

r := NewSingleHostRouter("http://localhost:10001")

// Add additional routes
r.RegisterEndpoint("Hello.World", "/helloworld")

// Create your new service
svc := service.NewService(
	service.Name("greeter"),
	// Set the router
	http.WithRouter(r),
)

// Run the service
service.Run()

func (*Router) Endpoint

func (p *Router) Endpoint(rpcEp string) (string, error)

Endpoint returns the http endpoint for an rpc endpoint. Endpoint("Foo.Bar") returns http://localhost:9090/foo/bar

func (*Router) ProcessMessage

func (p *Router) ProcessMessage(ctx context.Context, msg server.Message) error

func (*Router) RegisterEndpoint

func (p *Router) RegisterEndpoint(rpcEp, httpEp string) error

RegisterEndpoint registers a http endpoint against an RPC endpoint. It converts relative paths into backend:endpoint. Anything prefixed with http:// or https:// will be left as is.

RegisterEndpoint("Foo.Bar", "/foo/bar")
RegisterEndpoint("Greeter.Hello", "/helloworld")
RegisterEndpoint("Greeter.Hello", "http://localhost:8080/")

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