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 ¶
- Variables
- func AddLogger(next http.Handler) http.Handler
- func AddRequestID(next http.Handler) http.Handler
- func AddSecurityHeaders(next http.Handler) http.Handler
- func FuncName() string
- func FuncNameParent() string
- func HandlerTestWithCases(t *testing.T, handlerFunc http.HandlerFunc, testCases []TestCase)
- func LogRequest(next http.Handler) http.Handler
- func LoggerFromContext(ctx context.Context) *slog.Logger
- func RandomStringFromCharset(charset string, length int) (string, error)
- func RemoteGetHandler(w http.ResponseWriter, r *http.Request)
- func RequestHandler(w http.ResponseWriter, r *http.Request)
- func RequestID(ctx context.Context) string
- func RequestLogger(r *http.Request) *slog.Logger
- func RequestLoggerWithFunc(r *http.Request) *slog.Logger
- type LoggerKeyType
- type ReqIDType
- type TestCase
Constants ¶
This section is empty.
Variables ¶
var LoggerKey = LoggerKeyType{}
LoggerKey is the key to store and retrieve the logger from the context.
var ReqIDKey = ReqIDType{}
ReqIDKey is a key for storing and retrieving the request ID from the context.
Functions ¶
func AddLogger ¶
AddLogger wraps an HTTP handler to include a request-specific logger in its context.
func AddRequestID ¶
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 ¶
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 ¶
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 ¶
LoggerFromContext extracts the logger from ctx. If ctx is nil or does not contain a logger, the slog default logger returned.
func RandomStringFromCharset ¶
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 ¶
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 ¶
RequestLogger creates and returns a logger with request-specific details. This function can be a substitute to AddLogger middleware.
Types ¶
type LoggerKeyType ¶
type LoggerKeyType struct{}
LoggerKeyType is used to avoid key collisions with other context values.