Documentation
¶
Index ¶
- func AbortWithJsonError(ctx *gin.Context, err error)
- func AdaptMiddleware(middleware ...MiddlewareFunc) gin.HandlerFunc
- func Close(ctx context.Context, server *http.Server) error
- func EndpointsToMatches(base string, endpoints ...[][3]string) map[string]map[string]string
- func Fs(local string, emFs *embed.FS, emPath string) (http.FileSystem, error)
- func ListenAndAutoShutdown(ctx context.Context, server *http.Server, closeTimeout time.Duration) error
- func MatchOperation(base string, endpoints [][3]string, operations ...string) func(ctx *gin.Context) bool
- func ParseError(err error) (code int32, status int32, message string)
- func SetDefaultErrorParser(parser ErrorParser)
- func ShouldBind(ctx *gin.Context, obj any) error
- func ShouldBindHeader(ctx *gin.Context, obj any) error
- func ShouldBindJSON(ctx *gin.Context, obj any) error
- func ShouldBindQuery(ctx *gin.Context, obj any) error
- func ShouldBindUri(ctx *gin.Context, obj any) error
- func ShouldUniverseBind(ctx *gin.Context, obj any, uri, query, body bool) error
- func ShouldUniverseBindForm(ctx *gin.Context, obj any) error
- func ShouldUniverseBindQuery(ctx *gin.Context, obj any) error
- func ShouldUniverseBindUri(ctx *gin.Context, obj any) error
- func Start(server *http.Server) error
- func Value[T any](ctx *gin.Context, key string) (T, bool)
- func WithFormFileBytes[T any](handler func(ctx *gin.Context, file []byte, filename string) (*T, error), ...) gin.HandlerFunc
- func WithFormFileReader[T any](...) gin.HandlerFunc
- func WithHandler(h http.Handler) func(ctx *gin.Context)
- func WithJson[T any](handler func(ctx *gin.Context) (T, error)) gin.HandlerFunc
- func WithRecover(message string, handler func(ctx *gin.Context)) gin.HandlerFunc
- func WithText(handler func(ctx *gin.Context) (string, error)) gin.HandlerFunc
- type Context
- type DataResponse
- type ErrorParser
- type ErrorResponse
- type HandlerFunc
- type MiddlewareFunc
- 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 AdaptMiddleware ¶
func AdaptMiddleware(middleware ...MiddlewareFunc) gin.HandlerFunc
func Close ¶
Close gracefully shuts down the HTTP server using the provided context. It handles nil server gracefully and returns any shutdown error.
func EndpointsToMatches ¶
func Fs ¶
Fs creates an http.FileSystem from either a local directory or an embedded filesystem. It prioritizes the local path if provided, otherwise uses the embedded filesystem. Returns nil if neither source is available.
func ListenAndAutoShutdown ¶
func ListenAndAutoShutdown(ctx context.Context, server *http.Server, closeTimeout time.Duration) error
ListenAndAutoShutdown starts an HTTP server and automatically handles graceful shutdown. It listens for context cancellation to trigger shutdown with the specified timeout. Returns any error from server startup or shutdown, prioritizing startup errors.
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 ShouldBind ¶
ShouldBind binds form data to the given object using Gin's default binding.
func ShouldBindHeader ¶
ShouldBindHeader binds HTTP headers to the given object using Gin's default binding. It delegates to gin.Context.ShouldBindHeader for standard header binding.
func ShouldBindJSON ¶
ShouldBindJSON binds JSON request body to the given object using Gin's default binding. It delegates to gin.Context.ShouldBindJSON for standard JSON binding.
func ShouldBindQuery ¶
ShouldBindQuery binds query parameters to the given object using Gin's default binding. It delegates to gin.Context.ShouldBindQuery for standard query parameter binding.
func ShouldBindUri ¶
ShouldBindUri binds URI path parameters to the given object using Gin's default binding. It delegates to gin.Context.ShouldBindUri for standard URI parameter binding.
func ShouldUniverseBind ¶
ShouldUniverseBind performs comprehensive binding from multiple HTTP request sources. It can bind from JSON body, query parameters, and URI parameters based on the boolean flags. The binding order is: body -> query -> uri, with each step potentially overriding previous values.
func ShouldUniverseBindForm ¶
ShouldUniverseBindForm binds form data using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.
func ShouldUniverseBindQuery ¶
ShouldUniverseBindQuery binds query parameters using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.
func ShouldUniverseBindUri ¶
ShouldUniverseBindUri binds URI parameters using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.
func Start ¶
Start begins serving HTTP requests on the configured address. It ignores http.ErrServerClosed which is expected during graceful shutdown. Returns any other error that occurs during server startup.
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 *gin.Context, file []byte, filename string) (*T, error), options ...WithFormOption) gin.HandlerFunc
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 *gin.Context, file io.ReadSeekCloser, filename string) (*T, error), options ...WithFormOption) gin.HandlerFunc
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 WithHandler ¶
WithHandler wraps a standard http.Handler for use as a Gin handler function. It includes panic recovery and delegates the request/response handling to the wrapped handler.
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 ¶
func WithRecover(message string, handler func(ctx *gin.Context)) gin.HandlerFunc
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 HandlerFunc ¶
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
func MiddlewareChain ¶
func MiddlewareChain(middlewares ...MiddlewareFunc) MiddlewareFunc
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.