rp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 7 Imported by: 1

README

Reverse Proxy

The reverse proxy package provides a wrapper around httputil.ReverseProxy, adding the ability to create flexible, rule based routing

Usage

Import reverse-proxy

go get github.com/dbut2/reverse-proxy

In your Go code, add

import "github.com/dbut2/reverse-proxy"

Creating a reverse proxy with selectors

proxy := rp.New(
    rp.Select("https://myapi.com", rp.PathIsAt("/api")),
    rp.Select("https://example.com", rp.Always()),
)

Using selector options

proxy := rp.New(
    rp.Select("https://private-cloud-run-instance.com", rp.IPMatches("12.34.56.78"), rp.WithOIDC()),
    rp.Select("https://example.com", rp.Always()),
)

Listen and serve

proxy := rp.New(
    rp.Select("https://myapi.com", rp.PathIsAt("/api")),
    rp.Select("https://example.com", rp.Always()),
)

http.ListenAndServe(":8080", proxy)
Example
package main

import (
	"net/http"

	"github.com/dbut2/reverse-proxy"
)

func main() {
	proxy := rp.New(
		rp.Select("https://myapi.com", rp.PathIsAt("/api")),
		rp.Select("https://example.com", rp.Always()),
	)

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(selectors ...*Selector) *httputil.ReverseProxy

Types

type Rule

type Rule func(in *http.Request, out *http.Request) bool

Rule is a boolean function if a match condition is found in request is for matching out request is for modifying outgoing request, as is in PathIsAt

func AllOf

func AllOf(rules ...Rule) Rule

AllOf matches if all rules match

func Always

func Always() Rule

Always always matches

func AnyOf

func AnyOf(rules ...Rule) Rule

AnyOf matches if any rules match

func HasHeader

func HasHeader(header string) Rule

HasHeader matches if header key exists in request

func HasQueryParam

func HasQueryParam(param string) Rule

HasQueryParam matches if query param key exists in request

func HeaderContains

func HeaderContains(header string, value string) Rule

HeaderContains matches if header kv pair exists in request

func HostMatches

func HostMatches(host string) Rule

HostMatches matches on the request host

func HostPathIsAt

func HostPathIsAt(hostpath string) Rule

HostPathIsAt matches on HostMatches and PathIsAt

func IPMatches

func IPMatches(clientIP string) Rule

IPMatches matches on the client IP from the X-Forwarded-For header

func MethodMatches

func MethodMatches(method string) Rule

MethodMatches matches on the request method

func PathIsAt

func PathIsAt(path string) Rule

PathIsAt matches if request path is prepended by given path also trims that path from outgoing request

func QueryParamContains

func QueryParamContains(param string, value string) Rule

QueryParamContains matches if query param kv pair exists in request

type SelectOption

type SelectOption func(*Selector)

SelectOption modifies the selector

func WithOIDC

func WithOIDC() SelectOption

WithOIDC sets the authorization header using an OIDC token generated for the service

type Selector

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

Selector contains information for selecting and modifying requests

func Select

func Select(address string, when Rule, opts ...SelectOption) *Selector

Select returns a selector to the address for matching on when rule

Jump to

Keyboard shortcuts

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