Documentation
¶
Overview ¶
Package utils provides common utility functions.
Index ¶
- func BuildContext(c echo.Context) context.Context
- func GetRequestID(ctx context.Context) string
- func GetRequestIDFromEcho(c echo.Context) string
- func GetSpanID(ctx context.Context) string
- func GetStartTime(ctx context.Context) time.Time
- func GetTraceID(ctx context.Context) string
- func GetUserID(ctx context.Context) string
- func LoadPepperFromEnv(envKey string) ([]byte, error)
- func WithRequestInfo(ctx context.Context, requestID, userID string) context.Context
- type Clock
- type ContextKey
- type EncryptConfig
- type PasswordHasher
- func (h *PasswordHasher) Hash(plaintext string) (string, error)
- func (h *PasswordHasher) NeedsRehash(storedHash string) (bool, error)
- func (h *PasswordHasher) RehashIfNeeded(storedHash, plaintext string) (newHash string, changed bool, err error)
- func (h *PasswordHasher) TokenSafeNow() int64
- func (h *PasswordHasher) Verify(storedHash, plaintext string) (ok bool, needRehash bool, err error)
- type RealClock
- type TraceContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildContext ¶
BuildContext creates a context.Context with request information from echo.Context.
This function:
- Preserves the original context (including OpenTelemetry span context)
- Adds business-related values (RequestID, UserID, StartTime) to context
- Does NOT inject TraceID/SpanID as they are already available via OpenTelemetry
Usage:
func handler(c echo.Context) error {
ctx := utils.BuildContext(c)
requestID := utils.GetRequestID(ctx)
traceID := utils.GetTraceID(ctx) // From OpenTelemetry
// ...
}
func GetRequestID ¶
GetRequestID returns RequestID from context. The RequestID is set by BuildContext() which extracts it from Echo middleware. Returns empty string if not found.
func GetRequestIDFromEcho ¶ added in v0.0.3
GetRequestIDFromEcho is a convenience function to get RequestID directly from echo.Context. This is useful when you don't have a context.Context but have echo.Context.
func GetSpanID ¶
GetSpanID returns SpanID from context. It extracts from OpenTelemetry span context if available.
func GetStartTime ¶
GetStartTime returns start time from context. The StartTime should be set by BuildContext().
Returns current time if not found in context.
func GetTraceID ¶
GetTraceID returns TraceID from context. It extracts from OpenTelemetry span context if available.
Returns empty string if no valid span context is found.
func GetUserID ¶
GetUserID returns UserID from context. The UserID should be set by BuildContext() which extracts it from echo.Context.
Returns empty string if not found in context.
func LoadPepperFromEnv ¶
LoadPepperFromEnv loads pepper from environment variable.
func WithRequestInfo ¶ added in v0.0.3
WithRequestInfo adds request information to context. This is useful for testing or when you need to manually set request context.
Note: TraceID and SpanID should be managed by OpenTelemetry, not manually set.
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey is the type for context keys (prevents collisions).
const ( // KeyRequestID is the context key for request ID. KeyRequestID ContextKey = "request_id" // KeyUserID is the context key for user ID. KeyUserID ContextKey = "user_id" // KeyStartTime is the context key for request start time. KeyStartTime ContextKey = "start_time" )
type EncryptConfig ¶
type EncryptConfig struct {
// BcryptCost: recommended 10-14; default 12.
BcryptCost int
// EnablePrehash: SHA-256 the password before bcrypt to avoid 72-byte truncation.
EnablePrehash bool
// Pepper: server-side secret (optional). Use Base64 encoded environment variable.
Pepper []byte
}
EncryptConfig holds password hashing configuration.
func DefaultEncryptConfig ¶
func DefaultEncryptConfig() EncryptConfig
DefaultEncryptConfig returns the default encryption configuration.
type PasswordHasher ¶
type PasswordHasher struct {
// contains filtered or unexported fields
}
PasswordHasher handles password hashing with dependency injection.
func NewPasswordHasher ¶
func NewPasswordHasher(config EncryptConfig) *PasswordHasher
NewPasswordHasher creates a new password hasher.
func NewPasswordHasherWithClock ¶
func NewPasswordHasherWithClock(config EncryptConfig, clock Clock) *PasswordHasher
NewPasswordHasherWithClock creates a new password hasher with custom clock.
func (*PasswordHasher) Hash ¶
func (h *PasswordHasher) Hash(plaintext string) (string, error)
Hash generates a bcrypt password hash.
func (*PasswordHasher) NeedsRehash ¶
func (h *PasswordHasher) NeedsRehash(storedHash string) (bool, error)
NeedsRehash checks if a stored hash needs to be re-hashed with current config.
func (*PasswordHasher) RehashIfNeeded ¶
func (h *PasswordHasher) RehashIfNeeded(storedHash, plaintext string) (newHash string, changed bool, err error)
RehashIfNeeded verifies password and re-hashes if needed.
func (*PasswordHasher) TokenSafeNow ¶
func (h *PasswordHasher) TokenSafeNow() int64
TokenSafeNow returns current unix timestamp using injected clock.
type TraceContext ¶
type TraceContext struct {
TraceID string // TraceID from OpenTelemetry span context
SpanID string // SpanID from OpenTelemetry span context
RequestID string // RequestID from Echo middleware (middleware.RequestID)
UserID string // UserID from authenticated user (if available)
StartTime time.Time // Request start time
}
TraceContext contains trace and request information from a request. TraceID and SpanID are extracted from OpenTelemetry context, RequestID is extracted from Echo middleware.
func ExtractTraceContext ¶
func ExtractTraceContext(c echo.Context) *TraceContext
ExtractTraceContext extracts trace and request information from echo.Context.
This function extracts:
- TraceID and SpanID from OpenTelemetry span context (requires otelecho middleware)
- RequestID from Echo middleware (requires middleware.RequestID)
- UserID from echo.Context if authenticated
- StartTime as current time