separate template logic into own file
Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
parent
ef112be9bd
commit
048c06ac66
2 changed files with 79 additions and 70 deletions
75
server.go
75
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 {
|
||||
templatesLn := newTemplateListener(func(updateFn func(map[string]*raymond.Template)) {
|
||||
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
|
||||
},
|
||||
}
|
||||
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 {
|
||||
|
|
70
template.go
Normal file
70
template.go
Normal file
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue