Refactor template loading around the handler
This commit is contained in:
parent
d40dc21627
commit
5517da6a69
9 changed files with 40 additions and 42 deletions
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
19
lib/stats.go
19
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Reference in a new issue