From 1feee731cfb81c0d41f1ac665ab7e4266397f978 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Tue, 26 May 2026 16:14:02 +0200 Subject: [PATCH] feat: add audit request info context helpers --- internal/auditctx/context.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 internal/auditctx/context.go diff --git a/internal/auditctx/context.go b/internal/auditctx/context.go new file mode 100644 index 0000000..50db67a --- /dev/null +++ b/internal/auditctx/context.go @@ -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 +} +