bouncer/internal/proxy/director/context.go

38 lines
767 B
Go

package director
import (
"context"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
)
type contextKey string
const (
contextKeyProxy contextKey = "proxy"
)
var (
errContextKeyNotFound = errors.New("context key not found")
errUnexpectedContextValue = errors.New("unexpected context value")
)
func withProxy(ctx context.Context, proxy *store.Proxy) context.Context {
return context.WithValue(ctx, contextKeyProxy, proxy)
}
func ctxProxy(ctx context.Context) (*store.Proxy, error) {
value := ctx.Value(contextKeyProxy)
if value == nil {
return nil, errors.WithStack(errContextKeyNotFound)
}
proxy, ok := value.(*store.Proxy)
if !ok {
return nil, errors.WithStack(errUnexpectedContextValue)
}
return proxy, nil
}