middlewares

package
v0.0.0-...-8f4d57c Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const CLEANUP_CACHE_INTERVAL = time.Minute * 10
View Source
const DEFAULT_CACHE_TTL = time.Minute * 1
View Source
const DefaultRemoteAddr = "1.2.3.4"

DefaultRemoteAddr is the default remote address to return in RemoteAddr if an explicit DefaultRemoteAddr isn't set on ResponseRecorder.

Variables

View Source
var ErrZoneNotSupported = errors.New("rate limit zone is not supported")
View Source
var SupportedZones = []string{"ip"}

Functions

func GzipMiddleware

func GzipMiddleware(next http.Handler) http.Handler

GzipMiddleware compresses the response using gzip if the client supports it

func NewLoggingMiddleware

func NewLoggingMiddleware(out io.Writer) func(http.Handler) http.Handler

Logging middleware log the request / response with the log style of nginx

func NewOmitHeadersMiddleware

func NewOmitHeadersMiddleware(headersToOmit []string) func(http.Handler) http.Handler

OmitHeaders middleware removes specified headers from the response to enhance security.

func NewRateLimitMiddleware

func NewRateLimitMiddleware(limits []string) (func(http.Handler) http.Handler, error)

func NewRequestSizeLimitMiddleware

func NewRequestSizeLimitMiddleware(maxSize uint64) func(http.Handler) http.Handler

NewRequestSizeLimitMiddleware limits the size of the request body to the specified limit in bytes.

func NewTimeoutMiddleware

func NewTimeoutMiddleware(timeout time.Duration) func(next http.Handler) http.Handler

NewTimeoutMiddleware returns an HTTP handler that wraps the provided handler with a timeout. If the processing takes longer than the specified timeout, it returns a 503 Service Unavailable error.

Types

type CachedResponse

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

type HandlerWithMiddleware

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

func NewHandlerWithMiddleware

func NewHandlerWithMiddleware(handler http.Handler) *HandlerWithMiddleware

func (*HandlerWithMiddleware) Add

func (h *HandlerWithMiddleware) Add(middleware Middleware)

func (*HandlerWithMiddleware) ServeHTTP

type LimitConfig

type LimitConfig struct {
	Zone     string
	Requests int
	Per      time.Duration
}

func ParseLimitConfig

func ParseLimitConfig(config string) (LimitConfig, error)

func (LimitConfig) GetKey

func (lc LimitConfig) GetKey(r *http.Request) (key string, err error)

type Middleware

type Middleware func(http.Handler) http.Handler

func NewAddHeadersMiddleware

func NewAddHeadersMiddleware(headers map[string]string) Middleware

func NewCacheMiddleware

func NewCacheMiddleware() Middleware

func NewMinifyMiddleware

func NewMinifyMiddleware(config MinifyConfig) Middleware

func NewOpenAPIValidationMiddleware

func NewOpenAPIValidationMiddleware(specPath string) (Middleware, error)

func NewOpenTelemetryMiddleware

func NewOpenTelemetryMiddleware(ctx context.Context, config OTELConfig) (Middleware, error)

type MinifyConfig

type MinifyConfig struct {
	ALL  bool
	JS   bool
	CSS  bool
	HTML bool
	JSON bool
	SVG  bool
	XML  bool
}

type OTELConfig

type OTELConfig struct {
	ServiceDomain string
	BasePath      string
}

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter() *RateLimiter

type ResponseRecorder

type ResponseRecorder struct {
	// Code is the HTTP response code set by WriteHeader.
	//
	// Note that if a Handler never calls WriteHeader or Write,
	// this might end up being 0, rather than the implicit
	// http.StatusOK. To get the implicit value, use the Result
	// method.
	Code int

	// HeaderMap contains the headers explicitly set by the Handler.
	// It is an internal detail.
	//
	// Deprecated: HeaderMap exists for historical compatibility
	// and should not be used. To access the headers returned by a handler,
	// use the Response.Header map as returned by the Result method.
	HeaderMap http.Header

	// Body is the buffer to which the Handler's Write calls are sent.
	// If nil, the Writes are silently discarded.
	Body *bytes.Buffer

	// Flushed is whether the Handler called Flush.
	Flushed bool
	// contains filtered or unexported fields
}

ResponseRecorder is an implementation of http.ResponseWriter.

func NewRecorder

func NewRecorder() *ResponseRecorder

NewRecorder returns an initialized ResponseRecorder.

func (*ResponseRecorder) Header

func (rw *ResponseRecorder) Header() http.Header

Header implements http.ResponseWriter. It returns the response headers to mutate within a handler.

func (*ResponseRecorder) Result

func (rw *ResponseRecorder) Result() *http.Response

Result returns the response generated by the handler.

The returned Response will have at least its StatusCode, Header, Body, and optionally Trailer populated. More fields may be populated in the future, so callers should not DeepEqual the result in tests.

The Response.Header is a snapshot of the headers at the time of the first write call, or at the time of this call, if the handler never did a write.

The Response.Body is guaranteed to be non-nil and Body.Read call is guaranteed to not return any error other than io.EOF.

Result must only be called after the handler has finished running.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Write implements http.ResponseWriter. The data in buf is written to rw.Body, if not nil.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(code int)

WriteHeader implements http.ResponseWriter.

func (*ResponseRecorder) WriteHeadersTo

func (rr *ResponseRecorder) WriteHeadersTo(rw http.ResponseWriter)

func (*ResponseRecorder) WriteString

func (rw *ResponseRecorder) WriteString(str string) (int, error)

WriteString implements io.StringWriter. The data in str is written to rw.Body, if not nil.

func (*ResponseRecorder) WriteTo

func (rr *ResponseRecorder) WriteTo(rw http.ResponseWriter)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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