header

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package header provides HTTP header management for Gin-based applications. It offers a convenient interface for setting, getting, and managing HTTP headers across multiple routes and handlers.

Key features:

  • Add/Set/Get/Del operations for headers
  • Middleware integration with Gin
  • Handler chain registration
  • Header cloning for reuse

Example usage:

headers := header.NewHeaders()
headers.Set("X-API-Version", "v1")
headers.Set("Cache-Control", "no-cache")
engine.GET("/api/data", headers.Register(dataHandler)...)

See also: github.com/nabbar/golib/router

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Headers

type Headers interface {
	// Add appends a value to the header.
	// If the header already exists, the value is added to the existing values.
	Add(key, value string)

	// Set replaces all values for the header with the given value.
	// Any existing values are discarded.
	Set(key, value string)

	// Get returns the first value for the given header key.
	// Returns empty string if the header doesn't exist.
	// Header names are case-insensitive.
	Get(key string) string

	// Del removes all values for the given header key.
	Del(key string)

	// Header returns a map of all headers with their first values.
	// Useful for inspecting or serializing header configuration.
	Header() map[string]string

	// Register creates a handler chain with the Header middleware first,
	// followed by the provided handlers. This ensures headers are set
	// before the route handlers execute.
	Register(router ...ginsdk.HandlerFunc) []ginsdk.HandlerFunc

	// Handler is the Gin middleware function that applies headers to the response.
	// It sets all configured headers on the Gin context.
	Handler(c *ginsdk.Context)

	// Clone creates a shallow copy of the Headers instance.
	// Note: The underlying header map is shared between instances.
	Clone() Headers
}

Headers manages HTTP headers for routes and handlers. It provides methods to manipulate headers and integrate them into Gin middleware chains.

All methods are safe for concurrent use when used with separate instances.

func NewHeaders

func NewHeaders() Headers

NewHeaders creates a new Headers instance with an empty header map.

Example:

headers := NewHeaders()
headers.Set("X-Custom-Header", "value")

type HeadersConfig

type HeadersConfig map[string]string

HeadersConfig is a map-based configuration for HTTP headers. It provides a simple way to define headers in configuration files and convert them to a Headers instance.

Example:

config := HeadersConfig{
    "X-API-Version": "v1",
    "Cache-Control": "no-cache",
    "X-Request-ID":  "12345",
}
headers := config.New()

func (HeadersConfig) New

func (h HeadersConfig) New() Headers

New creates a Headers instance from the configuration. Each key-value pair in the map is added as a header.

Returns a Headers instance with all configured headers.

Example:

config := HeadersConfig{"X-Custom": "value"}
headers := config.New()
engine.GET("/api", headers.Register(handler)...)

func (HeadersConfig) Validate

func (h HeadersConfig) Validate() liberr.Error

Validate checks if the configuration is valid. Currently, this always returns nil as all string key-value pairs are valid headers.

This method exists for consistency with other configuration types and may be extended in the future to validate header names or values.

Returns nil if validation succeeds, or an error if validation fails.

Jump to

Keyboard shortcuts

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