http

package
v1.5.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

HTTP Proxy

This is a http proxy plugin which converts RPC to HTTP request

Overview

NewService returns a new http proxy. It acts as a micro 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

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

// Set fixed backend endpoints
// register an endpoint
http.RegisterEndpoint("Hello.World", "/helloworld")

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

Documentation

Overview

Package http provides a micro 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 NewService

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

NewService returns a new http proxy. It acts as a micro 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:

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

Set fixed backend endpoints

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

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

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) micro.Option

WithBackend provides an option to set the http backend url

func WithRouter

func WithRouter(r server.Router) micro.Option

WithRouter provides an option to set the http router

Types

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 micro 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
service := micro.NewService(
	micro.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) 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