middleware

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserIDKey   contextKey = "user_id"
	EmailKey    contextKey = "email"
	RoleKey     contextKey = "role"
	DomainIDKey contextKey = "domain_id"
)

Variables

This section is empty.

Functions

func Auth

func Auth(jwtSecret string, apiKeyRepo repository.APIKeyRepository, logger *zap.Logger) func(http.Handler) http.Handler

Auth returns middleware that validates JWT tokens and API keys

func GenerateJWT

func GenerateJWT(userID int64, email, role string, domainID *int64, secret string) (string, error)

GenerateJWT generates a new JWT token for a user

func GetDomainID

func GetDomainID(r *http.Request) (int64, bool)

GetDomainID retrieves domain ID from request context

func GetEmail

func GetEmail(r *http.Request) (string, bool)

GetEmail retrieves email from request context

func GetRole

func GetRole(r *http.Request) (string, bool)

GetRole retrieves role from request context

func GetUserID

func GetUserID(r *http.Request) (int64, bool)

GetUserID retrieves user ID from request context

func Logger

func Logger(logger *zap.Logger) func(http.Handler) http.Handler

Logger middleware logs HTTP requests

func RateLimit

func RateLimit(repo repository.RateLimitRepository, logger *zap.Logger) func(http.Handler) http.Handler

RateLimit returns middleware that enforces API rate limiting

func RespondCreated

func RespondCreated(w http.ResponseWriter, data interface{}, message string)

RespondCreated sends a 201 Created response

func RespondError

func RespondError(w http.ResponseWriter, status int, message string)

RespondError sends an error response

func RespondJSON

func RespondJSON(w http.ResponseWriter, status int, payload interface{})

RespondJSON sends a JSON response

func RespondNoContent

func RespondNoContent(w http.ResponseWriter)

RespondNoContent sends a 204 No Content response

func RespondPaginated

func RespondPaginated(w http.ResponseWriter, data interface{}, page, pageSize, totalItems int)

RespondPaginated sends a paginated response

func RespondSuccess

func RespondSuccess(w http.ResponseWriter, data interface{}, message string)

RespondSuccess sends a success response

Types

type Claims

type Claims struct {
	UserID   int64  `json:"user_id"`
	Email    string `json:"email"`
	Role     string `json:"role"`
	DomainID *int64 `json:"domain_id,omitempty"`
	jwt.RegisteredClaims
}

Claims represents JWT claims

func ValidateJWT

func ValidateJWT(tokenString, secret string) (*Claims, error)

ValidateJWT validates and parses a JWT token

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
	Code    int    `json:"code"`
}

ErrorResponse represents an API error response

type LoggingMiddleware

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

LoggingMiddleware handles request logging

func NewLoggingMiddleware

func NewLoggingMiddleware(logger *zap.Logger) *LoggingMiddleware

NewLoggingMiddleware creates a new logging middleware

func (*LoggingMiddleware) Wrap

func (m *LoggingMiddleware) Wrap(next http.Handler) http.Handler

Wrap wraps an HTTP handler with logging

type PaginatedResponse

type PaginatedResponse struct {
	Data       interface{} `json:"data"`
	Page       int         `json:"page"`
	PageSize   int         `json:"page_size"`
	TotalPages int         `json:"total_pages"`
	TotalItems int         `json:"total_items"`
}

PaginatedResponse represents a paginated API response

type SuccessResponse

type SuccessResponse struct {
	Data    interface{} `json:"data,omitempty"`
	Message string      `json:"message,omitempty"`
}

SuccessResponse represents an API success response

Jump to

Keyboard shortcuts

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