reverse

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package reverse provides a reusable HTTP reverse proxy handler with transport-level mutation and observation hooks.

The package owns target resolution, hop-by-hop header handling, request forwarding, and neutral observation callbacks. It does not own application routes, persistence, preview JSON schemas, or UI-facing DTOs.

Example
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "upstream %s", r.URL.Path)
}))
defer upstream.Close()

handler, err := New(Options{
	Resolver: QueryTargetResolver{MountPath: "/proxy"},
})
if err != nil {
	panic(err)
}

req := httptest.NewRequest(http.MethodGet, "/proxy/api/hello?_target="+url.QueryEscape(upstream.URL), nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)

fmt.Println(rec.Code)
fmt.Println(strings.TrimSpace(rec.Body.String()))
Output:
200
upstream /api/hello

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingTarget = errors.New("missing target")
	ErrInvalidTarget = errors.New("invalid target")
)

Functions

func RewriteRedirectLocation

func RewriteRedirectLocation(location string, upstream *url.URL, opts RedirectRewriteOptions) (string, error)

RewriteRedirectLocation rewrites an upstream redirect location so clients continue following redirects through the local proxy mount.

Example:

/next?x=1 -> /proxy/next?_target=https://example.com&x=1

Types

type ErrorWriter

type ErrorWriter func(http.ResponseWriter, *http.Request, int, error)

type Handler

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

func New

func New(opts Options) (*Handler, error)

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	Resolver                TargetResolver
	RoundTripper            http.RoundTripper
	GenerateSessionID       func() string
	ObserveTarget           func(*url.URL) bool
	WriteError              ErrorWriter
	MutateRequest           RequestMutator
	MutateResponse          ResponseMutator
	Hooks                   observe.Hooks
	PreserveHost            bool
	SampleRequestBodyBytes  int
	SampleResponseBodyBytes int
}

type QueryTargetResolver

type QueryTargetResolver struct {
	Param         string
	DefaultTarget string
	DropParams    []string
	MountPath     string
}

QueryTargetResolver resolves the upstream target from a query parameter or a configured default target, then merges path and query data from the incoming request.

func (QueryTargetResolver) Resolve

func (r QueryTargetResolver) Resolve(req *http.Request) (*url.URL, error)

type RedirectRewriteOptions

type RedirectRewriteOptions struct {
	MountPath   string
	TargetParam string
}

RedirectRewriteOptions configures how upstream redirect locations are rewritten back into the local proxy mount.

type RequestMutator

type RequestMutator func(context.Context, *http.Request) error

type ResolveTargetFunc

type ResolveTargetFunc func(*http.Request) (*url.URL, error)

func (ResolveTargetFunc) Resolve

func (f ResolveTargetFunc) Resolve(r *http.Request) (*url.URL, error)

type ResponseMutator

type ResponseMutator func(context.Context, *http.Request, *http.Response) error

type TargetResolver

type TargetResolver interface {
	Resolve(*http.Request) (*url.URL, error)
}

Jump to

Keyboard shortcuts

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