Documentation
¶
Index ¶
- func AbortWithJsonError(ctx httpx.Context, err error)
- func EndpointsToMatches(base string, endpoints ...[][3]string) map[string]map[string]string
- func Fs(local string, files fs.FS, emPath string) (fs.FS, error)
- func MatchOperation(base string, endpoints [][3]string, operations ...string) func(ctx httpx.Context) bool
- func ParseError(err error) (code int32, status int32, message string)
- func SetDefaultErrorParser(parser ErrorParser)
- func Value[T any](ctx httpx.Context, key string) (T, bool)
- func WithFormFileBytes[T any](handler func(ctx httpx.Context, file []byte, filename string) (*T, error), ...) httpx.Handler
- func WithFormFileReader[T any](...) httpx.Handler
- func WithJson[T any](handler func(ctx httpx.Context) (T, error)) httpx.Handler
- func WithRecover(message string, handler func(ctx httpx.Context) error) httpx.Handler
- func WithText(handler func(ctx httpx.Context) (string, error)) httpx.Handler
- type DataResponse
- type ErrorParser
- type ErrorResponse
- type WithFormOption
- type WithFormOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AbortWithJsonError ¶
AbortWithJsonError terminates the request with a JSON error response. It uses the configured error parser to extract error details and ensures the HTTP status code is valid (200-599 range).
func EndpointsToMatches ¶
func Fs ¶
Fs create a fs.FS from either a local directory or an embedded filesystem. Priority:
- local directory (if it exists)
- embedded filesystem + emPath
func MatchOperation ¶
func ParseError ¶
ParseError extracts error information from various error types. It recognizes StatusError, CodeError, and MessageError interfaces and falls back to defaults for unknown error types.
func SetDefaultErrorParser ¶
func SetDefaultErrorParser(parser ErrorParser)
SetDefaultErrorParser sets the global error parser function for the package. This parser will be used by AbortWithJsonError when no specific parser is provided.
func Value ¶
Value retrieves a typed value from the Gin context. It returns the value and a boolean indicating whether the key exists and the type matches.
func WithFormFileBytes ¶
func WithFormFileBytes[T any](handler func(ctx httpx.Context, file []byte, filename string) (*T, error), options ...WithFormOption) httpx.Handler
WithFormFileBytes creates a Gin handler that processes uploaded files as byte arrays. It reads the entire file content into memory and passes it to the handler function. This is convenient for smaller files but should be used carefully with large files.
func WithFormFileReader ¶
func WithFormFileReader[T any](handler func(ctx httpx.Context, file io.ReadSeekCloser, filename string) (*T, error), options ...WithFormOption) httpx.Handler
WithFormFileReader creates a Gin handler that processes uploaded files as io.ReadSeekCloser. It validates file size, extension constraints, and passes the file content to the handler function. The handler receives the file as an io.Reader along with the original filename.
func WithJson ¶
WithJson creates a Gin handler that returns JSON responses for typed data. It automatically handles errors by calling AbortWithJsonError and wraps successful responses in a standardized DataResponse structure.
func WithRecover ¶
WithRecover wraps a Gin handler with panic recovery. If a panic occurs, it logs the error and returns a standardized internal server error response.
Types ¶
type DataResponse ¶
type DataResponse[T any] struct { Success bool `json:"success" default:"true"` Code int `json:"code,omitempty" default:"0"` Data T `json:"data"` }
DataResponse represents a successful API response containing typed data. It follows a standard structure for consistent API responses across the application.
type ErrorParser ¶
ErrorParser is a function type that extracts error information for HTTP responses. It returns the error code, HTTP status code, and user-friendly message from an error.
type ErrorResponse ¶
type ErrorResponse struct {
Success bool `json:"success" default:"false"`
Code int `json:"code" default:"0"`
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
ErrorResponse represents an API error response with error details. It provides both error and message fields for different levels of error information.
type WithFormOption ¶
type WithFormOption func(*WithFormOptions)
WithFormOption is a functional option for configuring file upload behavior.
func WithFormAllowExtensions ¶
func WithFormAllowExtensions(extensions ...string) WithFormOption
WithFormAllowExtensions restricts file uploads to specific file extensions. Extensions are matched case-insensitively. If no extensions are provided, all file types are allowed.
func WithFormFileKey ¶
func WithFormFileKey(key string) WithFormOption
WithFormFileKey sets the form field name for file uploads. The default field name is "file".
func WithFormMaxSize ¶
func WithFormMaxSize(maxSize int64) WithFormOption
WithFormMaxSize sets the maximum file size allowed for uploads. The size is specified in bytes.
type WithFormOptions ¶
type WithFormOptions struct {
// contains filtered or unexported fields
}
WithFormOptions contains configuration for file upload handling via multipart forms.