reverseproxy

package module
v0.0.0-...-b1b0948 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 12 Imported by: 2

README

Go Reverse Proxy

This project is a lightweight and efficient reverse proxy module written in Go. Besides directly forwarding incoming requests to the specified remote, it allows for rewriting of the request path and defining custom headers.

Features

  • Easy configuration via a simple Go API.
  • Fine-grained control over which paths are passed through to the backend.
  • Support for rewriting of the request path.
  • Customizable request and response headers.
  • Integrated health check and load measurement functionality.

Installation

To install go-reverse-proxy, use the following command:

go get -u github.com/secondtruth/go-reverse-proxy

Usage

package main

import (
	"crypto/tls"
	"log"
	"net/http"

	reverseproxy "github.com/secondtruth/go-reverse-proxy"
)

func main() {
	rp, err := reverseproxy.New("http://my-backend:8000")
	if err != nil {
		log.Fatal(err)
	}
	rp.RequestHeader = http.Header{
		"Authorization": []string{"Bearer abc123"},
	}
	rp.Transport = &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	rp.PassPath("*", "/")
	rp.PassPaths("HEAD|GET|POST", "/api/version", "/api/posts")
	rp.RewritePath("HEAD|GET|POST", "/posts", "/api/posts")

	log.Fatal(http.ListenAndServe(":8080", rp))
}

Documentation

Overview

Package reverseproxy provides a reverse proxy implementation in Go. It allows you to create a reverse proxy server that forwards HTTP requests to a remote server. The package includes features like response modification, error handling, and request rewriting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpErrorHandler

type HttpErrorHandler func(http.ResponseWriter, *http.Request, error)

HttpErrorHandler is a function that handles errors occurring in HTTP request handlers.

type ResponseModifier

type ResponseModifier func(*http.Response) error

ResponseModifier is a function that modifies the HTTP response.

type ResponseModifierMap

type ResponseModifierMap map[string]map[string]ResponseModifier

ResponseModifierMap is a map of [method][path]ResponseModifier.

type ReverseProxyMux

type ReverseProxyMux struct {
	Transport               http.RoundTripper
	RequestHeader           http.Header
	ModifyResponse          ResponseModifier
	ErrorHandler            HttpErrorHandler
	NotFoundHandler         http.Handler
	MethodNotAllowedHandler http.Handler
	// contains filtered or unexported fields
}

ReverseProxyMux is a reverse proxy with a request path multiplexer.

func New

func New(remote string) (*ReverseProxyMux, error)

New creates a new ReverseProxyMux with the specified remote URL.

func (*ReverseProxyMux) GetLoad

func (p *ReverseProxyMux) GetLoad() int32

GetLoad returns the number of requests being served by the proxy at the moment

func (*ReverseProxyMux) HandlePath

func (pm *ReverseProxyMux) HandlePath(route Route) *ReverseProxyMux

HandlePath registers a route.

func (*ReverseProxyMux) IsAvailable

func (p *ReverseProxyMux) IsAvailable() bool

IsAvailable returns whether the proxy origin was successfully connected at the last check time.

func (*ReverseProxyMux) PassAnyPath

func (pm *ReverseProxyMux) PassAnyPath(methods string) *ReverseProxyMux

PassAnyPath registers all possible paths with the specified HTTP methods.

func (*ReverseProxyMux) PassAnyPathUnder

func (pm *ReverseProxyMux) PassAnyPathUnder(methods string, paths ...string) *ReverseProxyMux

PassAnyPathUnder registers all possible paths under the specified parent paths with the specified HTTP methods.

func (*ReverseProxyMux) PassPath

func (pm *ReverseProxyMux) PassPath(methods, path string) *ReverseProxyMux

PassPath registers a path with the specified HTTP methods.

func (*ReverseProxyMux) PassPaths

func (pm *ReverseProxyMux) PassPaths(methods string, paths ...string) *ReverseProxyMux

PassPaths registers multiple paths with the specified HTTP methods.

func (*ReverseProxyMux) RewritePath

func (pm *ReverseProxyMux) RewritePath(methods, sourcePath, targetPath string) *ReverseProxyMux

RewritePath registers a route with the specified HTTP methods and source path, and rewrites the request path to the target path.

func (*ReverseProxyMux) ServeHTTP

func (pm *ReverseProxyMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the HTTP request.

func (*ReverseProxyMux) SetHealthCheckFunc

func (p *ReverseProxyMux) SetHealthCheckFunc(check func(addr *url.URL) bool, period time.Duration)

SetHealthCheckFunc sets the passed check func as the algorithm of checking the origin availability

type Route

type Route struct {
	Method         []string
	Path           string
	RewritePath    string
	RequestHeader  http.Header
	ModifyResponse ResponseModifier
}

func NewRoute

func NewRoute(methods, path string) Route

func (Route) SetModifyResponse

func (r Route) SetModifyResponse(modifier ResponseModifier) Route

func (Route) SetRequestHeader

func (r Route) SetRequestHeader(header http.Header) Route

func (Route) SetRewritePath

func (r Route) SetRewritePath(path string) Route

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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