Documentation
¶
Overview ¶
Package middleware provides a simple interface and base implementation for creating HTTP middlewares.
This package defines a `Middleware` interface with a `Handle` method that allows chaining HTTP handlers. It also includes a `BaseMiddleware` struct that provides a basic implementation of the middleware pattern.
Usage:
import (
"net/http"
"github.com/trustingref/lessgo/pkg/lessgo/middleware"
)
func main() {
mw := &middleware.BaseMiddleware{}
mux := http.NewServeMux()
mux.Handle("/", mw.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})))
http.ListenAndServe(":8080", mux)
}
Index ¶
- func GenerateCSRFToken() (string, error)
- func GetTemplate(ctx context.Context) *template.Template
- func SetCSRFCookie(w http.ResponseWriter, token string)
- func ValidateCSRFToken(r *http.Request) bool
- type BaseMiddleware
- type CORSMiddleware
- type CORSOptions
- type CSRFProtection
- type Caching
- type CookieParser
- type Cookies
- type FileUploadMiddleware
- type InMemoryConfig
- type JSONParser
- type JsonKey
- type Middleware
- type MiddlewareWrapper
- type ParserOptions
- type ProfilingMiddleware
- type RateLimiter
- type RateLimiterType
- type RedisConfig
- type ResponseRecorder
- type TemplateMiddleware
- type TimeoutMiddleware
- type XSSProtection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateCSRFToken ¶
GenerateCSRFToken generates a new CSRF token.
func GetTemplate ¶
GetTemplate returns the template from the context
func SetCSRFCookie ¶
func SetCSRFCookie(w http.ResponseWriter, token string)
SetCSRFCookie sets a CSRF token as a secure cookie.
func ValidateCSRFToken ¶
ValidateCSRFToken validates the CSRF token from the request header or form data.
Types ¶
type BaseMiddleware ¶
type BaseMiddleware struct{}
BaseMiddleware provides a basic implementation of the Middleware interface. It allows chaining of HTTP handlers by passing the request to the next handler in the chain.
Example:
mw := &middleware.BaseMiddleware{}
http.Handle("/", mw.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})))
http.ListenAndServe(":8080", nil)
type CORSMiddleware ¶
type CORSMiddleware struct {
// contains filtered or unexported fields
}
CORSMiddleware is the middleware that handles CORS
func NewCORSMiddleware ¶
func NewCORSMiddleware(options CORSOptions) *CORSMiddleware
NewCORSMiddleware creates a new instance of CORSMiddleware
type CORSOptions ¶
CORSOptions defines the configuration for the CORS middleware
func NewCorsOptions ¶
func NewCorsOptions(origins []string, methods []string, headers []string) *CORSOptions
NewCorsOptions creates a new CORSOptions instance
type CSRFProtection ¶
type CSRFProtection struct{}
func NewCSRFProtection ¶
func NewCSRFProtection() *CSRFProtection
type Caching ¶
type Caching struct {
// contains filtered or unexported fields
}
func NewCaching ¶
new caching
type CookieParser ¶
type CookieParser struct{}
func NewCookieParser ¶
func NewCookieParser() *CookieParser
type FileUploadMiddleware ¶
type FileUploadMiddleware struct {
// contains filtered or unexported fields
}
func NewFileUploadMiddleware ¶
func NewFileUploadMiddleware(uploadDir string, maxFileSize int64, allowedExts []string) *FileUploadMiddleware
NewFileUploadMiddleware creates a new instance of FileUploadMiddleware
type InMemoryConfig ¶
type InMemoryConfig struct {
NumShards int
Limit int
Interval time.Duration
CleanupInterval time.Duration
}
InMemoryConfig is the configuration for the in-memory rate limiter.
func NewInMemoryConfig ¶
type JSONParser ¶
type JSONParser struct {
Options ParserOptions
}
func NewJsonParser ¶
func NewJsonParser(options ParserOptions) *JSONParser
type Middleware ¶
Middleware defines the interface for HTTP middlewares. Implementers should provide a `Handle` method that takes an `http.Handler` and returns a new `http.Handler`. This allows for wrapping existing handlers with additional functionality.
type MiddlewareWrapper ¶
MiddlewareWrapper wraps a function to match the Middleware interface
type ParserOptions ¶
type ParserOptions struct {
// contains filtered or unexported fields
}
func NewParserOptions ¶
func NewParserOptions(size int64) *ParserOptions
type ProfilingMiddleware ¶
type ProfilingMiddleware struct {
}
ProfilingMiddleware represents a structure for profiling requests
func NewProfilingMiddleware ¶
func NewProfilingMiddleware() *ProfilingMiddleware
NewProfilingMiddleware creates a new instance of ProfilingMiddleware
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is a middleware that limits the number of requests a client can make to your server within a specified interval.
func NewRateLimiter ¶
func NewRateLimiter(limiterType RateLimiterType, config interface{}) *RateLimiter
NewRateLimiter creates and returns a new RateLimiter instance based on the provided configuration.
The limiterType parameter determines whether an in-memory or Redis-backed rate limiter is used. The config parameter is either an InMemoryConfig or RedisConfig, depending on the limiterType.
type RateLimiterType ¶
type RateLimiterType int
RateLimiterType defines the type of rate limiter (InMemory or RedisBacked).
const ( InMemory RateLimiterType = iota RedisBacked )
type RedisConfig ¶
RedisConfig is the configuration for the Redis-backed rate limiter.
func NewRedisConfig ¶
func NewRedisConfig(client *redis.Client, limit int, interval time.Duration) *RedisConfig
type ResponseRecorder ¶
type ResponseRecorder struct {
http.ResponseWriter
StatusCode int
Body *bytes.Buffer
}
func (*ResponseRecorder) WriteHeader ¶
func (rec *ResponseRecorder) WriteHeader(statusCode int)
type TemplateMiddleware ¶
func NewTemplateMiddleware ¶
func NewTemplateMiddleware(templateDir string) *TemplateMiddleware
type TimeoutMiddleware ¶
func NewTimeoutMiddleware ¶
func NewTimeoutMiddleware(timeout time.Duration) *TimeoutMiddleware
NewTimeoutMiddleware creates a new instance of timeout middleware
type XSSProtection ¶
type XSSProtection struct{}
func NewXSSProtection ¶
func NewXSSProtection() *XSSProtection
Creates a new middleware for XSS protection