Skip to content

Commit

Permalink
dump
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwltr committed Aug 16, 2024
1 parent d0fe438 commit 00c5f14
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 62 deletions.
2 changes: 1 addition & 1 deletion migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ The builtin template engine has some shortcommings like being able to pass multi
### Hints

* The handler function signatures changed from `func(*webutil.View, *http.Request) webutil.Response` to `func(*http.Request) webutil.Response`.
* Wrap function changes from `webutil.ViewHandler.Wrap` to `webutil.Wrap`.
* Wrap function changes from `webutil.ViewHandler.Wrap` to `webutil.WrapView`.
* Since the `View` interface is not part of the function signature, it needs to be added to the struct where the handler function is attached to.
* Non-HTML responses are now created with functions directly from the `webutil` package. For example `return webutilext.ViewError(http.StatusInternalServerError, err)` instead of `return v.Error(http.StatusBadRequest, fmt.Errorf(`unknown value for "until"`))`. where `v` was passed to the handler.

Expand Down
13 changes: 13 additions & 0 deletions pkg/webutil/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ func DevAuthMiddleware(roles ...string) func(http.Handler) http.Handler {
// {{ else }}
// <a class="nav-link" href="/auth/login">Login</span></a>
// {{ end }}
//
// Deprecated: use AuthJetOptions
func AuthTemplateFunctions(r *http.Request) template.FuncMap {
authenticated := true
info := AuthInfoFromRequest(r)
Expand All @@ -329,6 +331,17 @@ func AuthTemplateFunctions(r *http.Request) template.FuncMap {
}
}

func AuthJetOptions() JetOption {
return JetOptions(
WithRequestVar("AuthIsAuthenticated", func(r *http.Request) bool {
return AuthInfoFromRequest(r) != nil
}),
WithRequestVar("AuthInfo", func(r *http.Request) *AuthInfo {
return AuthInfoFromRequest(r)
}),
)
}

// AuthInfoFromContext extracts the AuthInfo from the given context. The
// AuthInfo is injected into the request via the AuthMiddleware. Therefore it
// is required to use this middleware to be able to get the AuthInfo.
Expand Down
67 changes: 6 additions & 61 deletions pkg/webutil/jetview.go → pkg/webutil/viewjet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webutil

import (
"fmt"
"io"
"io/fs"
"net/http"
Expand All @@ -14,42 +13,14 @@ import (
)

type JetViewer struct {
views *jet.Set
htmlOptions []JetViewerHTMLOption
}

type JetOption func(*JetViewer)

func WithJetViewerHTMLOption(o JetViewerHTMLOption) JetOption {
return func(jet *JetViewer) {
jet.htmlOptions = append(jet.htmlOptions, o)
}
}

func JetFunctionOption(name string, fn any) JetOption {
return func(jet *JetViewer) {
jet.views.AddGlobal(name, fn)
}
}

// deprecated: use JetFunctionOption
func JetFunctionMapOption(funcs map[string]any) JetOption {
return func(jet *JetViewer) {
for name, fn := range funcs {
jet.views.AddGlobal(name, fn)
}
}
}

func JetVarOption(key string, value any) JetOption {
return func(jet *JetViewer) {
jet.views.AddGlobal(key, value)
}
views *jet.Set
options []JetOption
}

func NewJetViewer(js *jet.Set, options ...JetOption) *JetViewer {
jv := &JetViewer{
views: js,
views: js,
options: options,
}

jv.views.AddGlobal("contains", strings.Contains)
Expand All @@ -64,36 +35,10 @@ func NewJetViewer(js *jet.Set, options ...JetOption) *JetViewer {
return v
})

jv.apply(options...)

return jv
}

func (j *JetViewer) apply(options ...JetOption) {
for _, option := range options {
option(j)
}
}

type JetViewerHTMLOption func(*http.Request, *jet.VarMap)

func WithVar(name string, value any) JetViewerHTMLOption {
return func(_ *http.Request, vars *jet.VarMap) {
vars.Set(name, value)
}
}

func WithRequestVar(name string, fn func(*http.Request) any) JetViewerHTMLOption {
return func(r *http.Request, vars *jet.VarMap) {
vars.Set(name, fn(r))
}
}

func WithVarf(name string, s string, a ...any) JetViewerHTMLOption {
return WithVar(name, fmt.Sprintf(s, a...))
}

func (j *JetViewer) HTML(status int, filename string, data any, opts ...JetViewerHTMLOption) http.HandlerFunc {
func (j *JetViewer) HTML(status int, filename string, data any, opts ...JetOption) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
span, ctx := tracer.StartSpanFromContext(
r.Context(), "render",
Expand All @@ -113,7 +58,7 @@ func (j *JetViewer) HTML(status int, filename string, data any, opts ...JetViewe
vars := make(jet.VarMap)
vars.Set("currentURLPath", r.URL.Path)

for _, o := range j.htmlOptions {
for _, o := range j.options {
o(r, &vars)
}

Expand Down
69 changes: 69 additions & 0 deletions pkg/webutil/viewjetoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package webutil

import (
"fmt"
"net/http"

"github.com/CloudyKit/jet/v6"
)

type JetOption func(*http.Request, *jet.VarMap)

func JetOptions(options ...JetOption) JetOption {
return func(r *http.Request, m *jet.VarMap) {
for _, option := range options {
option(r, m)
}
}
}

func WithVar(name string, value any) JetOption {
return func(_ *http.Request, m *jet.VarMap) {
m.Set(name, value)
}
}

func WithVarf(name string, s string, a ...any) JetOption {
return WithVar(name, fmt.Sprintf(s, a...))
}

// WithRequestVar is a JetOption that adds any variable or function to the
// template context, that is based on the *http.Request.
func WithRequestVar[O any](name string, fn func(*http.Request) O) JetOption {
return func(r *http.Request, m *jet.VarMap) {
m.Set(name, fn(r))
}
}

// WithRequestVar1 is a shortcut to define a template function with one
// argument that depends on the *http.Request. It simply avoids nesting two
// `return func...` by merging their arguments.
func WithRequestVar1[I1, O any](name string, fn func(*http.Request, I1) O) JetOption {
return WithRequestVar(name, func(r *http.Request) any {
return func(v1 I1) any {
return fn(r, v1)
}
})
}

// WithRequestVar2 is a shortcut to define a template function with two
// argument that depends on the *http.Request. It simply avoids nesting two
// `return func...` by merging their arguments.
func WithRequestVar2[I1, I2, O any](name string, fn func(*http.Request, I1, I2) O) JetOption {
return WithRequestVar(name, func(r *http.Request) any {
return func(v1 I1, v2 I2) any {
return fn(r, v1, v2)
}
})
}

// WithRequestVar3 is a shortcut to define a template function with three
// argument that depends on the *http.Request. It simply avoids nesting two
// `return func...` by merging their arguments.
func WithRequestVar3[I1, I2, I3, O any](name string, fn func(*http.Request, I1, I2, I3) O) JetOption {
return WithRequestVar(name, func(r *http.Request) any {
return func(v1 I1, v2 I2, v3 I3) any {
return fn(r, v1, v2, v3)
}
})
}

0 comments on commit 00c5f14

Please sign in to comment.