common

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAnonymousUser added in v0.2.0

func GetAnonymousUser(roleName string) *user.User

GetAnonymousUser returns a singleton anonymous user with the specified role

func GetAuthenticatedUser

func GetAuthenticatedUser(ctx context.Context) (*user.User, bool)

GetAuthenticatedUser returns the current authenticated user

func ParseLimit

func ParseLimit(limitStr string, defaultLimit, maxLimit int) int

ParseLimit parses and validates limit parameter

func ParseOffset

func ParseOffset(offsetStr string) int

ParseOffset parses and validates offset parameter

func RequirePermission

func RequirePermission(userService user.Service, resourceType, action string) func(http.HandlerFunc) http.HandlerFunc

RequirePermission middleware checks if the user has required permissions

func RespondError

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

RespondError sends a standard error response

func RespondJSON

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

RespondJSON sends a JSON response with standard headers

func WithAnonymousContext added in v0.2.0

func WithAnonymousContext(ctx context.Context, role string) context.Context

WithAnonymousContext adds anonymous context to the request context

func WithAuth

func WithAuth(userService user.Service, authService auth.Service, cfg *config.Config) func(http.HandlerFunc) http.HandlerFunc

WithAuth middleware handles both API key and JWT authentication

func WithRateLimit added in v0.3.2

func WithRateLimit(cfg *config.Config, limit int, window int) func(http.HandlerFunc) http.HandlerFunc

WithRateLimit middleware enforces rate limiting per user/IP with per-endpoint limits

Types

type AnonymousContext added in v0.2.0

type AnonymousContext struct {
	RoleName string
}

AnonymousContext represents an authentication context for anonymous access

func GetAnonymousContext added in v0.2.0

func GetAnonymousContext(ctx context.Context) (AnonymousContext, bool)

GetAnonymousContext retrieves anonymous context from the request context

type ContextKey

type ContextKey string

Context keys for storing authenticated user info

const (
	UserContextKey ContextKey = "user"
)

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse represents an API error response

type Filter

type Filter struct {
	Limit  int      `json:"limit"`
	Offset int      `json:"offset"`
	Sort   []string `json:"sort,omitempty"`
}

Filter represents common query parameters for list operations

type RateLimitStore added in v0.3.2

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

RateLimitStore manages rate limit counters in memory

func NewRateLimitStore added in v0.3.2

func NewRateLimitStore() *RateLimitStore

NewRateLimitStore creates a new in-memory rate limit store

type Route

type Route struct {
	Path       string
	Method     string
	Handler    http.HandlerFunc
	Middleware []func(http.HandlerFunc) http.HandlerFunc
}

Route represents a route for the HTTP server

type TimeRange

type TimeRange struct {
	Start time.Time `json:"start"`
	End   time.Time `json:"end"`
}

TimeRange represents a time-based filter range

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

ValidationError represents a field validation error

Jump to

Keyboard shortcuts

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