middleware

package
v0.0.0-...-3264219 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 20 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCSRFToken

func GenerateCSRFToken() (string, error)

GenerateCSRFToken generates a new CSRF token.

func GetTemplate

func GetTemplate(ctx context.Context) *template.Template

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

func ValidateCSRFToken(r *http.Request) bool

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

func (*CORSMiddleware) Handle

func (cm *CORSMiddleware) Handle(next http.Handler) http.Handler

Handle sets the CORS headers on the response and restricts methods

type CORSOptions

type CORSOptions struct {
	AllowedOrigins []string
	AllowedMethods []string
	AllowedHeaders []string
}

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

func (*CSRFProtection) Handle

func (csrf *CSRFProtection) Handle(next http.Handler) http.Handler

type Caching

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

func NewCaching

func NewCaching(client *redis.Client, ttl time.Duration, cacheControl bool) *Caching

new caching

func (*Caching) Handle

func (c *Caching) Handle(next http.Handler) http.Handler

type CookieParser

type CookieParser struct{}

func NewCookieParser

func NewCookieParser() *CookieParser

func (*CookieParser) Handle

func (cp *CookieParser) Handle(next http.Handler) http.Handler

type Cookies

type Cookies string

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

func (*FileUploadMiddleware) Handle

func (f *FileUploadMiddleware) Handle(next http.Handler) http.Handler

Handle is the middleware function that processes file uploads

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

func NewInMemoryConfig(NumShards int, Limit int, Interval time.Duration, CleanupInterval time.Duration) *InMemoryConfig

type JSONParser

type JSONParser struct {
	Options ParserOptions
}

func NewJsonParser

func NewJsonParser(options ParserOptions) *JSONParser

func (*JSONParser) Handle

func (jp *JSONParser) Handle(next http.Handler) http.Handler

type JsonKey

type JsonKey string

type Middleware

type Middleware interface {
	Handle(next http.Handler) http.Handler
}

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

type MiddlewareWrapper struct {
	HandlerFunc func(next http.Handler) http.Handler
}

MiddlewareWrapper wraps a function to match the Middleware interface

func (MiddlewareWrapper) Handle

func (mw MiddlewareWrapper) Handle(next http.Handler) http.Handler

Handle implements 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

func (*ProfilingMiddleware) Handle

func (pm *ProfilingMiddleware) Handle(next http.Handler) http.Handler

Handle processes the requests and measures their execution time

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.

func (*RateLimiter) Handle

func (rl *RateLimiter) Handle(next http.Handler) http.Handler

Handle is the middleware function that processes incoming HTTP requests.

It applies rate limiting based on the specified rate limiter type (in-memory or Redis-backed).

type RateLimiterType

type RateLimiterType int

RateLimiterType defines the type of rate limiter (InMemory or RedisBacked).

const (
	InMemory RateLimiterType = iota
	RedisBacked
)

type RedisConfig

type RedisConfig struct {
	Client   redis.Client
	Limit    int
	Interval time.Duration
}

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) Flush

func (rec *ResponseRecorder) Flush()

Implement the Flush method

func (*ResponseRecorder) Write

func (rec *ResponseRecorder) Write(p []byte) (int, error)

func (*ResponseRecorder) WriteHeader

func (rec *ResponseRecorder) WriteHeader(statusCode int)

type TemplateMiddleware

type TemplateMiddleware struct {
	Tmpl *template.Template
}

func NewTemplateMiddleware

func NewTemplateMiddleware(templateDir string) *TemplateMiddleware

func (*TemplateMiddleware) Handle

func (tm *TemplateMiddleware) Handle(next http.Handler) http.Handler

type TimeoutMiddleware

type TimeoutMiddleware struct {
	Timeout time.Duration
}

func NewTimeoutMiddleware

func NewTimeoutMiddleware(timeout time.Duration) *TimeoutMiddleware

NewTimeoutMiddleware creates a new instance of timeout middleware

func (*TimeoutMiddleware) Handle

func (tm *TimeoutMiddleware) Handle(next http.Handler) http.Handler

Handle adds a timeout to the request context

type XSSProtection

type XSSProtection struct{}

func NewXSSProtection

func NewXSSProtection() *XSSProtection

Creates a new middleware for XSS protection

func (*XSSProtection) Handle

func (xss *XSSProtection) Handle(next http.Handler) http.Handler

Middleware to handle requests and check for XSS attacks

Jump to

Keyboard shortcuts

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