diff --git a/src/request_context/ctx.go b/src/request_context/ctx.go new file mode 100644 index 0000000..2436250 --- /dev/null +++ b/src/request_context/ctx.go @@ -0,0 +1,59 @@ +package request_context + +import ( + "context" + "time" +) + +type RequestContext interface { + context.Context + + RequestId() string + UserId() string +} + +type Opts struct { + RequestId string + UserId string +} + +func New(opts Opts) RequestContext { + return reqCtx{ + requestId: opts.RequestId, + userId: opts.UserId, + } +} + +type reqCtx struct { + ctx context.Context + requestId string + userId string +} + +func (r reqCtx) Deadline() (deadline time.Time, ok bool) { + return r.ctx.Deadline() +} + +func (r reqCtx) Done() <-chan struct{} { + return r.ctx.Done() +} + +func (r reqCtx) Err() error { + return r.ctx.Err() +} + +func (r reqCtx) Value(key any) any { + return r.ctx.Value(key) +} + +func (r reqCtx) String() string { + return r.ctx.Err().Error() +} + +func (r reqCtx) RequestId() string { + return r.requestId +} + +func (r reqCtx) UserId() string { + return r.userId +}