headers

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2025 License: Apache-2.0 Imports: 1 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 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

Jump to

Keyboard shortcuts

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