headers

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(key, value string) httpserver.HandlerFunc

Add creates a middleware that adds a single header. This is useful for simple header additions.

func CORS

func CORS(allowOrigin, allowMethods, allowHeaders string) httpserver.HandlerFunc

CORS creates a middleware that sets CORS headers for cross-origin requests.

Parameters:

  • allowOrigin: Which origins can access the resource ("*" for any, or specific domain)
  • allowMethods: Comma-separated list of allowed HTTP methods
  • allowHeaders: Comma-separated list of allowed request headers

Examples:

// Allow any origin (useful for public APIs)
CORS("*", "GET,POST,PUT,DELETE", "Content-Type,Authorization")

// Allow specific origin with credentials
CORS("https://app.example.com", "GET,POST", "Content-Type,Authorization")

// Minimal read-only API
CORS("*", "GET,OPTIONS", "Content-Type")

// Development setup with all methods
CORS("http://localhost:3000", "GET,POST,PUT,PATCH,DELETE,OPTIONS", "*")

func JSON

func JSON() httpserver.HandlerFunc

JSON creates a middleware that sets JSON-specific headers. Sets Content-Type to application/json and Cache-Control to no-cache.

func New

func New(headers http.Header) httpserver.HandlerFunc

New creates a middleware that sets HTTP headers on responses. Headers are set before the request is processed, allowing other middleware and handlers to override them if needed.

Note: The Go standard library's http package will validate headers when writing them to prevent protocol violations. This middleware does not perform additional validation beyond what the standard library provides.

func NewWithOperations added in v0.0.13

func NewWithOperations(operations ...HeaderOperation) httpserver.HandlerFunc

NewWithOperations creates a middleware with full header control using functional options. Operations are executed in order: remove → set → add (for both request and response headers)

func Security

func Security() httpserver.HandlerFunc

Security creates a middleware that sets common security headers.

Types

type HeaderOperation added in v0.0.13

type HeaderOperation func(*headerOperations)

HeaderOperation represents a single header manipulation operation

func WithAdd added in v0.0.13

func WithAdd(headers http.Header) HeaderOperation

WithAdd creates an operation to add (append) headers

func WithAddHeader added in v0.0.13

func WithAddHeader(key, value string) HeaderOperation

WithAddHeader creates an operation to add a single header

func WithAddRequest added in v0.0.14

func WithAddRequest(headers http.Header) HeaderOperation

WithAddRequest creates an operation to add (append) request headers

func WithAddRequestHeader added in v0.0.14

func WithAddRequestHeader(key, value string) HeaderOperation

WithAddRequestHeader creates an operation to add a single request header

func WithRemove added in v0.0.13

func WithRemove(headerNames ...string) HeaderOperation

WithRemove creates an operation to remove headers

func WithRemoveRequest added in v0.0.14

func WithRemoveRequest(headerNames ...string) HeaderOperation

WithRemoveRequest creates an operation to remove request headers

func WithSet added in v0.0.13

func WithSet(headers http.Header) HeaderOperation

WithSet creates an operation to set (replace) headers

func WithSetHeader added in v0.0.13

func WithSetHeader(key, value string) HeaderOperation

WithSetHeader creates an operation to set a single header

func WithSetRequest added in v0.0.14

func WithSetRequest(headers http.Header) HeaderOperation

WithSetRequest creates an operation to set (replace) request headers

func WithSetRequestHeader added in v0.0.14

func WithSetRequestHeader(key, value string) HeaderOperation

WithSetRequestHeader creates an operation to set a single request header

Jump to

Keyboard shortcuts

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