Migrate from templates to Gomponents (#103)
This commit is contained in:
parent
0bf9ab7189
commit
051d032038
104 changed files with 2768 additions and 2824 deletions
|
|
@ -3,29 +3,54 @@ package context
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
// AuthenticatedUserKey is the key value used to store the authenticated user in context
|
||||
// AuthenticatedUserKey is the key used to store the authenticated user in context.
|
||||
AuthenticatedUserKey = "auth_user"
|
||||
|
||||
// UserKey is the key value used to store a user in context
|
||||
// UserKey is the key used to store a user in context.
|
||||
UserKey = "user"
|
||||
|
||||
// FormKey is the key value used to store a form in context
|
||||
// FormKey is the key used to store a form in context.
|
||||
FormKey = "form"
|
||||
|
||||
// PasswordTokenKey is the key value used to store a password token in context
|
||||
// PasswordTokenKey is the key used to store a password token in context.
|
||||
PasswordTokenKey = "password_token"
|
||||
|
||||
// LoggerKey is the key value used to store a structured logger in context
|
||||
// LoggerKey is the key used to store a structured logger in context.
|
||||
LoggerKey = "logger"
|
||||
|
||||
// SessionKey is the key value used to store the session data in context
|
||||
// SessionKey is the key used to store the session data in context.
|
||||
SessionKey = "session"
|
||||
|
||||
// HTMXRequestKey is the key used to store the HTMX request data in context.
|
||||
HTMXRequestKey = "htmx"
|
||||
|
||||
// CSRFKey is the key used to store the CSRF token in context.
|
||||
CSRFKey = "csrf"
|
||||
|
||||
// ConfigKey is the key used to store the configuration in context.
|
||||
ConfigKey = "config"
|
||||
)
|
||||
|
||||
// IsCanceledError determines if an error is due to a context cancelation
|
||||
// IsCanceledError determines if an error is due to a context cancellation.
|
||||
func IsCanceledError(err error) bool {
|
||||
return errors.Is(err, context.Canceled)
|
||||
}
|
||||
|
||||
// Cache checks if a value of a given type exists in the Echo context for a given key and returns that, otherwise
|
||||
// it will use a callback to generate a value, which is stored in the context then returned. This allows you to
|
||||
// only generate items only once for a given request.
|
||||
func Cache[T any](ctx echo.Context, key string, gen func(echo.Context) T) T {
|
||||
if val := ctx.Get(key); val != nil {
|
||||
if v, ok := val.(T); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
val := gen(ctx)
|
||||
ctx.Set(key, val)
|
||||
return val
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
|
@ -22,3 +23,25 @@ func TestIsCanceled(t *testing.T) {
|
|||
|
||||
assert.False(t, IsCanceledError(errors.New("test error")))
|
||||
}
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
ctx := echo.New().NewContext(nil, nil)
|
||||
|
||||
key := "testing"
|
||||
value := "hello"
|
||||
called := 0
|
||||
callback := func(ctx echo.Context) string {
|
||||
called++
|
||||
return value
|
||||
}
|
||||
|
||||
assert.Nil(t, ctx.Get(key))
|
||||
|
||||
got := Cache(ctx, key, callback)
|
||||
assert.Equal(t, value, got)
|
||||
assert.Equal(t, 1, called)
|
||||
|
||||
got = Cache(ctx, key, callback)
|
||||
assert.Equal(t, value, got)
|
||||
assert.Equal(t, 1, called)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue