forked from belak/go-seabird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
43 lines (31 loc) · 1.04 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package seabird
import (
"context"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/belak/go-seabird/internal"
)
const (
contextKeyBot = internal.ContextKey("seabird-bot")
contextKeyLogger = internal.ContextKey("seabird-logger-entry")
contextKeyCurrentNick = internal.ContextKey("seabird-current-nick")
contextKeyRequestID = internal.ContextKey("seabird-request-id")
)
func withSeabirdValues(ctx context.Context, b *Bot, log *logrus.Entry) context.Context {
ctx = context.WithValue(ctx, contextKeyBot, b)
ctx = context.WithValue(ctx, contextKeyLogger, log)
return ctx
}
func CtxBot(ctx context.Context) *Bot {
return ctx.Value(contextKeyBot).(*Bot)
}
func CtxLogger(ctx context.Context, name string) *logrus.Entry {
logger := ctx.Value(contextKeyLogger).(*logrus.Entry)
return logger.WithField("category", name)
}
func CtxCurrentNick(ctx context.Context) string {
return ctx.Value(contextKeyCurrentNick).(string)
}
func CtxRequestID(ctx context.Context) uuid.UUID {
return ctx.Value(contextKeyRequestID).(uuid.UUID)
}