headers

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 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 HeaderMap) 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

func Security

func Security() httpserver.HandlerFunc

Security creates a middleware that sets common security headers.

Types

type HeaderMap

type HeaderMap map[string]string

HeaderMap represents a collection of HTTP headers

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 HeaderMap) 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 WithRemove added in v0.0.13

func WithRemove(headerNames ...string) HeaderOperation

WithRemove creates an operation to remove headers

func WithSet added in v0.0.13

func WithSet(headers HeaderMap) 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

Jump to

Keyboard shortcuts

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