scontext

package
v1.1.17 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClientIP

func GetClientIP[T comparable, U any](ctx context.Context) (string, bool)

GetClientIP retrieves a client IP from the router context

func GetClientIPFromRequest

func GetClientIPFromRequest[T comparable, U any](r *http.Request) (string, bool)

GetClientIPFromRequest is a convenience function to get the client IP from a request

func GetFlag

func GetFlag[T comparable, U any](ctx context.Context, name string) (bool, bool)

GetFlag retrieves a flag from the router context

func GetFlagFromRequest

func GetFlagFromRequest[T comparable, U any](r *http.Request, name string) (bool, bool)

GetFlagFromRequest is a convenience function to get a flag from a request

func GetTraceIDFromContext

func GetTraceIDFromContext[T comparable, U any](ctx context.Context) string

GetTraceIDFromContext extracts the trace ID from the SRouterContext within a context.

func GetTraceIDFromRequest

func GetTraceIDFromRequest[T comparable, U any](r *http.Request) string

GetTraceIDFromRequest is a convenience function to get the trace ID from a request.

func GetUser

func GetUser[T comparable, U any](ctx context.Context) (*U, bool)

GetUser retrieves a user from the router context

func GetUserFromRequest

func GetUserFromRequest[T comparable, U any](r *http.Request) (*U, bool)

GetUserFromRequest is a convenience function to get the user from a request

func GetUserID

func GetUserID[T comparable, U any](ctx context.Context) (T, bool)

GetUserID retrieves a user ID from the router context

func GetUserIDFromRequest

func GetUserIDFromRequest[T comparable, U any](r *http.Request) (T, bool)

GetUserIDFromRequest is a convenience function to get the user ID from a request

func WithClientIP

func WithClientIP[T comparable, U any](ctx context.Context, ip string) context.Context

WithClientIP adds a client IP to the context

func WithFlag

func WithFlag[T comparable, U any](ctx context.Context, name string, value bool) context.Context

WithFlag adds a flag to the context

func WithSRouterContext

func WithSRouterContext[T comparable, U any](ctx context.Context, rc *SRouterContext[T, U]) context.Context

WithSRouterContext adds or updates the router context in the request context

func WithTraceID

func WithTraceID[T comparable, U any](ctx context.Context, traceID string) context.Context

WithTraceID adds a trace ID to the SRouterContext in the provided context.

func WithTransaction

func WithTransaction[T comparable, U any](ctx context.Context, tx DatabaseTransaction) context.Context

WithTransaction adds a database transaction to the context

func WithUser

func WithUser[T comparable, U any](ctx context.Context, user *U) context.Context

WithUser adds a user to the context

func WithUserID

func WithUserID[T comparable, U any](ctx context.Context, userID T) context.Context

WithUserID adds a user ID to the context

Types

type DatabaseTransaction

type DatabaseTransaction interface {
	Commit() error
	Rollback() error
	SavePoint(name string) error
	RollbackTo(name string) error
	GetDB() *gorm.DB
}

DatabaseTransaction defines an interface for essential transaction control methods. This allows mocking transaction behavior for testing purposes. Note: Moved from middleware/db.go to avoid import cycle if db needed context. Consider if this interface truly belongs here or in a db-specific package. For now, placing here to resolve cycle.

func GetTransaction

func GetTransaction[T comparable, U any](ctx context.Context) (DatabaseTransaction, bool)

GetTransaction retrieves a database transaction from the router context

func GetTransactionFromRequest

func GetTransactionFromRequest[T comparable, U any](r *http.Request) (DatabaseTransaction, bool)

GetTransactionFromRequest is a convenience function to get the transaction from a request

type SRouterContext

type SRouterContext[T comparable, U any] struct {
	UserID T
	User   *U

	TraceID string

	ClientIP string

	Transaction DatabaseTransaction

	UserIDSet      bool
	UserSet        bool
	ClientIPSet    bool
	TraceIDSet     bool
	TransactionSet bool

	Flags map[string]bool
}

SRouterContext holds all values that SRouter adds to request contexts. T is the User ID type (comparable), U is the User object type (any).

func EnsureSRouterContext

func EnsureSRouterContext[T comparable, U any](ctx context.Context) (*SRouterContext[T, U], context.Context)

EnsureSRouterContext retrieves or creates a router context

func GetSRouterContext

func GetSRouterContext[T comparable, U any](ctx context.Context) (*SRouterContext[T, U], bool)

GetSRouterContext retrieves the router context from a request context

func NewSRouterContext

func NewSRouterContext[T comparable, U any]() *SRouterContext[T, U]

NewSRouterContext creates a new router context

Jump to

Keyboard shortcuts

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