27 lines
604 B
Go
27 lines
604 B
Go
package reqctx
|
|
|
|
import "context"
|
|
|
|
type ctxKey struct{}
|
|
|
|
// RequestContext is the per-request identity. Use RealIP for rate limit and audit, never 127.0.0.1 from gnet.RemoteAddr.
|
|
type RequestContext struct {
|
|
TraceID string
|
|
UserID int64
|
|
DeviceID uint64
|
|
Language string
|
|
AppVersion string
|
|
ConnectionID string
|
|
RemoteAddr string
|
|
RealIP string
|
|
}
|
|
|
|
func With(ctx context.Context, rc *RequestContext) context.Context {
|
|
return context.WithValue(ctx, ctxKey{}, rc)
|
|
}
|
|
|
|
func From(ctx context.Context) *RequestContext {
|
|
rc, _ := ctx.Value(ctxKey{}).(*RequestContext)
|
|
return rc
|
|
}
|