rp

package module
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 7 Imported by: 1

README

rp Go Reference build Coverage Code to Test Ratio Test Execution Time

rp is a reverse proxy package for multiple domains and multiple upstreams.

Usage

Prepare an instance that implements rp.Relayer interface.

And then, create a new http.Server using rp.NewServer or rp.NewTLSServer with the instance.

Use rp.NewServer ( rp.ListenAndServe ) if handling per-domain (or per-request, as the case may be) upstreams.

package main

import (
    "log"
    "net/http"

    "github.com/2manymws/rp"
)

func main() {
    var r rp.Relayer = newMyRelayer()
    log.Fatal(rp.ListenAndServe(":80", r))
}

Use rp.NewTLSServer ( rp.ListenAndServeTLS )if handling per-domain TLS termination as well as per-domain HTTP request routing.

package main

import (
    "log"
    "net/http"

    "github.com/2manymws/rp"
)

func main() {
    var r rp.Relayer = newMyRelayer()
    log.Fatal(rp.ListenAndServeTLS(":443", r))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, r Relayer) error

ListenAndServe listens on the TCP network address addr and then proxies requests using Relayer r.

func ListenAndServeTLS

func ListenAndServeTLS(addr string, r Relayer) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections.

func NewRouter

func NewRouter(r Relayer) http.Handler

NewRouter returns a new reverse proxy router.

func NewServer

func NewServer(addr string, r Relayer) *http.Server

NewServer returns a new reverse proxy server.

func NewTLSServer

func NewTLSServer(addr string, r Relayer) *http.Server

NewTLSServer returns a new reverse proxy TLS server.

Types

type CertGetter

type CertGetter interface {
	// GetCertificate returns the TLS certificate for the given client hello info.
	GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error)
}

type ErrorHandler

type ErrorHandler interface {
	// ErrorHandler handles the error returned by the upstream.
	// If this method is not implemented, the error will be returned to the client.
	ErrorHandler(http.ResponseWriter, *http.Request, error)
}

type Relayer

type Relayer interface {
	// GetUpstream returns the upstream URL for the given request.
	// If upstream is not determined, nil may be returned
	// DO NOT modify the request in this method.
	GetUpstream(*http.Request) (*url.URL, error)
}

Relayer is the interface of the implementation that determines the behavior of the reverse proxy

type Rewriter

type Rewriter interface {
	// Rewrite rewrites the request before sending it to the upstream.
	// For example, you can set `X-Forwarded-*` header here using [httputil.ProxyRequest.SetXForwarded](https://pkg.go.dev/net/http/httputil#ProxyRequest.SetXForwarded)
	Rewrite(*httputil.ProxyRequest) error
}

type RoundTipperOnErrorer

type RoundTipperOnErrorer interface {
	// RoundTripOnError performs the round trip of the request when the upstream returns an error.
	// If this method is not implemented, the request will be sent to the default transport error.
	RoundTripOnError(r *http.Request) (*http.Response, error)
}

type RoundTripper

type RoundTripper interface {
	// RoundTrip performs the round trip of the request.
	// It is necessary to implement the functions that http.Transport is responsible for (e.g. MaxIdleConnsPerHost).
	RoundTrip(r *http.Request) (*http.Response, error)
	RoundTripOnError(r *http.Request) (*http.Response, error)
}

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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