addheader

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

Header Adding Middleware

The header adding middleware is used to add custom headers to responses. Note that headers are generally not required to be unique. So repetitions are allowed, and this middleware will not check for duplicates.

Example

The header adding should be configured with actual headers to add. It will work as a NoOp, if none are provided.

finalHandler := midgard.StackMiddlewareHandler(
    []midgard.Middleware{
        addheader.New(
            addheader.WithHeaders([][2]string{
                "X-Test": "TestHeaderValue",
            }),
        ),
    },
    http.HandlerFunc(HelloHandler),
)

Documentation

Overview

Package addheader provides a middleware for adding headers to HTTP responses.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilOption = errors.New("option cannot be nil")

ErrNilOption is returned when an option is nil.

Functions

func New

func New(options ...func(*Handler) error) (defs.Middleware, error)

New generates a new header adding middleware.

func WithHeaders

func WithHeaders(headers [][2]string) func(*Handler) error

WithHeaders configures the headers to add to responses.

func WithLogLevel

func WithLogLevel(level slog.Level) func(h *Handler) error

WithLogLevel configures the log level to use with the logger.

func WithLogger

func WithLogger(log *slog.Logger) func(h *Handler) error

WithLogger configures the logger to use.

Types

type Handler

type Handler struct {
	defs.MWBase
	// contains filtered or unexported fields
}

Handler holds the information of the added headers.

func (*Handler) GetMWBase

func (h *Handler) GetMWBase() *defs.MWBase

GetMWBase returns the MWBase instance of the handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the requests, adding the additionally provided headers to the responses.

Jump to

Keyboard shortcuts

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