ginx

package
v0.0.2-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbortWithJsonError

func AbortWithJsonError(ctx *gin.Context, err error)

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

func Close(ctx context.Context, server *http.Server) error

Close gracefully shuts down the HTTP server using the provided context. It handles nil server gracefully and returns any shutdown error.

func EndpointsToMatches

func EndpointsToMatches(base string, endpoints ...[][3]string) map[string]map[string]string

func Fs

func Fs(local string, emFs *embed.FS, emPath string) (http.FileSystem, error)

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 MatchOperation(base string, endpoints [][3]string, operations ...string) func(ctx *gin.Context) bool

func ParseError

func ParseError(err error) (code int32, status int32, message string)

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

func ShouldBind(ctx *gin.Context, obj any) error

ShouldBind binds form data to the given object using Gin's default binding.

func ShouldBindHeader

func ShouldBindHeader(ctx *gin.Context, obj any) error

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

func ShouldBindJSON(ctx *gin.Context, obj any) error

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

func ShouldBindQuery(ctx *gin.Context, obj any) error

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

func ShouldBindUri(ctx *gin.Context, obj any) error

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

func ShouldUniverseBind(ctx *gin.Context, obj any, uri, query, body bool) error

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

func ShouldUniverseBindForm(ctx *gin.Context, obj any) error

ShouldUniverseBindForm binds form data using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.

func ShouldUniverseBindQuery

func ShouldUniverseBindQuery(ctx *gin.Context, obj any) error

ShouldUniverseBindQuery binds query parameters using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.

func ShouldUniverseBindUri

func ShouldUniverseBindUri(ctx *gin.Context, obj any) error

ShouldUniverseBindUri binds URI parameters using a custom form decoder that supports both standard struct tags and protobuf name resolution for field mapping.

func Start

func Start(server *http.Server) error

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

func Value[T any](ctx *gin.Context, key string) (T, bool)

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

func WithHandler(h http.Handler) func(ctx *gin.Context)

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

func WithJson[T any](handler func(ctx *gin.Context) (T, error)) gin.HandlerFunc

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.

func WithText

func WithText(handler func(ctx *gin.Context) (string, error)) gin.HandlerFunc

WithText creates a Gin handler that returns plain text responses. It handles errors by calling AbortWithJsonError and returns successful string responses with HTTP 200 status.

Types

type Context

type Context = gin.Context

Context is a type alias for gin.Context for convenience.

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

type ErrorParser func(error) (int32, int32, string)

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 HandlerFunc func(ctx *gin.Context) error

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.

Jump to

Keyboard shortcuts

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