util

package
v2.6.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

Functions

func ApplyBasePathToMetadataKey

func ApplyBasePathToMetadataKey(key, basePath string) string

ApplyBasePathToMetadataKey applies a basePath prefix to a metadata key (METHOD:path) If basePath is empty, the key is returned unchanged Example: ApplyBasePathToMetadataKey("GET:/auth/me", "/api") returns "GET:/api/auth/me"

func ApplyFieldUpdates

func ApplyFieldUpdates(q *bun.UpdateQuery, fields map[string]any) *bun.UpdateQuery

ApplyFieldUpdates applies map fields to a Bun UpdateQuery safely

func BuildActionURL

func BuildActionURL(baseURL string, basePath string, actionPath string, token string, callbackURL *string) string

BuildActionURL centralizes URL building logic for token-based flows while preserving callback semantics.

func BuildVerificationURL

func BuildVerificationURL(baseURL string, basePath string, token string, callbackURL *string) string

BuildVerificationURL constructs the verification endpoint URL with optional callback.

func CloneRequestContext

func CloneRequestContext(ctx *models.RequestContext) *models.RequestContext

CloneRequestContext creates a detached copy of a RequestContext for use in async operations. This is used for async hooks which execute in background goroutines without the response writer. The returned context uses context.Background() as its base, preventing goroutine leaks, and has a default 5-second timeout for async hook execution.

func CompareStringArrays

func CompareStringArrays(arr1 []string, arr2 []string) bool

func ConvertRouteMetadata

func ConvertRouteMetadata(routes []models.RouteMapping) (map[string]map[string]any, error)

ConvertRouteMetadata converts a list of RouteMapping configs into the internal route metadata map used by the router for plugin routing. Returns a map keyed by "METHOD:path" containing metadata with "plugins" field. Example:

Input: RouteMapping{Path: "/me", Method: "GET", Plugins: ["session.auth"]}
Output: {"GET:/me": {"plugins": ["session.auth"]}}

func CreateMockRequest

func CreateMockRequest(method string, path string, query map[string]string, body io.Reader, headers map[string]string) *http.Request

createMockRequest creates a basic mock HTTP request for testing

func ExtractClientIP

func ExtractClientIP(logger models.Logger, req *http.Request, trustedHeaders []string, trustedProxies []string) (net.IP, error)

ExtractClientIP extracts the client IP address from various sources accurately

func GenerateUUID

func GenerateUUID() string

GenerateUUID generates a new UUID string

func GetQueryBool

func GetQueryBool(r *http.Request, key string, defaultValue bool) bool

GetQueryBool retrieves a query parameter as a bool. Accepts "true", "1", "yes" (case insensitive) as true. Returns defaultValue if missing or invalid.

func GetQueryInt

func GetQueryInt(r *http.Request, key string, defaultValue int) int

GetQueryInt retrieves a query parameter as an int. If missing or invalid, returns the provided default value.

func GetQueryParam

func GetQueryParam(r *http.Request, key string, defaultValue string) string

GetQueryParam retrieves a query parameter from the request. If the parameter is missing, returns the provided default value.

func InitValidator

func InitValidator()

func IsPluginEnabled

func IsPluginEnabled(config *models.Config, pluginID string) bool

IsPluginEnabled checks if a plugin is enabled based on its metadata and configuration.

func JSONResponse

func JSONResponse(w http.ResponseWriter, status int, data any)

func LoadPluginConfig

func LoadPluginConfig[T any](config *models.Config, pluginID string, target *T) error

LoadPluginConfig parses the configuration for a specific plugin from the main config. First checks PreParsedConfigs (for type safety when plugins are instantiated manually). Falls back to unmarshalling from Plugins map (for config file loading).

func MarshalJSON

func MarshalJSON(v any) (json.RawMessage, error)

MarshalJSON marshals a value to JSON

func MaskIP

func MaskIP(ip string) string

MaskIP masks the last octet of an IPv4 address for GDPR compliance Example: "1.2.3.4" -> "1.2.3.x" For IPv6 or other formats, returns the original string unchanged

func NewMockPlugin

func NewMockPlugin() *mockPlugin

func NormalizePath

func NormalizePath(p string) string

func ParseJSON

func ParseJSON(r *http.Request, dest any) error

func ParsePluginConfig

func ParsePluginConfig(source any, target any) error

ParsePluginConfig is a utility function to parse plugin configuration from the generic config map. It uses mapstructure with custom decode hooks to handle: - Time duration strings (e.g., "5m", "300s") via StringToTimeDurationHookFunc - Comma-separated string slices via StringToSliceHookFunc

func PublishEventAsync

func PublishEventAsync(eventBus models.EventBus, logger models.Logger, event models.Event)

PublishEventAsync publishes an event asynchronously without blocking the caller. If the event bus is nil, it safely returns without error. The event is published in a background goroutine with a timeout context. Event publishing failures are logged but do not block or return errors to the caller, treating events as telemetry rather than critical to request success.

func ValidateStruct

func ValidateStruct(s any) error

func ValidateTrustedHeadersAndProxies

func ValidateTrustedHeadersAndProxies(logger models.Logger, trustedHeaders []string, trustedProxies []string)

func ValidateTrustedOrigins

func ValidateTrustedOrigins(trustedOrigins []string) error

ValidateTrustedOrigins validates that all trusted origins are well-formed URLs

Types

type MockLogger

type MockLogger struct {
}

func NewMockLogger

func NewMockLogger() *MockLogger

func (*MockLogger) Debug

func (m *MockLogger) Debug(msg string, args ...any)

func (*MockLogger) Error

func (m *MockLogger) Error(msg string, args ...any)

func (*MockLogger) Info

func (m *MockLogger) Info(msg string, args ...any)

func (*MockLogger) Warn

func (m *MockLogger) Warn(msg string, args ...any)

Jump to

Keyboard shortcuts

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