scontext

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCORSInfo added in v1.2.4

func GetCORSInfo[T comparable, U any](ctx context.Context) (allowedOrigin string, credentialsAllowed bool, ok bool)

GetCORSInfo retrieves CORS details from the router context. It returns the allowed origin, whether credentials are allowed, and a boolean indicating if the values were set.

func GetCORSInfoFromRequest added in v1.2.4

func GetCORSInfoFromRequest[T comparable, U any](r *http.Request) (allowedOrigin string, credentialsAllowed bool, ok bool)

GetCORSInfoFromRequest is a convenience function to get CORS details from a request.

func GetCORSRequestedHeaders added in v1.2.9

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

GetCORSRequestedHeaders retrieves the requested headers from a CORS preflight request from the context.

func GetCORSRequestedHeadersFromRequest added in v1.2.9

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

GetCORSRequestedHeadersFromRequest is a convenience function to get the requested headers from a request.

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 GetPathParamsFromContext added in v1.2.2

func GetPathParamsFromContext[T comparable, U any](ctx context.Context) (httprouter.Params, bool)

GetPathParamsFromContext extracts the path parameters from the SRouterContext within a context.

func GetPathParamsFromRequest added in v1.2.2

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

GetPathParamsFromRequest is a convenience function to get the path parameters from a request.

func GetRouteTemplateFromContext added in v1.2.2

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

GetRouteTemplateFromContext extracts the route template from the SRouterContext within a context.

func GetRouteTemplateFromRequest added in v1.2.2

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

GetRouteTemplateFromRequest is a convenience function to get the route template 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 WithCORSInfo added in v1.2.4

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

WithCORSInfo adds CORS details (allowed origin, credentials allowed) to the context.

func WithCORSRequestedHeaders added in v1.2.9

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

WithCORSRequestedHeaders adds the requested headers from a CORS preflight request to the context.

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 WithRouteInfo added in v1.2.2

func WithRouteInfo[T comparable, U any](ctx context.Context, params httprouter.Params, routeTemplate string) context.Context

WithRouteInfo adds route information (path parameters and route template) to the context. This is called by the router when a route is matched.

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

	// Route information
	RouteTemplate string
	PathParams    httprouter.Params

	// CORS information determined by middleware
	AllowedOrigin      string
	CredentialsAllowed bool
	RequestedHeaders   string // Stores the requested headers from CORS preflight requests

	UserIDSet             bool
	UserSet               bool
	ClientIPSet           bool
	TraceIDSet            bool
	TransactionSet        bool
	RouteTemplateSet      bool
	AllowedOriginSet      bool
	CredentialsAllowedSet bool
	RequestedHeadersSet   bool // Flag for RequestedHeaders

	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