middleware

package
v0.0.0-...-e705344 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const LoggerContextKey contextKey = "logger"
View Source
const RequestIDContextKey contextKey = "request_id"

Variables

This section is empty.

Functions

func Chain

func Chain(h http.Handler, middlewares ...Middleware) http.Handler

func ContextGet

func ContextGet(r *http.Request) *models.User

func ContextSetRequestId

func ContextSetRequestId(r *http.Request, requestId string) *http.Request

func ContextSetUser

func ContextSetUser(r *http.Request, user *models.User) *http.Request

func GetRequestID

func GetRequestID(ctx context.Context) string

Types

type CustomClaims

type CustomClaims struct {
	jwt.RegisteredClaims
	UserID string `json:"userId,omitempty"`
}

func ValidateToken

func ValidateToken(logger *slog.Logger, secret string, tokenString string) (*CustomClaims, error)

ValidateToken parses and validates the JWT token

type Middleware

type Middleware func(http.Handler) http.Handler

func Recovery

func Recovery(logger *slog.Logger) Middleware

func RequireActiveUser

func RequireActiveUser(logger *slog.Logger, db *database.Queries) Middleware

func WithRequestID

func WithRequestID() Middleware

Log each request to an id

type MiddlewareConfig

type MiddlewareConfig struct {
	Auth struct {
		TokenTTL  time.Duration
		JWTSecret string
	}
	Logger struct {
		SkipPaths      []string
		LogRequestBody bool
	}
	RateLimit struct {
		RequestsPerMinute int
		BurstSize         int
	}
}

type MiddlewareHandler

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

func NewMiddlewareHandler

func NewMiddlewareHandler(logger *slog.Logger, config MiddlewareConfig) *MiddlewareHandler

func (*MiddlewareHandler) Auth

func (m *MiddlewareHandler) Auth(userRepository repository.UserRepository) Middleware

func (*MiddlewareHandler) Logger

func (m *MiddlewareHandler) Logger() Middleware

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Custom ResponseWriter to capture status code

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Status

func (rw *ResponseWriter) Status() int

func (*ResponseWriter) WriteHeader

func (rw *ResponseWriter) WriteHeader(code int)

type TokenError

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

func (*TokenError) Error

func (e *TokenError) Error() string

Jump to

Keyboard shortcuts

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