ctxutil

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DefaultAsyncTimeout is the default timeout for async operations
	DefaultAsyncTimeout = 5 * time.Second
)
View Source
const (
	TraceIDKey = "trace_id"
)

Variables

This section is empty.

Functions

func EnsureTraceID

func EnsureTraceID(ctx context.Context) (context.Context, string)

EnsureTraceID ensures that a trace ID exists in the context.

func ExtractContext

func ExtractContext(payload *map[string]any) context.Context

ExtractContext extracts context from payload map safely

func FromGinContext

func FromGinContext(c *gin.Context) context.Context

FromGinContext extracts the context.Context from *gin.Context.

func GenerateBusinessCode

func GenerateBusinessCode(identifier string) string

GenerateBusinessCode generate business code

format: Code+YYYYMM+AntifakeCODE+Serial, eg: CO2009110GA0001

func GetAcceptLanguage

func GetAcceptLanguage(ctx context.Context) string

GetAcceptLanguage gets Accept-Language header from context

func GetClientIP

func GetClientIP(ctx context.Context) string

GetClientIP gets client IP from context.Context

func GetClientInfo

func GetClientInfo(ctx context.Context) (ip, userAgent, sessionID string)

GetClientInfo gets all client information from context.Context

func GetConfig

func GetConfig(ctx context.Context) *config.Config

GetConfig gets config from context.Context.

func GetContentType

func GetContentType(ctx context.Context) string

GetContentType gets Content-Type header from context

func GetEmailSender

func GetEmailSender(ctx context.Context) (email.Sender, error)

GetEmailSender gets email sender from context.Context based on the configured provider

func GetGinContext

func GetGinContext(ctx context.Context) (*gin.Context, bool)

GetGinContext extracts *gin.Context from context.Context if it exists.

func GetHTTPRequest

func GetHTTPRequest(ctx context.Context) *http.Request

GetHTTPRequest gets HTTP request from context.Context

func GetHost

func GetHost(ctx context.Context) string

GetHost gets Host header from context

func GetProfile

func GetProfile(ctx context.Context) any

GetProfile gets profile from context.Context.

func GetProvider

func GetProvider(ctx context.Context) string

GetProvider gets provider from context.Context.

func GetQueryParams

func GetQueryParams(ctx context.Context) map[string]string

GetQueryParams gets all query parameters from context

func GetReferer

func GetReferer(ctx context.Context) string

GetReferer gets HTTP referer from context

func GetRemoteAddr

func GetRemoteAddr(ctx context.Context) string

GetRemoteAddr gets remote address from context

func GetRequestHeaders

func GetRequestHeaders(ctx context.Context) map[string]string

GetRequestHeaders gets all request headers from context

func GetRequestMethod

func GetRequestMethod(ctx context.Context) string

GetRequestMethod gets HTTP method from context

func GetRequestURI

func GetRequestURI(ctx context.Context) string

GetRequestURI gets request URI from context

func GetRequestURL

func GetRequestURL(ctx context.Context) string

GetRequestURL gets full request URL from context

func GetSessionID

func GetSessionID(ctx context.Context) string

GetSessionID gets session ID from context.Context

func GetSpaceID

func GetSpaceID(ctx context.Context) string

GetSpaceID gets space id from context.Context.

func GetStorage

func GetStorage(ctx context.Context) (oss.Interface, *oss.Config)

func GetToken

func GetToken(ctx context.Context) string

GetToken gets token from context.Context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID gets trace id from context.Context or gin.Context.

func GetUserAgent

func GetUserAgent(ctx context.Context) string

GetUserAgent gets user agent from context.Context

func GetUserEmail

func GetUserEmail(ctx context.Context) string

GetUserEmail gets user email from context.Context.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID gets user id from context.Context.

func GetUserIsAdmin

func GetUserIsAdmin(ctx context.Context) bool

GetUserIsAdmin gets user admin status from context.Context.

func GetUserIsCertified

func GetUserIsCertified(ctx context.Context) bool

GetUserIsCertified gets user is certified from context.Context.

func GetUserPermissions

func GetUserPermissions(ctx context.Context) []string

GetUserPermissions gets user permissions from context.Context.

func GetUserRoles

func GetUserRoles(ctx context.Context) []string

GetUserRoles gets user roles from context.Context.

func GetUserSpaceIDs

func GetUserSpaceIDs(ctx context.Context) []string

GetUserSpaceIDs gets user space IDs from context.Context.

func GetUserStatus

func GetUserStatus(ctx context.Context) int

GetUserStatus gets user status from context.Context.

func GetUsername

func GetUsername(ctx context.Context) string

GetUsername gets username from context.Context.

func GetValue

func GetValue(ctx context.Context, key string) any

GetValue retrieves a value from the context.

func IsAjaxRequest

func IsAjaxRequest(ctx context.Context) bool

IsAjaxRequest checks if request is an AJAX request

func IsSecureRequest

func IsSecureRequest(ctx context.Context) bool

IsSecureRequest checks if request is HTTPS

func SendEmailWithTemplate

func SendEmailWithTemplate(ctx context.Context, recipientEmail string, template email.Template) (string, error)

SendEmailWithTemplate sends an email with a template

func SetClientIP

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

SetClientIP sets client IP to context.Context

func SetClientInfo

func SetClientInfo(ctx context.Context, ip, userAgent, sessionID string) context.Context

SetClientInfo sets client information to context.Context

func SetConfig

func SetConfig(ctx context.Context, conf *config.Config) context.Context

SetConfig sets config to context.Context.

func SetEmailSender

func SetEmailSender(ctx context.Context, sender email.Sender) context.Context

SetEmailSender sets email sender to context.Context

func SetHTTPRequest

func SetHTTPRequest(ctx context.Context, req *http.Request) context.Context

SetHTTPRequest sets HTTP request to context.Context

func SetProfile

func SetProfile(ctx context.Context, profile any) context.Context

SetProfile sets profile to context.Context.

func SetProvider

func SetProvider(ctx context.Context, provider string) context.Context

SetProvider sets provider to context.Context.

func SetReferer

func SetReferer(ctx context.Context, referer string) context.Context

SetReferer sets HTTP referer in context

func SetSessionID

func SetSessionID(ctx context.Context, sessionID string) context.Context

SetSessionID sets session ID to context.Context

func SetSpaceID

func SetSpaceID(ctx context.Context, uid string) context.Context

SetSpaceID sets space id to context.Context.

func SetStorage

func SetStorage(ctx context.Context, s oss.Interface) context.Context

func SetToken

func SetToken(ctx context.Context, token string) context.Context

SetToken sets token to context.Context.

func SetTraceID

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

SetTraceID sets trace id to context.Context and gin.Context if available.

func SetUserAgent

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

SetUserAgent sets user agent to context.Context

func SetUserEmail

func SetUserEmail(ctx context.Context, email string) context.Context

SetUserEmail sets user email to context.Context.

func SetUserID

func SetUserID(ctx context.Context, uid string) context.Context

SetUserID sets user id to context.Context.

func SetUserIsAdmin

func SetUserIsAdmin(ctx context.Context, isAdmin bool) context.Context

SetUserIsAdmin sets user admin status to context.Context.

func SetUserIsCertified

func SetUserIsCertified(ctx context.Context, isCertified bool) context.Context

SetUserIsCertified sets user is certified to context.Context.

func SetUserPermissions

func SetUserPermissions(ctx context.Context, permissions []string) context.Context

SetUserPermissions sets user permissions to context.Context.

func SetUserRoles

func SetUserRoles(ctx context.Context, roles []string) context.Context

SetUserRoles sets user roles to context.Context.

func SetUserSpaceIDs

func SetUserSpaceIDs(ctx context.Context, spaceIDs []string) context.Context

SetUserSpaceIDs sets user space IDs to context.Context.

func SetUserStatus

func SetUserStatus(ctx context.Context, status int) context.Context

SetUserStatus sets user status to context.Context.

func SetUsername

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

SetUsername sets username to context.Context.

func SetValue

func SetValue(ctx context.Context, key string, val any) context.Context

SetValue sets a value to the context.

func WithAsyncContext added in v0.1.21

func WithAsyncContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

WithAsyncContext creates a context suitable for async operations It derives from the parent context to preserve trace information, but uses a separate timeout to avoid blocking the main request

func WithAsyncContextDefault added in v0.1.21

func WithAsyncContextDefault(parent context.Context) (context.Context, context.CancelFunc)

WithAsyncContextDefault creates an async context with default timeout

func WithGinContext

func WithGinContext(ctx context.Context, c *gin.Context) context.Context

WithGinContext returns a context.Context that embeds the *gin.Context.

Types

type UserAgentInfo

type UserAgentInfo struct {
	Browser  string `json:"browser"`
	Version  string `json:"version"`
	OS       string `json:"os"`
	Platform string `json:"platform"`
	Mobile   bool   `json:"mobile"`
}

UserAgentInfo represents parsed user agent information

func GetParsedUserAgent

func GetParsedUserAgent(ctx context.Context) *UserAgentInfo

GetParsedUserAgent gets parsed user agent information from context

func ParseUserAgent

func ParseUserAgent(userAgent string) *UserAgentInfo

ParseUserAgent parses user agent string into structured information

Jump to

Keyboard shortcuts

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