rp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 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

Overview

Package rp provides a set of utilities to set up and modify behavior of reverse proxies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New creates a new reverse proxy instance configured with the provided selectors. The selectors determine which rules apply to which incoming requests.

Types

type Matcher added in v0.2.0

type Matcher func(r *http.Request) bool

Matcher is a function type that defines the criteria for whether a selector should be applied to a request.

type Modifier added in v0.2.0

type Modifier func(r *http.Request)

Modifier is a function type that describes how to alter an outgoing request before it's sent.

type Rule

type Rule struct {
	// Matcher determines if the rule is applicable to a given request. It must be defined for every rule.
	Matcher Matcher

	// Modifier is an optional function to alter the outgoing request when the Matcher's criteria is satisfied.
	Modifier Modifier
}

Rule represents the structure for request matching and modification. It consists of a Matcher, which determines if a rule is applicable for a request, and a Modifier, which optionally alters the outgoing request when the rule criteria is met.

func AllOf

func AllOf(rules ...Rule) Rule

AllOf creates a composite rule that matches only if all of the provided rules are satisfied. Modifiers of individual rules are applied in the order they are provided.

func Always

func Always() Rule

Always creates a rule that always matches any request.

func AnyOf

func AnyOf(rules ...Rule) Rule

AnyOf creates a composite rule that matches if any of the provided rules are satisfied. The modifier of the first matching rule is used.

func Group added in v0.2.0

func Group(parent Rule, children ...Rule) Rule

Group creates a composite rule that first checks if the parent rule is satisfied. If the parent is met, it then matches if any of the child rules are met.

func HasHeader

func HasHeader(header string) Rule

HasHeader creates a rule that matches if the specified header key is present in the request.

func HasQueryParam

func HasQueryParam(param string) Rule

HasQueryParam creates a rule that matches if the specified query parameter key exists in the request.

func HeaderContains

func HeaderContains(header string, value string) Rule

HeaderContains creates a rule that matches if the specified header key-value pair exists in the request.

func HostMatches

func HostMatches(host string) Rule

HostMatches creates a rule that matches a request based on its host.

func HostPathIsAt

func HostPathIsAt(hostpath string) Rule

HostPathIsAt creates a rule that matches a request based on both its host and path.

func IPMatches

func IPMatches(clientIP string) Rule

IPMatches creates a rule that matches a request based on the client IP derived from the X-Forwarded-For header.

func MethodMatches

func MethodMatches(method string) Rule

MethodMatches creates a rule that matches a request based on its HTTP method.

func PathIsAt

func PathIsAt(path string) Rule

PathIsAt creates a rule that matches if the request path starts with the specified path. When the rule is satisfied, it also trims the matched path from the outgoing request.

func QueryParamContains

func QueryParamContains(param string, value string) Rule

QueryParamContains creates a rule that matches if the specified query parameter key-value pair exists in the request.

type SelectOption

type SelectOption func(*Selector)

SelectOption defines a type for functions that customize a selector.

func WithOIDC

func WithOIDC() SelectOption

WithOIDC constructs a SelectOption that augments a selector to attach an OIDC token as the authorization header for the outgoing request, intended for the target service.

type Selector

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

Selector defines the criteria and actions for selecting and modifying requests in the reverse proxy. It contains a Matcher to decide if the Selector applies to an incoming request, a destination URL to which the request should be sent, and a list of Modifiers to apply to the outgoing request.

func Select

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

Select creates a new selector with the given address, rule, and optional selector modifications. The "when" rule determines when this selector should be applied to incoming requests.

func (*Selector) Modify added in v0.2.0

func (s *Selector) Modify(m Modifier)

Modify appends a new modifier to the selector's list of modifiers.

Jump to

Keyboard shortcuts

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