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 ¶
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 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.
type RedirectRewriteOptions ¶
RedirectRewriteOptions configures how upstream redirect locations are rewritten back into the local proxy mount.