33 lines
721 B
Go
33 lines
721 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
)
|
|
|
|
const (
|
|
// KeyDebug is the context key associated with the debug value
|
|
KeyDebug ContextKey = "debug"
|
|
)
|
|
|
|
// ErrInvalidDebug is returned when no debug value
|
|
// could be found on the given context
|
|
var ErrInvalidDebug = errors.New("invalid debug")
|
|
|
|
// GetDebug retrieves the debug value from the given context
|
|
func GetDebug(ctx context.Context) (bool, error) {
|
|
debug, ok := ctx.Value(KeyDebug).(bool)
|
|
if !ok {
|
|
return false, ErrInvalidDebug
|
|
}
|
|
return debug, nil
|
|
}
|
|
|
|
// Debug expose the given debug flag as a context value
|
|
// on the HTTP requests
|
|
func Debug(debug bool) Middleware {
|
|
return middleware.WithValue(KeyDebug, debug)
|
|
}
|