Documentation
¶
Index ¶
- Constants
- func DecompressGzip(data []byte) []byte
- type CaptureReader
- type Event
- type Option
- type Proxy
- type ReverseProxy
- func (rp *ReverseProxy) Close() error
- func (rp *ReverseProxy) Events() <-chan Event
- func (rp *ReverseProxy) ListenAndServe(ctx context.Context) error
- func (rp *ReverseProxy) Replay(ctx context.Context, method, path string, headers http.Header, body []byte) (Event, error)
- func (rp *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
Constants ¶
const MaxCaptureSize = 64 * 1024
MaxCaptureSize is the maximum number of bytes captured per request/response body.
Variables ¶
This section is empty.
Functions ¶
func DecompressGzip ¶
DecompressGzip decompresses gzip data. If data is not gzip-encoded (detected by the two-byte magic header 0x1f 0x8b), it is returned unchanged.
Types ¶
type CaptureReader ¶
type CaptureReader struct {
// contains filtered or unexported fields
}
CaptureReader wraps an io.Reader and stores the first maxSize bytes that pass through it. The underlying reader is read normally; the captured bytes are available via Bytes after reading is done.
func NewCaptureReader ¶
func NewCaptureReader(r io.Reader, maxSize int) *CaptureReader
NewCaptureReader returns a CaptureReader that stores up to maxSize bytes.
func (*CaptureReader) Bytes ¶
func (cr *CaptureReader) Bytes() []byte
Bytes returns the captured bytes so far.
type Event ¶
type Event struct {
ID string
Method string // HTTP method: GET, POST, PUT, DELETE, etc.
Path string // Request path including query string, e.g. /api/users?page=1
Status int32 // HTTP status code: 200, 404, 500, etc.
StartTime time.Time
Duration time.Duration
Error string // Non-empty when the upstream request failed.
RequestHeaders http.Header
ResponseHeaders http.Header
RequestBody []byte // Captured request body (up to MaxCaptureSize).
ResponseBody []byte // Captured response body (up to MaxCaptureSize).
}
Event represents a captured HTTP request/response pair.
type Option ¶
type Option func(*ReverseProxy)
Option configures the reverse proxy.
func WithTLSSkipVerify ¶
func WithTLSSkipVerify() Option
WithTLSSkipVerify disables TLS certificate verification for HTTPS upstreams.
type Proxy ¶
type Proxy interface {
// ListenAndServe accepts client connections and relays them to the upstream server.
ListenAndServe(ctx context.Context) error
// Events returns the channel of captured events.
Events() <-chan Event
// Replay sends a request to the upstream server and returns the resulting event.
Replay(ctx context.Context, method, path string, headers http.Header, body []byte) (Event, error)
// Close stops the proxy.
Close() error
}
Proxy is the interface for HTTP reverse proxies.
type ReverseProxy ¶
type ReverseProxy struct {
// contains filtered or unexported fields
}
ReverseProxy is an HTTP reverse proxy that captures request/response traffic.
func New ¶
func New(listenAddr, upstreamAddr string, opts ...Option) (*ReverseProxy, error)
New creates a new ReverseProxy. listenAddr is the address to listen on (e.g. ":8080"). upstreamAddr is the upstream server URL (e.g. "http://localhost:9000").
func (*ReverseProxy) Events ¶
func (rp *ReverseProxy) Events() <-chan Event
Events returns the channel of captured events.
func (*ReverseProxy) ListenAndServe ¶
func (rp *ReverseProxy) ListenAndServe(ctx context.Context) error
ListenAndServe starts the proxy and blocks until ctx is cancelled.
func (*ReverseProxy) Replay ¶
func (rp *ReverseProxy) Replay(ctx context.Context, method, path string, headers http.Header, body []byte) (Event, error)
Replay sends an HTTP request to the upstream server and returns the resulting event. The event is also published to the events channel.
func (*ReverseProxy) ServeHTTP ¶
func (rp *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles each proxied request.