Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HttpService ¶
type HttpService struct {
// URL is the external service URL. It may contain Caddy placeholders
// such as {host}, {uri}, {path}, {method}, etc.
URL string `json:"url,omitempty"`
// Method is the HTTP method used when calling the external service.
// Defaults to GET if empty.
Method string `json:"method,omitempty"`
// Headers are custom headers to include in the request to the external
// service.
Headers map[string]string `json:"headers,omitempty"`
// Body, when non-empty, provides a request body template. Typically
// used for POST and PUT requests.
Body string `json:"body,omitempty"`
// Timeout is the maximum duration for the request to the external
// service. Zero means no timeout.
Timeout caddy.Duration `json:"timeout,omitempty"`
// TLSSkipVerify controls whether the TLS certificate presented by the
// external service is verified. It should only be used in trusted
// development or internal environments.
TLSSkipVerify bool `json:"tls_skip_verify,omitempty"`
// CacheEnabled controls whether responses from the external service
// are cached. When true, responses are stored using the configured
// Caddy storage backend and served from cache on subsequent matching
// requests until the TTL expires. Default: true.
CacheEnabled bool `json:"cache_enabled,omitempty"`
// CacheTTL is the time-to-live for cache entries. After this duration,
// the cached entry is considered expired and a fresh request is made
// to the external service. A zero value means entries do not expire.
CacheTTL caddy.Duration `json:"cache_ttl,omitempty"`
// CacheStaleEnabled controls whether a stale (expired) cache entry is
// served when the external service request fails. When true, if a
// fresh HTTP call fails and a cache entry exists (even if expired),
// the stale data is served instead of returning an error. Default: true.
CacheStaleEnabled bool `json:"cache_stale_enabled,omitempty"`
// CacheKeyTemplate defines the template used to build the cache key.
// It supports Caddy placeholders. Default: "{method}:{url}".
CacheKeyTemplate string `json:"cache_key_template,omitempty"`
// Params are query parameters to append to the URL. Each key is the
// parameter name; each value is a Caddy placeholder template. Values
// are expanded and URL-encoded before being appended as query strings.
Params map[string]string `json:"params,omitempty"`
// contains filtered or unexported fields
}
HttpService implements an HTTP handler module that proxies requests to an external HTTP service. parses the JSON response, and injects each key as a {http_service.<key>} placeholder available to subsequent handlers in the middleware chain. It is registered as http.handlers.http_service.
func (HttpService) CaddyModule ¶
func (HttpService) CaddyModule() caddy.ModuleInfo
CaddyModule returns the Caddy module information.
func (*HttpService) Provision ¶
func (h *HttpService) Provision(ctx caddy.Context) error
Provision validates the configuration and prepares the HTTP client and cache storage backend.
func (HttpService) ServeHTTP ¶
func (h HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
ServeHTTP calls the configured external HTTP service, injects JSON response keys as {http_service.<key>} placeholders, and passes control to the next handler in the middleware chain.
When caching is enabled, the handler first checks the configured Caddy storage backend for a matching cache entry. On a cache hit, the stored JSON data is injected without making an outbound HTTP call. On a miss, the HTTP call is made and the result is cached.
If CacheStaleEnabled is true and the outbound call fails, a stale (expired) cache entry is served as a fallback.
func (*HttpService) UnmarshalCaddyfile ¶
func (h *HttpService) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
UnmarshalCaddyfile parses the http_service Caddyfile directive.