19 lines
450 B
Go
19 lines
450 B
Go
|
|
package main
|
||
|
|
|
||
|
|
import "context"
|
||
|
|
|
||
|
|
type contextKey string
|
||
|
|
|
||
|
|
const userIDKey contextKey = "userID"
|
||
|
|
|
||
|
|
// withUserID adds the user ID to the request context
|
||
|
|
func withUserID(ctx context.Context, userID string) context.Context {
|
||
|
|
return context.WithValue(ctx, userIDKey, userID)
|
||
|
|
}
|
||
|
|
|
||
|
|
// getUserID extracts the user ID from the request context
|
||
|
|
func getUserID(ctx context.Context) (string, bool) {
|
||
|
|
userID, ok := ctx.Value(userIDKey).(string)
|
||
|
|
return userID, ok
|
||
|
|
}
|