27 lines
553 B
Go
27 lines
553 B
Go
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
// keyWrapper is wrapper for context keys.
|
||
|
type keyWrapper string
|
||
|
|
||
|
const nsKey = keyWrapper("namespace")
|
||
|
|
||
|
// GetNamespace extract namespace from context.
|
||
|
func GetNamespace(ctx context.Context) (string, error) {
|
||
|
ns, ok := ctx.Value(nsKey).(string)
|
||
|
if !ok {
|
||
|
return "", fmt.Errorf("couldn't get namespace from context")
|
||
|
}
|
||
|
|
||
|
return ns, nil
|
||
|
}
|
||
|
|
||
|
// SetNamespace sets namespace in the context.
|
||
|
func SetNamespace(ctx context.Context, ns string) context.Context {
|
||
|
return context.WithValue(ctx, nsKey, ns)
|
||
|
}
|