route/cmd/routed/ctx.go

42 lines
749 B
Go
Raw Normal View History

2018-01-21 16:22:10 +00:00
package main
import (
"context"
"time"
"git.xeserv.us/xena/route/internal/database"
)
type ctxKey int
const (
startTimeKey ctxKey = iota
tokenKey
)
func withStartTime(ctx context.Context) context.Context {
return context.WithValue(ctx, startTimeKey, time.Now())
}
func getStartTime(ctx context.Context) (time.Time, bool) {
t, ok := ctx.Value(startTimeKey).(time.Time)
if !ok {
return time.Time{}, false
}
return t, true
}
func withAuthToken(ctx context.Context, token database.Token) context.Context {
return context.WithValue(ctx, tokenKey, token)
}
func getAuthToken(ctx context.Context) (database.Token, bool) {
t, ok := ctx.Value(tokenKey).(database.Token)
if !ok {
return database.Token{}, false
}
return t, true
}