middleware
v1.0.3
goforge.dev/gpchi/middleware
Documentation
pkg.go.dev ↗Constants
RequestIDKey
const RequestIDKey requestIDKey = 0
Variables
DefaultLogger
var DefaultLogger func(http.Handler) http.Handler
IsTTY
var IsTTY = stdoutIsTTY()
LogEntryCtxKey
var LogEntryCtxKey = &logEntryKey{"LogEntry"}
RequestIDHeader
var RequestIDHeader = "X-Request-Id"
URLFormatCtxKey
var URLFormatCtxKey = &urlFormatKey{}
Functions
AllowContentEncoding
func AllowContentEncoding(encodings ...string) func(http.Handler) http.Handler
AllowContentType
func AllowContentType(contentTypes ...string) func(http.Handler) http.Handler
BasicAuth
func BasicAuth(realm string, credentials map[string]string) func(http.Handler) http.Handler
CleanPath
func CleanPath(next http.Handler) http.Handler
ClientIPFromHeader
func ClientIPFromHeader(name string) func(http.Handler) http.Handler
ClientIPFromRemoteAddr
func ClientIPFromRemoteAddr(next http.Handler) http.Handler
ClientIPFromXFF
func ClientIPFromXFF(trustedPrefixes ...string) func(http.Handler) http.Handler
ClientIPFromXFFTrustedProxies
func ClientIPFromXFFTrustedProxies(count int) func(http.Handler) http.Handler
Compress
func Compress(level int, types ...string) func(http.Handler) http.Handler
ContentCharset
func ContentCharset(charsets ...string) func(http.Handler) http.Handler
GetClientIP
func GetClientIP(ctx context.Context) string
GetClientIPAddr
func GetClientIPAddr(ctx context.Context) netip.Addr
GetHead
func GetHead(next http.Handler) http.Handler
GetReqID
func GetReqID(ctx context.Context) string
Heartbeat
func Heartbeat(endpoint string) func(http.Handler) http.Handler
Logger
func Logger(next http.Handler) http.Handler
Maybe
func Maybe(middleware func(http.Handler) http.Handler, when func(*http.Request) bool) func(http.Handler) http.Handler
New
func New(handler http.Handler) func(http.Handler) http.Handler
NextRequestID
func NextRequestID() uint64
NoCache
func NoCache(next http.Handler) http.Handler
PageRoute
func PageRoute(path string, page http.Handler) func(http.Handler) http.Handler
PathRewrite
func PathRewrite(old, new string) func(http.Handler) http.Handler
PrintPrettyStack
func PrintPrettyStack(recovered any)
Profiler
func Profiler() http.Handler
RealIP
func RealIP(next http.Handler) http.Handler
RealIP rewrites RemoteAddr from proxy headers for chi compatibility.
Deprecated: forwarded headers are attacker-controlled unless a trusted proxy overwrites them. Prefer a policy that explicitly names trusted proxies.
Recoverer
func Recoverer(next http.Handler) http.Handler
RedirectSlashes
func RedirectSlashes(next http.Handler) http.Handler
RequestID
func RequestID(next http.Handler) http.Handler
RequestLogger
func RequestLogger(formatter LogFormatter) func(http.Handler) http.Handler
RequestSize
func RequestSize(bytes int64) func(http.Handler) http.Handler
SetHeader
func SetHeader(key, value string) func(http.Handler) http.Handler
StripPrefix
func StripPrefix(prefix string) func(http.Handler) http.Handler
StripSlashes
func StripSlashes(next http.Handler) http.Handler
Sunset
func Sunset(at time.Time, links ...string) func(http.Handler) http.Handler
SupressNotFound
func SupressNotFound(router *chi.Mux) func(http.Handler) http.Handler
Throttle
func Throttle(limit int) func(http.Handler) http.Handler
ThrottleBacklog
func ThrottleBacklog(limit, backlogLimit int, backlogTimeout time.Duration) func(http.Handler) http.Handler
ThrottleWithOpts
func ThrottleWithOpts(options ThrottleOpts) func(http.Handler) http.Handler
Timeout
func Timeout(timeout time.Duration) func(http.Handler) http.Handler
URLFormat
func URLFormat(next http.Handler) http.Handler
WithLogEntry
func WithLogEntry(r *http.Request, entry LogEntry) *http.Request
WithValue
func WithValue(key, value any) func(http.Handler) http.Handler
Types
Compressor
type Compressor struct {
// contains filtered or unexported fields
}
NewCompressor
func NewCompressor(level int, types ...string) *Compressor
Compressor.Handler
func (compressor *Compressor) Handler(next http.Handler) http.Handler
Compressor.SetEncoder
func (compressor *Compressor) SetEncoder(name string, makeEncoder EncoderFunc)
DefaultLogFormatter
type DefaultLogFormatter struct {
Logger LoggerInterface
NoColor bool
}
DefaultLogFormatter.NewLogEntry
func (formatter *DefaultLogFormatter) NewLogEntry(r *http.Request) LogEntry
EncoderFunc
type EncoderFunc func(io.Writer, int) io.Writer
HeaderRoute
type HeaderRoute struct {
Middleware func(http.Handler) http.Handler
MatchOne Pattern
MatchAny []Pattern
}
HeaderRoute.IsMatch
func (route HeaderRoute) IsMatch(value string) bool
HeaderRouter
type HeaderRouter map[string][]HeaderRoute
RouteHeaders
func RouteHeaders() HeaderRouter
HeaderRouter.Handler
func (router HeaderRouter) Handler(next http.Handler) http.Handler
HeaderRouter.Route
func (router HeaderRouter) Route(header, match string, middleware func(http.Handler) http.Handler) HeaderRouter
HeaderRouter.RouteAny
func (router HeaderRouter) RouteAny(header string, matches []string, middleware func(http.Handler) http.Handler) HeaderRouter
HeaderRouter.RouteDefault
func (router HeaderRouter) RouteDefault(middleware func(http.Handler) http.Handler) HeaderRouter
LogEntry
type LogEntry interface {
Write(status, bytes int, header http.Header, elapsed time.Duration, extra any)
Panic(value any, stack []byte)
}
GetLogEntry
func GetLogEntry(r *http.Request) LogEntry
LogFormatter
type LogFormatter interface {
NewLogEntry(*http.Request) LogEntry
}
LoggerInterface
type LoggerInterface interface {
Print(...any)
}
Pattern
type Pattern struct {
// contains filtered or unexported fields
}
NewPattern
func NewPattern(value string) Pattern
Pattern.Match
func (pattern Pattern) Match(value string) bool
ThrottleOpts
type ThrottleOpts struct {
RetryAfterFn func(ctxDone bool) time.Duration
Limit int
BacklogLimit int
BacklogTimeout time.Duration
StatusCode int
}
WrapResponseWriter
type WrapResponseWriter interface {
http.ResponseWriter
Status() int
BytesWritten() int
Tee(io.Writer)
Unwrap() http.ResponseWriter
Discard()
}
NewWrapResponseWriter
func NewWrapResponseWriter(writer http.ResponseWriter, protocolMajor int) WrapResponseWriter