From 5517da6a69c318136e48b21ff148ffd4ef45107f Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 6 Jun 2017 14:18:47 +0000 Subject: [PATCH] Refactor template loading around the handler --- lib/admin.go | 4 ++-- lib/news.go | 4 ++-- lib/reader.go | 2 +- lib/search.go | 2 +- lib/stats.go | 19 +++++++++++++++++++ lib/template.go | 33 ++++++--------------------------- lib/trantor.go | 10 +++++----- lib/upload.go | 2 +- lib/user.go | 6 +++--- 9 files changed, 40 insertions(+), 42 deletions(-) diff --git a/lib/admin.go b/lib/admin.go index 6d93d58..510122a 100644 --- a/lib/admin.go +++ b/lib/admin.go @@ -73,7 +73,7 @@ func editHandler(h handler) { author = " by " + book.Authors[0] } data.S.Title = book.Title + author + " -- Edit -- " + data.S.Title - h.template.load(h, "edit", data) + h.load("edit", data) } func cleanEmptyStr(s []string) []string { @@ -185,7 +185,7 @@ func newHandler(h handler) { data.Prev = "/new/?q=" + req + "&p=" + strconv.Itoa(page-1) } data.Search = req - h.template.load(h, "new", data) + h.load("new", data) } func storeHandler(h handler) { diff --git a/lib/news.go b/lib/news.go index 6ef6a80..f8e8aa6 100644 --- a/lib/news.go +++ b/lib/news.go @@ -33,7 +33,7 @@ func newsHandler(h handler) { data.S.News = true data.News = getNews(numNews, 0, h.db) - h.template.load(h, "news", data) + h.load("news", data) } func editNewsHandler(h handler) { @@ -46,7 +46,7 @@ func editNewsHandler(h handler) { data.S = GetStatus(h) data.S.Title = "Edit news -- " + data.S.Title data.S.News = true - h.template.load(h, "edit_news", data) + h.load("edit_news", data) } func postNewsHandler(h handler) { diff --git a/lib/reader.go b/lib/reader.go index 0961f46..78b3a15 100644 --- a/lib/reader.go +++ b/lib/reader.go @@ -180,7 +180,7 @@ func readHandler(h handler) { data.Next, data.Prev = getNextPrev(e, file, id, "/read/") data.Chapters = getChapters(e, file, id, "/read/") data.Content = genLink(id, "/content/", file) - h.template.load(h, "read", data) + h.load("read", data) } func openReadEpub(id string, store storage.Store) *epubgo.Epub { diff --git a/lib/search.go b/lib/search.go index de42a89..0f6f759 100644 --- a/lib/search.go +++ b/lib/search.go @@ -54,7 +54,7 @@ func searchHandler(h handler) { data.Prev = "/search/?q=" + req + "&p=" + strconv.Itoa(page-1) + "&num=" + strconv.Itoa(items_page) } - h.template.load(h, "search", data) + h.load("search", data) } func itemsPage(r *http.Request) int { diff --git a/lib/stats.go b/lib/stats.go index 963c9cf..f473377 100644 --- a/lib/stats.go +++ b/lib/stats.go @@ -28,6 +28,25 @@ type handler struct { ro bool } +func (h handler) load(tmpl string, data interface{}) { + var err error + fmt := h.r.FormValue("fmt") + switch fmt { + case "rss": + err = h.template.rss.ExecuteTemplate(h.w, tmpl+".rss", data) + case "opds": + err = h.template.opds.ExecuteTemplate(h.w, tmpl+".opds", data) + case "json": + err = loadJson(h.w, tmpl, data) + default: + err = h.template.html.ExecuteTemplate(h.w, tmpl+".html", data) + } + if err != nil { + h.template.html.ExecuteTemplate(h.w, "404.html", data) + log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err) + } +} + type StatsGatherer struct { db database.DB store storage.Store diff --git a/lib/template.go b/lib/template.go index a4b98fb..f7e5992 100644 --- a/lib/template.go +++ b/lib/template.go @@ -4,8 +4,6 @@ import ( html_tmpl "html/template" txt_tmpl "text/template" - log "github.com/cihub/seelog" - "encoding/json" "errors" "net/http" @@ -47,16 +45,16 @@ func GetStatus(h handler) Status { } type Template struct { - tmpl_html *html_tmpl.Template - tmpl_rss *txt_tmpl.Template - tmpl_opds *txt_tmpl.Template + html *html_tmpl.Template + rss *txt_tmpl.Template + opds *txt_tmpl.Template } func InitTemplate(assetsPath string) *Template { var t Template templatePath := path.Join(assetsPath, "templates") - t.tmpl_html = html_tmpl.Must(html_tmpl.ParseFiles( + t.html = html_tmpl.Must(html_tmpl.ParseFiles( path.Join(templatePath, "header.html"), path.Join(templatePath, "footer.html"), path.Join(templatePath, "404.html"), @@ -76,12 +74,12 @@ func InitTemplate(assetsPath string) *Template { path.Join(templatePath, "help.html"), )) - t.tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles( + t.rss = txt_tmpl.Must(txt_tmpl.ParseFiles( path.Join(templatePath, "search.rss"), path.Join(templatePath, "news.rss"), )) - t.tmpl_opds = txt_tmpl.Must(txt_tmpl.ParseFiles( + t.opds = txt_tmpl.Must(txt_tmpl.ParseFiles( path.Join(templatePath, "index.opds"), path.Join(templatePath, "search.opds"), )) @@ -89,25 +87,6 @@ func InitTemplate(assetsPath string) *Template { return &t } -func (t Template) load(h handler, tmpl string, data interface{}) { - var err error - fmt := h.r.FormValue("fmt") - switch fmt { - case "rss": - err = t.tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data) - case "opds": - err = t.tmpl_opds.ExecuteTemplate(h.w, tmpl+".opds", data) - case "json": - err = loadJson(h.w, tmpl, data) - default: - err = t.tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data) - } - if err != nil { - t.tmpl_html.ExecuteTemplate(h.w, "404.html", data) - log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err) - } -} - func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error { var res []byte var err error diff --git a/lib/trantor.go b/lib/trantor.go index ba124ed..6457ae8 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -29,7 +29,7 @@ func aboutHandler(h handler) { data.S = GetStatus(h) data.S.Title = "About -- " + data.S.Title data.S.About = true - h.template.load(h, "about", data) + h.load("about", data) } func helpHandler(h handler) { @@ -37,7 +37,7 @@ func helpHandler(h handler) { data.S = GetStatus(h) data.S.Title = "Help -- " + data.S.Title data.S.Help = true - h.template.load(h, "help", data) + h.load("help", data) } func logoutHandler(h handler) { @@ -72,7 +72,7 @@ func bookHandler(h handler) { data.S.Title = book.Title + author + " -- " + data.S.Title data.Description = strings.Split(data.Book.Description, "\n") - h.template.load(h, "book", data) + h.load("book", data) } func downloadHandler(h handler) { @@ -121,7 +121,7 @@ func indexHandler(h handler) { data.VisitedBooks = frontPage.Visited data.DownloadedBooks = frontPage.Download - h.template.load(h, "index", data) + h.load("index", data) } func notFound(h handler) { @@ -130,7 +130,7 @@ func notFound(h handler) { data.S = GetStatus(h) data.S.Title = "Not found --" + data.S.Title h.w.WriteHeader(http.StatusNotFound) - h.template.load(h, "404", data) + h.load("404", data) } func UpdateLogger(loggerConfig string) error { diff --git a/lib/upload.go b/lib/upload.go index 62cc2de..4bf368b 100644 --- a/lib/upload.go +++ b/lib/upload.go @@ -105,7 +105,7 @@ func uploadHandler(h handler) { data.S = GetStatus(h) data.S.Title = "Upload -- " + data.S.Title data.S.Upload = true - h.template.load(h, "upload", data) + h.load("upload", data) } type uploadData struct { diff --git a/lib/user.go b/lib/user.go index 063301b..8f20dc2 100644 --- a/lib/user.go +++ b/lib/user.go @@ -15,7 +15,7 @@ func loginHandler(h handler) { var data statusData data.S = GetStatus(h) data.S.Title = "Login -- " + data.S.Title - h.template.load(h, "login", data) + h.load("login", data) } func loginPostHandler(h handler) { @@ -61,7 +61,7 @@ func dashboardHandler(h handler) { data.S = GetStatus(h) data.S.Title = "Dashboard -- " + data.S.Title data.S.Dasboard = true - h.template.load(h, "dashboard", data) + h.load("dashboard", data) } func settingsHandler(h handler) { @@ -88,5 +88,5 @@ func settingsHandler(h handler) { var data statusData data.S = GetStatus(h) data.S.Title = "Settings -- " + data.S.Title - h.template.load(h, "settings", data) + h.load("settings", data) }