nexmiddleware

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxRequests is the number of allowed requests in the defined time window.
	MaxRequests = 100

	// TimeWindow is the duration in which the requests are counted.
	TimeWindow = time.Hour
)
View Source
const (
	// SecretKey used to sign JWTs. This should be kept private and secure.
	SecretKey = "your_secret_key_here"
)

Variables

View Source
var DefaultCORSConfig = CORSConfig{
	AllowOrigins: []string{"*"},
	AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
	AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
}

DefaultCORSConfig is a basic default configuration for CORS.

Functions

func Authentication

func Authentication() router.MiddlewareFunc

Authentication middleware using JWT

func Authorization

func Authorization(requiredPermission string) router.MiddlewareFunc

Authorization middleware

func CORS

func CORS(config CORSConfig) router.MiddlewareFunc

CORS middleware

func CacheControl

func CacheControl(maxAge time.Duration, directives ...string) router.MiddlewareFunc

CacheControl sets the Cache-Control header for the response.

func Compression

func Compression() router.MiddlewareFunc

Compression checks the request's Accept-Encoding header and, if appropriate, wraps the response writer in a gzip writer.

func ContentTypeChecking

func ContentTypeChecking(expectedContentType string) router.MiddlewareFunc

ContentTypeChecking ensures that the client sends requests with the expected content type.

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the Request ID from the context.

func Logging

func Logging() router.MiddlewareFunc

Logging logs the method, path, request ID, IP, user agent, and time for each request.

func RateLimiter

func RateLimiter(store Store) router.MiddlewareFunc

RateLimiter is a middleware for rate limiting requests.

func Recovery

func Recovery() router.MiddlewareFunc

Recovery recovers from panics and logs a stack trace.

func RequestID

func RequestID() router.MiddlewareFunc

RequestID is a middleware that assigns a unique ID to each incoming request.

func SessionManagement

func SessionManagement(store SessionStore) router.MiddlewareFunc

SessionManagement manages user sessions.

Types

type CORSConfig

type CORSConfig struct {
	AllowOrigins []string
	AllowMethods []string
	AllowHeaders []string
}

CORSConfig defines the config for CORS middleware.

type Session

type Session struct {
	ID        string
	UserID    string
	ExpiresAt time.Time
	Data      map[string]interface{}
}

Session represents a user's session with data.

type SessionStore

type SessionStore interface {
	Get(sessionID string) (*Session, error)
	Save(session *Session) error
	Delete(sessionID string) error
}

SessionStore represents a store for sessions.

type Store

type Store interface {
	GetRequestCount(identifier string) (int, error)
	IncrementRequestCount(identifier string, duration time.Duration) error
}

Store interface defines methods to interact with the backend store.

Jump to

Keyboard shortcuts

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