apix

package
v0.0.0-...-a89d057 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorKey = "error_g"
)

Variables

This section is empty.

Functions

func Bind

func Bind(ctx *gin.Context, req interface{}) (err *errors.Error)

Bind parses and validates request payload into a target struct pointer.

func BindJSON

func BindJSON[T any](ctx *gin.Context) (*T, *errors.Error)

BindJSON binds JSON body into a new struct *T.

func BindParams

func BindParams(ctx *gin.Context, req interface{}, noPrint ...bool) (err *errors.Error)

BindParams binds and validates request data into req struct pointer.

func BindQuery

func BindQuery[T any](ctx *gin.Context) (*T, *errors.Error)

BindQuery binds URL query parameters into a new struct *T.

func BindReq

func BindReq[T any](ctx *gin.Context, noPrint ...bool) (*T, *errors.Error)

BindReq is a generic constructor and binder: allocates a new struct T, binds, validates, and returns *T.

func BindURI

func BindURI[T any](ctx *gin.Context) (*T, *errors.Error)

BindURI binds URI route parameters into a new struct *T.

func EnsureTraceID

func EnsureTraceID(ctx *gin.Context) string

EnsureTraceID retrieves the trace ID or generates and sets a new one if missing.

func GetBearerToken

func GetBearerToken(ctx *gin.Context) string

GetBearerToken extracts Bearer token from Authorization header or 'token' query/header parameter.

func GetClientIP

func GetClientIP(ctx *gin.Context) string

GetClientIP gets client IP address checking headers with fallback.

func GetContextVal

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

GetContextVal safely retrieves a typed value from Gin Context.

func GetFullURL

func GetFullURL(ctx *gin.Context) string

GetFullURL returns the full request URL string including scheme and host.

func GetHeader

func GetHeader(ctx *gin.Context, key string, defValue ...string) string

GetHeader safely retrieves a header value with optional default.

func GetHost

func GetHost(ctx *gin.Context) string

GetHost returns the request host.

func GetPageReq

func GetPageReq(ctx *gin.Context) (pageReq *load.Page, err *errors.Error)

GetPageReq parses standard pagination parameters (page, size, lastID) from request.

func GetParam

func GetParam(ctx *gin.Context, key string, defValue ...string) (value interface{}, err *errors.Error)

GetParam retrieves a raw parameter.

func GetParamArray

func GetParamArray[t any](ctx *gin.Context, key string, force Forcible, defValue ...[]t) (value []t, err *errors.Error)

GetParamArray retrieves an array parameter.

func GetParamBool

func GetParamBool(ctx *gin.Context, key string, force Forcible, defValue ...bool) (value bool, err *errors.Error)

GetParamBool retrieves a bool parameter.

func GetParamFloat64

func GetParamFloat64(ctx *gin.Context, key string, force Forcible, defValue ...float64) (value float64, err *errors.Error)

GetParamFloat64 retrieves a float64 parameter.

func GetParamForce

func GetParamForce(ctx *gin.Context, key string) (value string, err *errors.Error)

GetParamForce retrieves a required string parameter.

func GetParamInt

func GetParamInt(ctx *gin.Context, key string, force Forcible, defValue ...int) (value int, err *errors.Error)

GetParamInt retrieves an int parameter.

func GetParamInt64

func GetParamInt64(ctx *gin.Context, key string, force Forcible, defValue ...int64) (value int64, err *errors.Error)

GetParamInt64 retrieves an int64 parameter.

func GetParamInt64Slice

func GetParamInt64Slice(ctx *gin.Context, key string, force Forcible, defValue ...[]int64) (value []int64, err *errors.Error)

GetParamInt64Slice retrieves an []int64 parameter (supporting comma separated strings).

func GetParamStr

func GetParamStr(ctx *gin.Context, key string, defValue ...string) (value string, err *errors.Error)

GetParamStr retrieves a string parameter.

func GetParamType

func GetParamType[t ParamType](ctx *gin.Context, key string, force Forcible, defValue ...t) (value t, err *errors.Error)

GetParamType retrieves a parameter conforming to ParamType.

func GetParams

func GetParams(ctx *gin.Context, requestType ...RequestType) map[string]interface{}

GetParams extracts and merges all request parameters (URL query, form, and JSON body).

func GetScheme

func GetScheme(ctx *gin.Context) string

GetScheme returns http or https based on connection/proxy headers.

func GetTraceID

func GetTraceID(ctx *gin.Context) string

GetTraceID extracts the Trace ID from Gin Context, Request Context, or HTTP headers.

func GetUserID

func GetUserID(ctx *gin.Context) string

GetUserID returns the User ID string from context.

func GetUserIDInt

func GetUserIDInt(ctx *gin.Context) int

GetUserIDInt returns the User ID as int.

func GetUserIDInt64

func GetUserIDInt64(ctx *gin.Context) int64

GetUserIDInt64 returns the User ID as int64.

func GetUserInfo

func GetUserInfo(ctx *gin.Context) map[string]interface{}

GetUserInfo returns the UserInfo map from context.

func GetUserInfoAs

func GetUserInfoAs[T any](ctx *gin.Context) (*T, error)

GetUserInfoAs converts user info into a typed struct pointer.

func GetUserInfoValue

func GetUserInfoValue(ctx *gin.Context, key string) any

GetUserInfoValue gets a specific field from UserInfo map.

func GetValidator

func GetValidator() *validator.Validate

GetValidator returns the global validator instance (defaults to binding tag).

func Handle

func Handle(ctx *gin.Context, code int, err error)

Handle renders success (S) when err is nil, or handles the error when non-nil.

func HandleData

func HandleData(ctx *gin.Context, code int, data interface{}, err error)

HandleData renders success with data when err is nil, or handles the error when non-nil.

func HandleError

func HandleError(ctx *gin.Context, code int, data any, err error)

HandleError handles errors, distinguishes between System & Application errors, and renders the JSON response.

func HandleOk

func HandleOk(ctx *gin.Context)

HandleOk renders standard success response.

func HandlePage

func HandlePage[T any](ctx *gin.Context, code int, pageResp *load.PageRespT[T], err error)

HandlePage renders paginated response or handles error.

func HandlePanic

func HandlePanic(ctx *gin.Context)

HandlePanic recovers from panics, logs stack traces, and renders 500 JSON response.

func HandleResult

func HandleResult[T any](ctx *gin.Context, code int, data T, err error)

HandleResult is a generic helper for standard handler returns.

func NewCtx

func NewCtx() *gin.Context

NewCtx creates an isolated gin.Context for unit testing or offline execution.

func PanicNotify

func PanicNotify(ctx *gin.Context, err interface{})

PanicNotify logs and sends panic alert notifications.

func Param

func Param[T any](ctx *gin.Context, key string, defValue ...T) (T, *errors.Error)

Param retrieves a parameter by key converted to type T with optional default value.

func ParamReq

func ParamReq[T any](ctx *gin.Context, key string) (T, *errors.Error)

ParamReq retrieves a required parameter by key converted to type T, triggering validation error if missing.

func ParamSlice

func ParamSlice[T any](ctx *gin.Context, key string, defValue ...[]T) ([]T, *errors.Error)

ParamSlice retrieves a slice parameter by key with optional default.

func ParamSliceReq

func ParamSliceReq[T any](ctx *gin.Context, key string) ([]T, *errors.Error)

ParamSliceReq retrieves a required slice parameter by key, triggering validation error if missing.

func PutParams

func PutParams(ctx *gin.Context, params map[string]interface{})

PutParams caches parsed parameters in Gin context.

func RegisterErrorHook

func RegisterErrorHook(h ErrorHandler)

RegisterErrorHook adds a custom system error notification hook.

func RegisterPanicHook

func RegisterPanicHook(h PanicHandler)

RegisterPanicHook adds a custom panic notification hook.

func RegisterValidation

func RegisterValidation(tag string, fn validator.Func) error

RegisterValidation registers a custom validation function to both binding and validate validators.

func SendError

func SendError(ctx *gin.Context, code int, message string, err ...error)

SendError logs error and dispatches notifications.

func SetContextVal

func SetContextVal[T any](ctx *gin.Context, key string, val T)

SetContextVal sets a typed value into Gin Context.

func SetTraceID

func SetTraceID(ctx *gin.Context, customTraceID ...string) string

SetTraceID sets a new or given Trace ID into both Gin Context and standard Request Context.

func SetUserID

func SetUserID(ctx *gin.Context, userID string)

SetUserID sets the User ID into both Gin Context and Request Context.

func SetUserInfo

func SetUserInfo(ctx *gin.Context, userInfo map[string]interface{})

SetUserInfo sets the UserInfo map into context.

func ValidateStruct

func ValidateStruct(obj interface{}) *errors.Error

ValidateStruct validates a struct using validation tags (supports both binding:"..." and validate:"...").

func ValidateVar

func ValidateVar(field any, tag string) *errors.Error

ValidateVar validates a single variable against a validator tag rule (e.g. "required,email").

Types

type ErrorHandler

type ErrorHandler func(ctx *gin.Context, err error, stack string)

ErrorHandler is a callback hook invoked when a system error occurs.

type Forcible

type Forcible bool

Forcible indicates whether a missing parameter triggers a validation error response.

const (
	Force    Forcible = true
	NotForce Forcible = false
)

type PanicHandler

type PanicHandler func(ctx *gin.Context, err any, stack string)

PanicHandler is a callback hook invoked when a panic occurs.

type ParamType

type ParamType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64 | ~string | ~bool | ~uintptr
}

ParamType defines supported primitive constraint types for generic parameter extraction.

type RequestType

type RequestType string
const (
	Get      RequestType = "Get"
	PostForm RequestType = "PostForm"
	PostBody RequestType = "PostBody"
)

type ValueExtractor

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

ValueExtractor provides a fluent interface for extracting single parameter values.

func Value

func Value(ctx *gin.Context, key string) ValueExtractor

Value creates a fluent ValueExtractor for a parameter key.

func (ValueExtractor) Bool

func (e ValueExtractor) Bool(def ...bool) bool

Bool returns parameter as bool or fallback default.

func (ValueExtractor) Float64

func (e ValueExtractor) Float64(def ...float64) float64

Float64 returns parameter as float64 or fallback default.

func (ValueExtractor) Int

func (e ValueExtractor) Int(def ...int) int

Int returns parameter as int or fallback default.

func (ValueExtractor) Int64

func (e ValueExtractor) Int64(def ...int64) int64

Int64 returns parameter as int64 or fallback default.

func (ValueExtractor) String

func (e ValueExtractor) String(def ...string) string

String returns parameter as string or fallback default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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