handler

package
v0.0.0-...-e308f56 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler provides a fluent API for creating cache-backed HTTP handlers.

Example usage:

h := handler.New(client, cache).
	CacheKey(func(r *http.Request) string {
		return "custom-key"
	}).
	Transform(func(r *http.Request) (*http.Request, error) {
		// Modify request before fetching
		return modifiedRequest, nil
	})

func New

func New(client *http.Client, c cache.Cache) *Handler

New creates a new Handler with the given HTTP client and cache. By default: - Cache key is derived from the request URL - No request transformation is performed - Standard error handling is used.

func (*Handler) CacheKey

func (h *Handler) CacheKey(f func(*http.Request) string) *Handler

CacheKey sets the function used to determine the cache key for a request. The function receives the original incoming request.

func (*Handler) OnError

func (h *Handler) OnError(f func(error, http.ResponseWriter, *http.Request)) *Handler

OnError sets a custom error handler for the built handler. If not set, a default error handler is used.

func (*Handler) ServeHTTP

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

ServeHTTP implements http.Handler. The handler will: 1. Determine the cache key using the configured function 2. Check if the content exists in cache 3. If cached, stream from cache 4. If not cached, transform the request and fetch from upstream 5. Cache the response while streaming to the client.

func (*Handler) TTL

func (h *Handler) TTL(f func(*http.Request) time.Duration) *Handler

TTL sets the function used to determine the cache TTL for a request. The function receives the original incoming request. If not set or returns 0, the cache's default/maximum TTL is used.

func (*Handler) Transform

func (h *Handler) Transform(f func(*http.Request) (*http.Request, error)) *Handler

Transform sets the function used to transform the incoming request before fetching. This is where you can modify the request URL, headers, etc. The function receives the original incoming request and should return the request that will be sent to the upstream server.

Jump to

Keyboard shortcuts

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