diff options
Diffstat (limited to 'modules/caddyhttp/vars.go')
-rw-r--r-- | modules/caddyhttp/vars.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/modules/caddyhttp/vars.go b/modules/caddyhttp/vars.go index cd0d8d0..d6c5a66 100644 --- a/modules/caddyhttp/vars.go +++ b/modules/caddyhttp/vars.go @@ -15,6 +15,7 @@ package caddyhttp import ( + "context" "net/http" "github.com/caddyserver/caddy/v2" @@ -75,6 +76,27 @@ func (m VarsMatcher) Match(r *http.Request) bool { return true } +// GetVar gets a value out of the context's variable table by key. +// If the key does not exist, the return value will be nil. +func GetVar(ctx context.Context, key string) interface{} { + varMap, ok := ctx.Value(VarsCtxKey).(map[string]interface{}) + if !ok { + return nil + } + return varMap[key] +} + +// SetVar sets a value in the context's variable table with +// the given key. It overwrites any previous value with the +// same key. +func SetVar(ctx context.Context, key string, value interface{}) { + varMap, ok := ctx.Value(VarsCtxKey).(map[string]interface{}) + if !ok { + return + } + varMap[key] = value +} + // Interface guards var ( _ MiddlewareHandler = (*VarsMiddleware)(nil) |