Documentation
¶
Overview ¶
SPDX-License-Identifier: AGPL-3.0-or-later
SPDX-License-Identifier: AGPL-3.0-or-later
SPDX-License-Identifier: AGPL-3.0-or-later
SPDX-License-Identifier: AGPL-3.0-or-later
SPDX-License-Identifier: AGPL-3.0-or-later
Index ¶
- Constants
- func AddRequestIDToContext(next http.Handler) http.Handler
- func GetClientIP(r *http.Request) string
- func GetUserFromContext(ctx context.Context) (*models.User, bool)
- func RequestLogger(next http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- func WriteConflict(w http.ResponseWriter, message string)
- func WriteError(w http.ResponseWriter, statusCode int, code ErrorCode, message string, ...)
- func WriteForbidden(w http.ResponseWriter, message string)
- func WriteInternalError(w http.ResponseWriter)
- func WriteJSON(w http.ResponseWriter, statusCode int, data interface{})
- func WriteJSONWithMeta(w http.ResponseWriter, statusCode int, data interface{}, ...)
- func WriteNoContent(w http.ResponseWriter)
- func WriteNotFound(w http.ResponseWriter, resource string)
- func WritePaginatedJSON(w http.ResponseWriter, data interface{}, page, limit, total int)
- func WriteUnauthorized(w http.ResponseWriter, message string)
- func WriteValidationError(w http.ResponseWriter, message string, fieldErrors map[string]string)
- type ContextKey
- type ErrorCode
- type ErrorDetail
- type ErrorResponse
- type Middleware
- func (m *Middleware) CORS(next http.Handler) http.Handler
- func (m *Middleware) CSRFProtect(next http.Handler) http.Handler
- func (m *Middleware) GenerateCSRFToken() (string, error)
- func (m *Middleware) OptionalAuth(next http.Handler) http.Handler
- func (m *Middleware) RequireAdmin(next http.Handler) http.Handler
- func (m *Middleware) RequireAuth(next http.Handler) http.Handler
- func (m *Middleware) ValidateCSRFToken(token string) bool
- type PaginationMeta
- type RateLimit
- type Response
Constants ¶
const ( // ContextKeyUser is the context key for the authenticated user ContextKeyUser ContextKey = "user" // ContextKeyRequestID is the context key for the request ID ContextKeyRequestID ContextKey = "request_id" // CSRFTokenHeader is the header name for CSRF token CSRFTokenHeader = "X-CSRF-Token" // CSRFTokenCookie is the cookie name for CSRF token CSRFTokenCookie = "csrf_token" )
Variables ¶
This section is empty.
Functions ¶
func AddRequestIDToContext ¶
AddRequestIDToContext middleware adds the request ID from chi middleware to our context
func GetClientIP ¶ added in v1.2.1
GetClientIP extracts the real client IP address from the request It checks X-Forwarded-For, X-Real-IP, and falls back to RemoteAddr
func GetUserFromContext ¶
GetUserFromContext retrieves the user from the request context
func RequestLogger ¶
RequestLogger middleware logs all API requests with structured logging
func SecurityHeaders ¶
SecurityHeaders middleware adds security headers
func WriteConflict ¶
func WriteConflict(w http.ResponseWriter, message string)
WriteConflict writes a conflict error response
func WriteError ¶
func WriteError(w http.ResponseWriter, statusCode int, code ErrorCode, message string, details map[string]interface{})
WriteError writes a standardized error response
func WriteForbidden ¶
func WriteForbidden(w http.ResponseWriter, message string)
WriteForbidden writes a forbidden error response
func WriteInternalError ¶
func WriteInternalError(w http.ResponseWriter)
WriteInternalError writes an internal server error response
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, statusCode int, data interface{})
WriteJSON writes a JSON response
func WriteJSONWithMeta ¶
func WriteJSONWithMeta(w http.ResponseWriter, statusCode int, data interface{}, meta map[string]interface{})
WriteJSONWithMeta writes a JSON response with metadata
func WriteNoContent ¶
func WriteNoContent(w http.ResponseWriter)
WriteNoContent writes a 204 No Content response
func WriteNotFound ¶
func WriteNotFound(w http.ResponseWriter, resource string)
WriteNotFound writes a not found error response
func WritePaginatedJSON ¶
func WritePaginatedJSON(w http.ResponseWriter, data interface{}, page, limit, total int)
WritePaginatedJSON writes a paginated JSON response
func WriteUnauthorized ¶
func WriteUnauthorized(w http.ResponseWriter, message string)
WriteUnauthorized writes an unauthorized error response
func WriteValidationError ¶
func WriteValidationError(w http.ResponseWriter, message string, fieldErrors map[string]string)
WriteValidationError writes a validation error response
Types ¶
type ErrorCode ¶
type ErrorCode string
ErrorCode represents standardized API error codes
const ( // Client errors ErrCodeValidation ErrorCode = "VALIDATION_ERROR" ErrCodeBadRequest ErrorCode = "BAD_REQUEST" ErrCodeForbidden ErrorCode = "FORBIDDEN" ErrCodeNotFound ErrorCode = "NOT_FOUND" ErrCodeConflict ErrorCode = "CONFLICT" ErrCodeRateLimited ErrorCode = "RATE_LIMITED" ErrCodeCSRFInvalid ErrorCode = "CSRF_INVALID" // Server errors ErrCodeInternal ErrorCode = "INTERNAL_ERROR" )
type ErrorDetail ¶
type ErrorDetail struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details,omitempty"`
}
ErrorDetail contains error details
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorDetail `json:"error"`
}
ErrorResponse represents a standardized error response
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware represents API middleware
func NewMiddleware ¶
func NewMiddleware(authService *auth.OauthService, baseURL string, adminEmails []string) *Middleware
NewMiddleware creates a new middleware instance
func (*Middleware) CORS ¶
func (m *Middleware) CORS(next http.Handler) http.Handler
CORS middleware for handling cross-origin requests
func (*Middleware) CSRFProtect ¶
func (m *Middleware) CSRFProtect(next http.Handler) http.Handler
CSRFProtect middleware for CSRF protection
func (*Middleware) GenerateCSRFToken ¶
func (m *Middleware) GenerateCSRFToken() (string, error)
GenerateCSRFToken generates a new CSRF token
func (*Middleware) OptionalAuth ¶
func (m *Middleware) OptionalAuth(next http.Handler) http.Handler
OptionalAuth middleware adds user to context if authenticated, but doesn't block if not
func (*Middleware) RequireAdmin ¶
func (m *Middleware) RequireAdmin(next http.Handler) http.Handler
RequireAdmin middleware ensures user is an admin
func (*Middleware) RequireAuth ¶
func (m *Middleware) RequireAuth(next http.Handler) http.Handler
RequireAuth middleware ensures user is authenticated
func (*Middleware) ValidateCSRFToken ¶
func (m *Middleware) ValidateCSRFToken(token string) bool
ValidateCSRFToken validates a CSRF token
type PaginationMeta ¶
type PaginationMeta struct {
Page int `json:"page"`
Limit int `json:"limit"`
Total int `json:"total"`
TotalPages int `json:"totalPages"`
}
PaginationMeta represents pagination metadata
type RateLimit ¶
type RateLimit struct {
// contains filtered or unexported fields
}
RateLimit represents a simple rate limiter
func NewRateLimit ¶
NewRateLimit creates a new rate limiter