util

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReqBodyKey        = "req-body"
	ResBodyKey        = "res-body"
	TreePathDelimiter = "."
)

Variables

View Source
var ClearGatewayConfigCacheFunc func()

ClearGatewayConfigCacheFunc clears in-process gateway config cache (set by resource/biz).

Functions

func ClearGatewayConfigCache

func ClearGatewayConfigCache()

ClearGatewayConfigCache clears the gateway config cache via registered func.

func ConvertTimestampToTime

func ConvertTimestampToTime(timestamp string) *time.Time

ConvertTimestampToTime converts a string timestamp in milliseconds to a time.Time object

func ConvertToFloat

func ConvertToFloat(value string) float64

func Exists

func Exists(ctx context.Context, db *gorm.DB) (bool, error)

func FindOne

func FindOne(ctx context.Context, db *gorm.DB, opts QueryOptions, out interface{}) (bool, error)

func FindPage

func FindPage(ctx context.Context, db *gorm.DB, pp PaginationParam, opts QueryOptions, out interface{}) (int64, error)

func FromClientIP

func FromClientIP(ctx context.Context) string

func FromIsRootUser

func FromIsRootUser(ctx context.Context) bool

func FromRowLock

func FromRowLock(ctx context.Context) bool

func FromTenant

func FromTenant(ctx context.Context) string

func FromTraceID

func FromTraceID(ctx context.Context) string

func FromTrans

func FromTrans(ctx context.Context) (*gorm.DB, bool)

func FromUserAgent

func FromUserAgent(ctx context.Context) string

func FromUserID

func FromUserID(ctx context.Context) string

func FromUserToken

func FromUserToken(ctx context.Context) string

func FromUsername

func FromUsername(ctx context.Context) string

func GetBodyData

func GetBodyData(c *gin.Context) []byte

Get body data from context

func GetDB

func GetDB(ctx context.Context, defDB *gorm.DB) *gorm.DB

func GetToken

func GetToken(c *gin.Context) string

Get access token from header or query parameter

func IsEmptyOrBlank

func IsEmptyOrBlank(value string) bool

IsEmptyOrBlank The function is used to check if a string is empty or if it contains only whitespace characters.

func IsNilOrEmpty

func IsNilOrEmpty(value *string) bool

IsNilOrEmpty The function is used to check if a string pointer is nil or if the string it points to is empty.

func MustNewUUID

func MustNewUUID() string

The function generates a new UUID and panics if there is an error.

func NewClientIP

func NewClientIP(ctx context.Context, ip string) context.Context

func NewIsRootUser

func NewIsRootUser(ctx context.Context) context.Context

func NewRowLock

func NewRowLock(ctx context.Context) context.Context

func NewTenant

func NewTenant(ctx context.Context, tenant string) context.Context

func NewTraceID

func NewTraceID(ctx context.Context, traceID string) context.Context

func NewTrans

func NewTrans(ctx context.Context, db *gorm.DB) context.Context

func NewUserAgent

func NewUserAgent(ctx context.Context, userAgent string) context.Context

func NewUserCache

func NewUserCache(ctx context.Context, userCache UserCache) context.Context

func NewUserID

func NewUserID(ctx context.Context, userID string) context.Context

func NewUserToken

func NewUserToken(ctx context.Context, userToken string) context.Context

func NewUsername

func NewUsername(ctx context.Context, username string) context.Context

func NewXID

func NewXID() string

The function "NewXID" generates a new unique identifier (XID) and returns it as a string.

func NotifyConfigChanged

func NotifyConfigChanged(ctx context.Context, kind ConfigChangeKind, keys ...string)

NotifyConfigChanged invokes all listeners. Never panics on listener failure.

func OnConfigChanged

func OnConfigChanged(fn ConfigChangeListener)

OnConfigChanged registers a listener. Safe for concurrent use.

func ParseDurationString

func ParseDurationString(durationStr string) (time.Duration, error)

ParseDurationString 将类似 "1s", "1m", "1h", "1d", "5w", "6y" 的字符串转换为 time.Duration

func ParseForm

func ParseForm(c *gin.Context, obj interface{}) error

Parse body form data to struct

func ParseJSON

func ParseJSON(c *gin.Context, obj interface{}) error

Parse body json data to struct

func ParseQuery

func ParseQuery(c *gin.Context, obj interface{}) error

Parse query parameter to struct

func RandomizedIPAddr

func RandomizedIPAddr() string

The RandomizedIPAddr function generates a random IP address.

func ResError

func ResError(c *gin.Context, err error, status ...int)

func ResJSON

func ResJSON(c *gin.Context, status int, v interface{})

Response json data with status code

func ResOK

func ResOK(c *gin.Context)

func ResPage

func ResPage(c *gin.Context, v interface{}, pr *PaginationResult)

func ResSuccess

func ResSuccess(c *gin.Context, v interface{})

func ResetConfigChangeListeners

func ResetConfigChangeListeners()

ResetConfigChangeListeners clears all listeners (tests / shutdown).

func Run

func Run(ctx context.Context, handler func(ctx context.Context) (func(), error)) error

The Run function sets up a signal handler and executes a handler function until a termination signal is received.

func SoftDelete

func SoftDelete(ctx context.Context, db *gorm.DB, id string) error

SoftDelete performs a logical delete by setting deleted to the record's id and deleted_at to the current time. The db parameter should already include the model scope (e.g., from GetXxxDB) with the deleted='0' filter applied. This ensures consistency across all tables that follow the deleted/deleted_at convention.

func String

func String(s *string) string

String The function is used to create a string from a string pointer.

func StringPtr

func StringPtr(s string) *string

StringPtr The function is used to create a string pointer from a string.

func TimeConvert

func TimeConvert(startTimeStr string, endTimeStr string) (time.Time, time.Time, error)

Types

type ConfigChangeKind

type ConfigChangeKind = string

ConfigChangeKind identifies which gateway runtime data changed. Values: "endpoints" | "policies" | "apikeys" | "all"

const (
	ConfigChangeEndpoints ConfigChangeKind = "endpoints"
	ConfigChangePolicies  ConfigChangeKind = "policies"
	ConfigChangeAPIKeys   ConfigChangeKind = "apikeys"
	ConfigChangeAll       ConfigChangeKind = "all"
)

type ConfigChangeListener

type ConfigChangeListener func(ctx context.Context, kind ConfigChangeKind, keys ...string)

ConfigChangeListener is invoked after admin mutations that affect gateway runtime. Admin never imports gateway; hosts (e.g. tokenlive-standalone) register listeners.

type Direction

type Direction string
const (
	ASC  Direction = "ASC"
	DESC Direction = "DESC"
)

type OrderByParam

type OrderByParam struct {
	Field     string
	Direction Direction
}

type OrderByParams

type OrderByParams []OrderByParam

func (OrderByParams) ToSQL

func (a OrderByParams) ToSQL() string

type PaginationParam

type PaginationParam struct {
	Pagination bool `form:"-"`
	OnlyCount  bool `form:"-"`
	Current    int  `form:"current"`
	PageSize   int  `form:"pageSize" binding:"max=1000"`
}

type PaginationResult

type PaginationResult struct {
	Total    int64 `json:"total"`
	Current  int   `json:"current"`
	PageSize int   `json:"pageSize"`
}

func WrapPageQuery

func WrapPageQuery(ctx context.Context, db *gorm.DB, pp PaginationParam, opts QueryOptions, out interface{}) (*PaginationResult, error)

type QueryOptions

type QueryOptions struct {
	SelectFields []string
	OmitFields   []string
	OrderFields  OrderByParams
}

type ResponseResult

type ResponseResult struct {
	Success bool          `json:"success"`
	Data    interface{}   `json:"data,omitempty"`
	Total   int64         `json:"total,omitempty"`
	Error   *errors.Error `json:"error,omitempty"`
}

type Trans

type Trans struct {
	DB *gorm.DB
}

func (*Trans) Exec

func (a *Trans) Exec(ctx context.Context, fn TransFunc) error

type TransFunc

type TransFunc func(context.Context) error

type UserCache

type UserCache struct {
	RoleIDs  []string `json:"rids"`
	Username string   `json:"username"`
	Tenant   string   `json:"tenant"`
}

Set user cache object

func FromUserCache

func FromUserCache(ctx context.Context) UserCache

func ParseUserCache

func ParseUserCache(s string) UserCache

func (UserCache) String

func (a UserCache) String() string

Jump to

Keyboard shortcuts

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