feat: add audit request info context helpers

This commit is contained in:
2026-05-26 16:14:02 +02:00
parent fa91c2a22d
commit 1feee731cf

View File

@@ -0,0 +1,36 @@
package auditctx
import "context"
type ctxKey string
const (
ctxKeyIP ctxKey = "audit_ip"
ctxKeyUserAgent ctxKey = "audit_user_agent"
)
func WithRequestInfo(ctx context.Context, ip string, userAgent string) context.Context {
if ctx == nil {
return nil
}
next := context.WithValue(ctx, ctxKeyIP, ip)
return context.WithValue(next, ctxKeyUserAgent, userAgent)
}
func RequestInfoFromContext(ctx context.Context) (ip string, userAgent string) {
if ctx == nil {
return "", ""
}
if v := ctx.Value(ctxKeyIP); v != nil {
if s, ok := v.(string); ok {
ip = s
}
}
if v := ctx.Value(ctxKeyUserAgent); v != nil {
if s, ok := v.(string); ok {
userAgent = s
}
}
return ip, userAgent
}