httprp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2016 License: MIT Imports: 4 Imported by: 0

README

package transport/httprp

package transport/httprp provides an HTTP reverse-proxy transport.

Rationale

HTTP server applications often associate multiple handlers with a single HTTP listener, each handler differentiated by the request URI and/or HTTP method. Handlers that perform business-logic in the app can implement the Endpoint interface and be exposed using the package transport/http server. Handlers that need to proxy the request to another HTTP endpoint can do so with this package by simply specifying the base URL to forward the request to.

Usage

The following example uses the Gorilla Mux router to illustrate how a mixture of proxying and non-proxying request handlers can be used with a single listener:

import (
	"net/http"
	"net/url"

	kithttp "github.com/go-kit/kit/transport/http"
	kithttprp "github.com/go-kit/kit/transport/httprp"
	"github.com/gorilla/mux"
	"golang.org/x/net/context"
)

func main() {
	router := mux.NewRouter()

	// server HTTP endpoint handled here
	router.Handle("/foo",
		kithttp.NewServer(
			context.Background(),
			func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil },
			func(*http.Request) (interface{}, error) { return struct{}{}, nil },
			func(http.ResponseWriter, interface{}) error { return nil },
		)).Methods("GET")

	// proxy endpoint, forwards requests to http://other.service.local/base/bar
	remoteServiceURL, _ := url.Parse("http://other.service.local/base")
	router.Handle("/bar",
		kithttprp.NewServer(
			context.Background(),
			remoteServiceURL,
		)).Methods("GET")

	http.ListenAndServe(":8080", router)
}

You can also supply a set of RequestFunc functions to be run before proxying the request. This can be useful for adding request headers required by the backend system (e.g. API tokens).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestFunc

type RequestFunc func(context.Context, *http.Request) context.Context

RequestFunc may take information from an HTTP request and put it into a request context. BeforeFuncs are executed prior to invoking the endpoint.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a proxying request handler.

func NewServer

func NewServer(
	ctx context.Context,
	baseURL *url.URL,
	options ...ServerOption,
) *Server

NewServer constructs a new server that implements http.Server and will proxy requests to the given base URL using its scheme, host, and base path. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir.

func (Server) ServeHTTP

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type ServerOption

type ServerOption func(*Server)

ServerOption sets an optional parameter for servers.

func ServerBefore

func ServerBefore(before ...RequestFunc) ServerOption

ServerBefore functions are executed on the HTTP request object before the request is decoded.

Jump to

Keyboard shortcuts

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