Files
juwan-backend/app/users/api/internal/contextx/contextx.go
T
2026-02-27 05:42:13 +08:00

55 lines
1.3 KiB
Go

package contextx
import (
"context"
"errors"
)
func WithRequestId(c context.Context, requestId string) context.Context {
return context.WithValue(c, "request_id", requestId)
}
func RequestIdFrom(c context.Context) (string, error) {
requestID, ok := c.Value("request_id").(string)
if !ok {
return "", errors.New("request_id not found in context")
}
return requestID, nil
}
func WithToken(c context.Context, token string) context.Context {
return context.WithValue(c, "token", token)
}
func TokenFrom(c context.Context) (string, error) {
token, ok := c.Value("token").(string)
if !ok {
return "", errors.New("token not found in context")
}
return token, nil
}
func WithUserID(c context.Context, id int64) context.Context {
return context.WithValue(c, "user_id", id)
}
func UserIDFrom(c context.Context) (int64, error) {
if userID, ok := c.Value("user_id").(int64); !ok {
return 0, errors.New("user_id not found in context")
} else {
return userID, nil
}
}
func WithRequestID(c context.Context, requestID string) context.Context {
return context.WithValue(c, "request_id", requestID)
}
func RequestIDFrom(c context.Context) (string, error) {
if requestID, ok := c.Value("request_id").(string); !ok {
return "", errors.New("request_id not found in context")
} else {
return requestID, nil
}
}