From 048c06ac662fa3a5185a72f8c74ee677875b9a7d Mon Sep 17 00:00:00 2001 From: Naman Sood Date: Mon, 15 Mar 2021 23:36:00 -0400 Subject: [PATCH] separate template logic into own file Signed-off-by: Naman Sood --- server.go | 79 ++++++----------------------------------------------- template.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 70 deletions(-) create mode 100644 template.go diff --git a/server.go b/server.go index 3444f38..99e779b 100644 --- a/server.go +++ b/server.go @@ -7,10 +7,8 @@ import ( "net/http" "os" "runtime" - "strconv" "strings" "sync" - "time" "github.com/aymerick/raymond" "github.com/rjeczalik/notify" @@ -40,10 +38,13 @@ func newServer() (*server, error) { s.postList = posts s.postsMutex.Unlock() - err = s.refreshTemplates() + tpls, err := loadTemplates([]string{"page", "fullpost", "summary", "notfound", "error"}) if err != nil { return nil, err } + s.tplMutex.Lock() + s.templates = tpls + s.tplMutex.Unlock() err = s.refreshStyles() if err != nil { @@ -57,28 +58,11 @@ func newServer() (*server, error) { }) go postsLn.listen() - templatesLn := &listener{ - folder: "templates/", - update: func(file string) error { - s.tplMutex.Lock() - defer s.tplMutex.Unlock() - tplName := strings.TrimSuffix(file, ".html") - newTpl, err := loadTemplate(tplName) - if err != nil { - return err - } - s.templates[tplName] = newTpl - return nil - }, - clean: func(file string) error { - s.tplMutex.Lock() - defer s.tplMutex.Unlock() - tplName := strings.TrimSuffix(file, ".html") - delete(s.templates, tplName) - log.Printf("Unloaded template: %s", tplName) - return nil - }, - } + templatesLn := newTemplateListener(func(updateFn func(map[string]*raymond.Template)) { + s.tplMutex.Lock() + defer s.tplMutex.Unlock() + updateFn(s.templates) + }) go templatesLn.listen() stylesLn := &listener{ @@ -107,51 +91,6 @@ func newServer() (*server, error) { return s, nil } -func loadTemplate(file string) (*raymond.Template, error) { - tpl, err := raymond.ParseFile("templates/" + file + ".html") - if err != nil { - return nil, fmt.Errorf("Could not parse %s template: %w", file, err) - } - tpl.RegisterHelper("datetime", func(timeStr string) string { - timestamp, err := strconv.ParseInt(timeStr, 10, 64) - if err != nil { - log.Printf("Could not parse timestamp '%v', falling back to current time", timeStr) - timestamp = time.Now().Unix() - } - return time.Unix(timestamp, 0).Format("Jan 2 2006, 3:04 PM") - }) - log.Printf("Loaded template: %s", file) - return tpl, nil -} - -// loadTemplates, for each f in files, loads `templates/$f.html` -// as a handlebars HTML template. If any single template fails to -// load, only an error is returned. Conversely, if there is no error, -// every template name passed is guaranteed to have loaded successfully. -func loadTemplates(files []string) (map[string]*raymond.Template, error) { - templates := make(map[string]*raymond.Template) - for _, f := range files { - tpl, err := loadTemplate(f) - if err != nil { - return nil, err - } - templates[f] = tpl - } - log.Printf("Loaded templates: %s", files) - return templates, nil -} - -func (s *server) refreshTemplates() error { - s.tplMutex.Lock() - defer s.tplMutex.Unlock() - tpls, err := loadTemplates([]string{"page", "fullpost", "summary", "notfound", "error"}) - if err != nil { - return err - } - s.templates = tpls - return nil -} - func loadSassStylesheet(filename string) error { in, err := os.Open("styles/" + filename) if err != nil { diff --git a/template.go b/template.go new file mode 100644 index 0000000..f2e7f48 --- /dev/null +++ b/template.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "log" + "strconv" + "strings" + "time" + + "github.com/aymerick/raymond" +) + +func loadTemplate(file string) (*raymond.Template, error) { + tpl, err := raymond.ParseFile("templates/" + file + ".html") + if err != nil { + return nil, fmt.Errorf("Could not parse %s template: %w", file, err) + } + tpl.RegisterHelper("datetime", func(timeStr string) string { + timestamp, err := strconv.ParseInt(timeStr, 10, 64) + if err != nil { + log.Printf("Could not parse timestamp '%v', falling back to current time", timeStr) + timestamp = time.Now().Unix() + } + return time.Unix(timestamp, 0).Format("Jan 2 2006, 3:04 PM") + }) + log.Printf("Loaded template: %s", file) + return tpl, nil +} + +// loadTemplates, for each f in files, loads `templates/$f.html` +// as a handlebars HTML template. If any single template fails to +// load, only an error is returned. Conversely, if there is no error, +// every template name passed is guaranteed to have loaded successfully. +func loadTemplates(files []string) (map[string]*raymond.Template, error) { + templates := make(map[string]*raymond.Template) + for _, f := range files { + tpl, err := loadTemplate(f) + if err != nil { + return nil, err + } + templates[f] = tpl + } + log.Printf("Loaded templates: %s", files) + return templates, nil +} + +func newTemplateListener(update func(func(map[string]*raymond.Template))) *listener { + return &listener{ + folder: "templates/", + update: func(file string) error { + tplName := strings.TrimSuffix(file, ".html") + newTpl, err := loadTemplate(tplName) + if err != nil { + return err + } + update(func(oldMap map[string]*raymond.Template) { + oldMap[tplName] = newTpl + }) + return nil + }, + clean: func(file string) error { + tplName := strings.TrimSuffix(file, ".html") + update(func(oldMap map[string]*raymond.Template) { + delete(oldMap, tplName) + }) + log.Printf("Unloaded template: %s", tplName) + return nil + }, + } +}