aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2020-02-19 02:17:11 +0000
committerFelix Hanley <felix@userspace.com.au>2020-02-19 02:17:11 +0000
commitee02835f27fc7bd222a34d86cafbd723b05946c0 (patch)
treed918b796f7552ec8f81dea740670659d254c9b90 /cmd
parent5c23a2bfb751f2435d9d6ebfa4e168b115988f0b (diff)
downloadsws-ee02835f27fc7bd222a34d86cafbd723b05946c0.tar.gz
sws-ee02835f27fc7bd222a34d86cafbd723b05946c0.tar.bz2
Update templates to nest correctly
Diffstat (limited to 'cmd')
-rw-r--r--cmd/server/handlers.go15
-rw-r--r--cmd/server/main.go40
-rw-r--r--cmd/server/sites.go15
3 files changed, 42 insertions, 28 deletions
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go
index de3bfa3..e47fd30 100644
--- a/cmd/server/handlers.go
+++ b/cmd/server/handlers.go
@@ -1,20 +1,25 @@
package main
import (
- "html/template"
"net/http"
)
-func handleIndex(tmpls *template.Template) http.HandlerFunc {
+func handleIndex(rndr Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
- tmpls.ExecuteTemplate(w, "home", nil)
+ if err := rndr.Render(w, "home", nil); err != nil {
+ log(err)
+ http.Error(w, http.StatusText(500), 500)
+ }
}
}
-func handleExample(tmpls *template.Template) http.HandlerFunc {
+func handleExample(rndr Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
- tmpls.ExecuteTemplate(w, "example", nil)
+ if err := rndr.Render(w, "example", nil); err != nil {
+ log(err)
+ http.Error(w, http.StatusText(500), 500)
+ }
}
}
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 386247e..d23fb8f 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -4,7 +4,6 @@ import (
"context"
"flag"
"fmt"
- "html/template"
"net/http"
"os"
"path/filepath"
@@ -19,6 +18,7 @@ import (
_ "github.com/mattn/go-sqlite3"
"src.userspace.com.au/sws"
"src.userspace.com.au/sws/store"
+ "src.userspace.com.au/templates"
)
// Flags
@@ -44,6 +44,10 @@ func init() {
debug = func(v ...interface{}) {}
}
+type Renderer interface {
+ Render(http.ResponseWriter, string, interface{}) error
+}
+
func main() {
flag.Parse()
@@ -91,18 +95,18 @@ func main() {
st = store.NewSqlite3Store(db)
}
- tmpls := template.Must(loadTemplateHTML([]string{
- "home",
- "sites",
- "example",
- "partials/navMain",
- "partials/pageHead",
- "partials/pageFoot",
- "partials/siteForList",
- "partials/pageForList",
- "partials/barChart",
- }, funcMap))
- debug(tmpls.DefinedTemplates())
+ tmpls, err := LoadHTMLTemplateMap(map[string][]string{
+ "sites": []string{"layouts/base", "sites", "charts"},
+ "site": []string{"layouts/base", "site", "charts"},
+ "home": []string{"layouts/public", "home"},
+ "example": []string{"example"},
+ }, funcMap)
+ if err != nil {
+ log(err)
+ os.Exit(1)
+ }
+ //debug(tmpls.DefinedTemplates())
+ renderer := templates.NewRenderer(tmpls)
r := chi.NewRouter()
r.Use(middleware.RequestID)
@@ -121,10 +125,10 @@ func main() {
// For UI
r.Get("/hits", handleHits(st))
r.Route("/sites", func(r chi.Router) {
- r.Get("/", handleSites(st, tmpls))
+ r.Get("/", handleSites(st, renderer))
r.Route("/{siteID}", func(r chi.Router) {
r.Use(siteCtx)
- r.Get("/", handleSite(st, tmpls))
+ r.Get("/", handleSite(st, renderer))
r.Route("/sparklines", func(r chi.Router) {
r.Get("/{b:\\d+}-{e:\\d+}.svg", sparklineHandler(st))
})
@@ -143,11 +147,11 @@ func main() {
}
// Example
- r.Get("/test.html", handleExample(tmpls))
- r.Get("/test-again.html", handleExample(tmpls))
+ r.Get("/test.html", handleExample(renderer))
+ r.Get("/test-again.html", handleExample(renderer))
r.Route("/", func(r chi.Router) {
- r.Get("/", handleIndex(tmpls))
+ r.Get("/", handleIndex(renderer))
fileServer(r, filepath.Dir(staticPath), "/", http.Dir(staticPath))
})
diff --git a/cmd/server/sites.go b/cmd/server/sites.go
index 7e5cacd..1dc265e 100644
--- a/cmd/server/sites.go
+++ b/cmd/server/sites.go
@@ -1,14 +1,13 @@
package main
import (
- "html/template"
"net/http"
"time"
"src.userspace.com.au/sws"
)
-func handleSites(db sws.SiteStore, tmpls *template.Template) http.HandlerFunc {
+func handleSites(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sites, err := db.GetSites()
if err != nil {
@@ -20,11 +19,14 @@ func handleSites(db sws.SiteStore, tmpls *template.Template) http.HandlerFunc {
}{
Sites: sites,
}
- tmpls.ExecuteTemplate(w, "sites", payload)
+ if err := rndr.Render(w, "sites", payload); err != nil {
+ log(err)
+ http.Error(w, http.StatusText(500), 500)
+ }
}
}
-func handleSite(db sws.SiteStore, tmpls *template.Template) http.HandlerFunc {
+func handleSite(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
site, ok := ctx.Value("site").(*sws.Site)
@@ -64,6 +66,9 @@ func handleSite(db sws.SiteStore, tmpls *template.Template) http.HandlerFunc {
Pages: pages,
Hits: buckets,
}
- tmpls.ExecuteTemplate(w, "site", payload)
+ if err := rndr.Render(w, "site", payload); err != nil {
+ log(err)
+ http.Error(w, http.StatusText(500), 500)
+ }
}
}