1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-06 23:46:29 +02:00

55 lines
1005 B
Go
Raw Normal View History

package webcontext
import (
"context"
"time"
"github.com/pkg/errors"
)
// ctxKey represents the type of value for the context key.
type ctxKeyValues int
// KeyValues is how request values or stored/retrieved.
const KeyValues ctxKeyValues = 1
var ErrContextRequired = errors.New("web value missing from context")
// Values represent state for each request.
type Values struct {
Now time.Time
TraceID uint64
SpanID uint64
StatusCode int
Env Env
RequestIP string
}
func ContextValues(ctx context.Context) (*Values, error) {
// If the context is missing this value, request the service
// to be shutdown gracefully.
v, ok := ctx.Value(KeyValues).(*Values)
if !ok {
e := Values{}
return &e, ErrContextRequired
}
return v, nil
}
type Env = string
var (
Env_Dev Env = "dev"
Env_Stage Env = "stage"
Env_Prod Env = "prod"
)
func ContextEnv(ctx context.Context) string {
cv := ctx.Value(KeyValues).(*Values)
if cv != nil {
return cv.Env
}
return ""
}