webhandler

package
v0.0.0-...-ec082de Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Overview

Package webhandler provides handlers, middleware, and utilities for web applications. It simplifies common tasks, enhances request processing, and includes features like request logging, unique request IDs, and HTML template rendering.

Index

Constants

This section is empty.

Variables

View Source
var LoggerKey = LoggerKeyType{}

LoggerKey is the key to store and retrieve the logger from the context.

View Source
var ReqIDKey = ReqIDType{}

ReqIDKey is a key for storing and retrieving the request ID from the context.

Functions

func AddLogger

func AddLogger(next http.Handler) http.Handler

AddLogger wraps an HTTP handler to include a request-specific logger in its context.

func AddRequestID

func AddRequestID(next http.Handler) http.Handler

AddRequestID is middleware that adds a unique request ID for each request to the request's context and a X-Request-ID header to the response.

func AddSecurityHeaders

func AddSecurityHeaders(next http.Handler) http.Handler

AddSecurityHeaders is middleware that adds headers to improve security.

func FuncName

func FuncName() string

FuncName returns the name of the function that called it. If the function name cannot be determined, "unknown" is returned.

func FuncNameParent

func FuncNameParent() string

FuncNameParent returns the name of the parent of the calling function. If the parent function name cannot be determined, "unknown" is returned.

func HandlerTestWithCases

func HandlerTestWithCases(t *testing.T, handlerFunc http.HandlerFunc, testCases []TestCase)

HandlerTestWithCases is a utility function for testing a handler.

func LogRequest

func LogRequest(next http.Handler) http.Handler

LogRequest is a middleware function that logs the details of incoming HTTP requests. It is designed to wrap around other HTTP handlers, adding logging functionality to them.

func LoggerFromContext

func LoggerFromContext(ctx context.Context) *slog.Logger

LoggerFromContext extracts the logger from ctx. If ctx is nil or does not contain a logger, the slog default logger returned.

func RandomStringFromCharset

func RandomStringFromCharset(charset string, length int) (string, error)

RandomStringFromCharset generates a random string of length from characters in the charset. This version is Unicode-aware and works correctly with multi-byte characters.

func RemoteGetHandler

func RemoteGetHandler(w http.ResponseWriter, r *http.Request)

RemoteGetHandler responds with RemoteAddr and common headers for the actual RemoteAddr if a proxy, load balancer, or similar is used to route the request.

func RequestHandler

func RequestHandler(w http.ResponseWriter, r *http.Request)

RequestHandler responds with a dump of the HTTP request.

func RequestID

func RequestID(ctx context.Context) string

RequestID retrieves the request ID from the context. If the context is nil or does not contain a request ID, an empty string is returned.

func RequestLogger

func RequestLogger(r *http.Request) *slog.Logger

RequestLogger creates and returns a logger with request-specific details. This function can be a substitute to AddLogger middleware.

func RequestLoggerWithFunc

func RequestLoggerWithFunc(r *http.Request) *slog.Logger

RequestLoggerWithFunc enhances RequestLogger with the function name.

Types

type LoggerKeyType

type LoggerKeyType struct{}

LoggerKeyType is used to avoid key collisions with other context values.

type ReqIDType

type ReqIDType struct{}

ReqIDType is a unique key type to avoid collisions with other context values.

type TestCase

type TestCase struct {
	Name               string
	Target             string
	RequestMethod      string
	RequestHeaders     http.Header
	RequestCookies     []http.Cookie
	RequestBody        string
	WantStatus         int
	WantBody           string
	WantCookies        []http.Cookie
	WantCookiesCmpOpts cmp.Options
}

Jump to

Keyboard shortcuts

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