Skip to content

Commit

Permalink
withfuncmap
Browse files Browse the repository at this point in the history
  • Loading branch information
adnaan committed Jun 10, 2023
1 parent 9b748ca commit c8f2a84
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
45 changes: 33 additions & 12 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fir
import (
"embed"
"flag"
"html/template"
"log"
"net/http"
"reflect"
Expand Down Expand Up @@ -48,11 +49,25 @@ type opt struct {
cookieName string
secureCookie *securecookie.SecureCookie
cache *cache.Cache
funcMap template.FuncMap
}

// ControllerOption is an option for the controller.
type ControllerOption func(*opt)

func WithFuncMap(funcMap template.FuncMap) ControllerOption {
return func(opt *opt) {
mergedFuncMap := make(template.FuncMap)
for k, v := range opt.funcMap {
mergedFuncMap[k] = v
}
for k, v := range funcMap {
mergedFuncMap[k] = v
}
opt.funcMap = mergedFuncMap
}
}

func WithSecureCookie(s *securecookie.SecureCookie) ControllerOption {
return func(o *opt) {
o.secureCookie = s
Expand Down Expand Up @@ -192,7 +207,8 @@ func NewController(name string, options ...ControllerOption) Controller {
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32),
),
cache: cache.New(5*time.Minute, 10*time.Minute),
cache: cache.New(5*time.Minute, 10*time.Minute),
funcMap: defaultFuncMap(),
}

for _, option := range options {
Expand Down Expand Up @@ -240,21 +256,25 @@ type controller struct {
opt
}

var defaultRouteOpt = &routeOpt{
id: shortuuid.New(),
content: "Hello Fir App!",
layoutContentName: "content",
partials: []string{"./routes/partials"},
funcMap: defaultFuncMap(),
extensions: []string{".gohtml", ".gotmpl", ".html", ".tmpl"},
eventSender: make(chan Event),
onLoad: func(ctx RouteContext) error {
return nil
},
func (c *controller) defaults() *routeOpt {
defaultRouteOpt := &routeOpt{
id: shortuuid.New(),
content: "Hello Fir App!",
layoutContentName: "content",
partials: []string{"./routes/partials"},
funcMap: c.opt.funcMap,
extensions: []string{".gohtml", ".gotmpl", ".html", ".tmpl"},
eventSender: make(chan Event),
onLoad: func(ctx RouteContext) error {
return nil
},
}
return defaultRouteOpt
}

// Route returns an http.HandlerFunc that renders the route
func (c *controller) Route(route Route) http.HandlerFunc {
defaultRouteOpt := c.defaults()
for _, option := range route.Options() {
option(defaultRouteOpt)
}
Expand All @@ -268,6 +288,7 @@ func (c *controller) Route(route Route) http.HandlerFunc {

// RouteFunc returns an http.HandlerFunc that renders the route
func (c *controller) RouteFunc(opts RouteFunc) http.HandlerFunc {
defaultRouteOpt := c.defaults()
for _, option := range opts() {
option(defaultRouteOpt)
}
Expand Down
2 changes: 1 addition & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func FuncMap(funcMap template.FuncMap) RouteOption {
for k, v := range opt.funcMap {
mergedFuncMap[k] = v
}
for k, v := range defaultFuncMap() {
for k, v := range funcMap {
mergedFuncMap[k] = v
}
opt.funcMap = mergedFuncMap
Expand Down

0 comments on commit c8f2a84

Please sign in to comment.