middlewares

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware(next http.Handler) http.Handler

CORSMiddleware function sets the headers for the response and returns directly if there is an OPTIONS request

func ExampleUsage added in v0.1.12

func ExampleUsage()

Usage example

func GetClientIP added in v0.1.12

func GetClientIP(r *http.Request) string

GetClientIP extracts the client IP address from the request

func InitDefaultLogger

func InitDefaultLogger()

InitDefaultLogger sets the default logger to "github.com/gyozatech/noodlog" is no other logger has been set

func MaliciousRateLimitMiddleware added in v0.1.12

func MaliciousRateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler

MaliciousRateLimitMiddleware creates a middleware that limits requests based on IP

func RecoverPanicMiddleware

func RecoverPanicMiddleware(h http.Handler) http.Handler

func RequestLoggerMiddleware

func RequestLoggerMiddleware(next http.Handler) http.Handler

RequestLoggerMiddleware is the middleware layer to log all the HTTP requests

func SecurityHeaders added in v0.1.12

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders adds security headers to responses

func SetLogger

func SetLogger(l interface{})

SetLogger sets the logger for the middlewares

func SimpleRateLimitMiddleware added in v0.1.12

func SimpleRateLimitMiddleware(next http.Handler) http.Handler

SimpleRateLimitMiddleware is a middleware that limits the number of requests per IP address.

Types

type IPRateLimiter added in v0.1.12

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

IPRateLimiter stores rate limiters for individual IP addresses

func NewIPRateLimiter added in v0.1.12

func NewIPRateLimiter(r rate.Limit, b int, banDuration time.Duration) *IPRateLimiter

NewIPRateLimiter creates a new rate limiter for IP addresses rate: requests per second burst: maximum burst size banDuration: how long to ban IPs that exceed the rate limit

func (*IPRateLimiter) AddIP added in v0.1.12

func (i *IPRateLimiter) AddIP(ip string) *rate.Limiter

AddIP creates a new rate limiter and adds it to the ips map, using the IP address as the key

func (*IPRateLimiter) BanIP added in v0.1.12

func (i *IPRateLimiter) BanIP(ip string)

BanIP bans an IP for the configured duration

func (*IPRateLimiter) CleanupJob added in v0.1.12

func (i *IPRateLimiter) CleanupJob(cleanupInterval time.Duration)

CleanupJob removes old IP entries to prevent memory leaks Should be run periodically, e.g., via a goroutine

func (*IPRateLimiter) GetLimiter added in v0.1.12

func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter

GetLimiter returns the rate limiter for the provided IP address if it exists, otherwise calls AddIP to add a new one

type ReqRespLogStruct

type ReqRespLogStruct struct {
	Request  RequestStruct
	ExecTime time.Duration
	Response ResponseStruct
}

ReqRespLogStruct struct represents the schema for the HTTP Request/ExecutionTime/Response log

type RequestStruct

type RequestStruct struct {
	Verb     string            `json:"verb,omitempty"`
	Path     string            `json:"path,omitempty"`
	Protocol string            `json:"protocol,omitempty"`
	Headers  map[string]string `json:"headers,omitempty"`
	Body     interface{}       `json:"body,omitempty"`
}

RequestStruct struct represents the schema for the HTTP Request log

func HTTPRequest

func HTTPRequest(r *http.Request) RequestStruct

HTTPRequest static function flatten pointers of HTTP Request and obscurate passwords to prepare for logging

type ResponseStruct

type ResponseStruct struct {
	Status  int               `json:"status,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
	Body    interface{}       `json:"body,omitempty"`
}

ResponseStruct struct represents the schema for the HTTP Response log

func HTTPResponse

func HTTPResponse(statusCode int, responseHeaders http.Header, requestURI string, responseBody string) ResponseStruct

HTTPResponse static function flatten pointers of HTTP Response and obscurate passwords to prepare for logging

type ResponseWriterWrapper

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

ResponseWriterWrapper struct is used to log the response

func NewResponseWriterWrapper

func NewResponseWriterWrapper(w *http.ResponseWriter, r *http.Request) ResponseWriterWrapper

NewResponseWriterWrapper static function creates a wrapper for the http.ResponseWriter

func (ResponseWriterWrapper) Header

func (rww ResponseWriterWrapper) Header() http.Header

Header function overwrites the http.ResponseWriter Header() function

func (ResponseWriterWrapper) Hijack added in v0.1.8

func (rww ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack allows the ResponseWriterWrapper to implement the http.Hijacker interface necessary to upgrade the connection to a WebSocket

func (ResponseWriterWrapper) Write

func (rww ResponseWriterWrapper) Write(buf []byte) (int, error)

func (ResponseWriterWrapper) WriteHeader

func (rww ResponseWriterWrapper) WriteHeader(statusCode int)

WriteHeader function overwrites the http.ResponseWriter WriteHeader() function

Jump to

Keyboard shortcuts

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