ctxutil

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

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 added in v0.1.2

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 added in v0.1.4

func GetAcceptLanguage(ctx context.Context) string

GetAcceptLanguage gets Accept-Language header from context

func GetClientIP added in v0.1.4

func GetClientIP(ctx context.Context) string

GetClientIP gets client IP from context.Context

func GetClientInfo added in v0.1.4

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 added in v0.1.4

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 added in v0.1.4

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

GetHTTPRequest gets HTTP request from context.Context

func GetHost added in v0.1.4

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 added in v0.1.4

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

GetQueryParams gets all query parameters from context

func GetReferer added in v0.1.4

func GetReferer(ctx context.Context) string

GetReferer gets HTTP referer from context

func GetRemoteAddr added in v0.1.4

func GetRemoteAddr(ctx context.Context) string

GetRemoteAddr gets remote address from context

func GetRequestHeaders added in v0.1.4

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

GetRequestHeaders gets all request headers from context

func GetRequestMethod added in v0.1.4

func GetRequestMethod(ctx context.Context) string

GetRequestMethod gets HTTP method from context

func GetRequestURI added in v0.1.4

func GetRequestURI(ctx context.Context) string

GetRequestURI gets request URI from context

func GetRequestURL added in v0.1.4

func GetRequestURL(ctx context.Context) string

GetRequestURL gets full request URL from context

func GetSessionID added in v0.1.4

func GetSessionID(ctx context.Context) string

GetSessionID gets session ID from context.Context

func GetSpaceID added in v0.1.5

func GetSpaceID(ctx context.Context) string

GetSpaceID gets space id from context.Context.

func GetStorage

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

GetStorage gets storage from context.Context

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 added in v0.1.4

func GetUserAgent(ctx context.Context) string

GetUserAgent gets user agent from context.Context

func GetUserEmail added in v0.1.4

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 added in v0.1.4

func GetUserIsAdmin(ctx context.Context) bool

GetUserIsAdmin gets user admin status from context.Context.

func GetUserIsCertified added in v0.1.4

func GetUserIsCertified(ctx context.Context) bool

GetUserIsCertified gets user is certified from context.Context.

func GetUserPermissions added in v0.1.4

func GetUserPermissions(ctx context.Context) []string

GetUserPermissions gets user permissions from context.Context.

func GetUserRoles added in v0.1.4

func GetUserRoles(ctx context.Context) []string

GetUserRoles gets user roles from context.Context.

func GetUserSpaceIDs added in v0.1.5

func GetUserSpaceIDs(ctx context.Context) []string

GetUserSpaceIDs gets user space IDs from context.Context.

func GetUserStatus added in v0.1.4

func GetUserStatus(ctx context.Context) int

GetUserStatus gets user status from context.Context.

func GetUsername added in v0.1.4

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 added in v0.1.4

func IsAjaxRequest(ctx context.Context) bool

IsAjaxRequest checks if request is an AJAX request

func IsSecureRequest added in v0.1.4

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 added in v0.1.4

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

SetClientIP sets client IP to context.Context

func SetClientInfo added in v0.1.4

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 added in v0.1.4

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 added in v0.1.4

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

SetReferer sets HTTP referer in context

func SetSessionID added in v0.1.4

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

SetSessionID sets session ID to context.Context

func SetSpaceID added in v0.1.5

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

SetSpaceID sets space id to context.Context.

func SetStorage

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

SetStorage sets storage to 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 added in v0.1.4

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

SetUserAgent sets user agent to context.Context

func SetUserEmail added in v0.1.4

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 added in v0.1.4

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

SetUserIsAdmin sets user admin status to context.Context.

func SetUserIsCertified added in v0.1.4

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

SetUserIsCertified sets user is certified to context.Context.

func SetUserPermissions added in v0.1.4

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

SetUserPermissions sets user permissions to context.Context.

func SetUserRoles added in v0.1.4

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

SetUserRoles sets user roles to context.Context.

func SetUserSpaceIDs added in v0.1.5

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

SetUserSpaceIDs sets user space IDs to context.Context.

func SetUserStatus added in v0.1.4

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

SetUserStatus sets user status to context.Context.

func SetUsername added in v0.1.4

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 WithGinContext

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

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

Types

type UserAgentInfo added in v0.1.4

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 added in v0.1.4

func GetParsedUserAgent(ctx context.Context) *UserAgentInfo

GetParsedUserAgent gets parsed user agent information from context

func ParseUserAgent added in v0.1.4

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