middleware

package
v0.0.0-...-3d2ea00 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDKey ctxKey = "request_id"

Variables

This section is empty.

Functions

func Bind

func Bind(mid Middleware, route http.HandlerFunc) http.HandlerFunc

Bind applies the middleware to the route handler. This function takes a Middleware and an http.HandlerFunc (the route handler) and returns a new http.HandlerFunc that incorporates the middleware logic.

func Compress

func Compress(next http.Handler) http.Handler

Compress middleware for gzip and brotli

func ContextTimeout

func ContextTimeout(duration time.Duration) func(http.Handler) http.Handler

ContextTimeout is a middleware that sets a timeout on the request context. It will be useful for long tasks such as IO or long queries at database. Strictly use with context cancellation in your handlers! Otherwise the middleware will be useless.

func IDFromContext

func IDFromContext(ctx context.Context) string

helper func

func Log

func Log(next http.Handler) http.Handler

Log middleware handler shows network data log info

func Recover

func Recover(next http.Handler) http.Handler

Recover middleware recovers panic and debug stacktrace

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID create "X-Request-ID" header and pass context next

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache struct for caching strings in memory

func NewCache

func NewCache(cacheDuration string) *Cache

NewCache creates a new in memory Cache

func (*Cache) CacheResponse

func (c *Cache) CacheResponse(next http.Handler) http.Handler

CacheResponse middleware

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, string)

Get a cached content by key

func (*Cache) Set

func (c *Cache) Set(key string, content []byte, contentType string, duration time.Duration)

Set a cached content by key

type Item

type Item struct {
	Content     []byte
	ContentType string
	Expiration  int64
}

Item is a cached reference

func (Item) Expired

func (item Item) Expired() bool

Expired returns true if the item has expired.

type Middleware

type Middleware func(next http.Handler) http.Handler

func Limit

func Limit(requestsPerSecond rate.Limit, maxRequests int) Middleware

Limit middleware requestsPerSecond sets how many requests are allowed per second. maxRequests sets the maximum burst size.

func Middlewares

func Middlewares(middlewares ...Middleware) Middleware

Middlewares chains multiple middleware functions together. in order of execution

type StatusHTTP

type StatusHTTP struct {
	http.ResponseWriter
	StatusCode int
	// contains filtered or unexported fields
}

StatusHTTP custom struct for show response status code in log middleware

func NewStatusHTTP

func NewStatusHTTP(w http.ResponseWriter) *StatusHTTP

NewStatusHTTP function init new StatusHTTP

func (*StatusHTTP) WriteHeader

func (sr *StatusHTTP) WriteHeader(statusCode int)

WriteHeader method wrties status code in http header

type Storage

type Storage interface {

	// Get retrieves the cached content for the given key.
	Get(key string) ([]byte, string)

	// Set stores the content in the cache with the specified key and duration.
	Set(key string, content []byte, contentType string, duration time.Duration)
}

Storage interface for cache middleware implementation

Jump to

Keyboard shortcuts

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