Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions render/fromctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ func FromCtx(ctx context.Context) *Page {
page := ctx.Value("renderer").(*Page)

// Setting values from the valuer in the page
vlr, ok := ctx.Value("valuer").(interface{ Values() map[string]any })
type vlr interface {
Values() map[string]any
}

vlx, ok := ctx.Value("valuer").(vlr)
if !ok {
return page
}

for k, v := range vlr.Values() {
for k, v := range vlx.Values() {
page.Set(k, v)
}

return page
}

// FromCtx returns the render engine from the context,
// EngineFromCtx returns the render engine from the context,
// it assumes the render render engine has been set
// by the render middleware.
func EngineFromCtx(ctx context.Context) *Engine {
Expand Down
7 changes: 5 additions & 2 deletions server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go.leapkit.dev/core/server/session"
)

// Options for the server
// Option for the server
type Option func(*mux)

// WithHost allows to specify the host to run the server at
Expand Down Expand Up @@ -46,7 +46,10 @@ func WithAssets(embedded fs.FS, servingPath string) Option {
return func(m *mux) {
m.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if vlr, ok := r.Context().Value("valuer").(interface{ Set(string, any) }); ok {
// finding the valuer from the context
vlr := ValuerFromContext(r.Context())
if vlr != nil {
vlr.Set("assetManager", manager)
vlr.Set("assetPath", manager.PathFor)
vlr.Set("importMap", manager.ImportMap)
}
Expand Down
25 changes: 24 additions & 1 deletion server/valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import (
"sync"
)

const (
valuerContextKey = "valuer"
)

// Valuer interface to store and retrieve values from the request context
// these values can be used by other components such as the render engine
// or the assets manager.
type Valuer interface {
Values() map[string]any
Value(string) any
Set(string, any)
}

// ValuerFromContext retrieves the Valuer from the context.
func ValuerFromContext(ctx context.Context) Valuer {
vlr, ok := ctx.Value(valuerContextKey).(Valuer)
if !ok {
return nil
}

return vlr
}

// values is a map where we can store values for the request context
// these values will then be available for other components such as
// the render engine.
Expand Down Expand Up @@ -45,7 +68,7 @@ func setValuer(next http.Handler) http.Handler {
},
}

r = r.WithContext(context.WithValue(r.Context(), "valuer", vlr))
r = r.WithContext(context.WithValue(r.Context(), valuerContextKey, vlr))
next.ServeHTTP(w, r)
})
}
Loading