diff --git a/router/faqHandler.go b/router/faqHandler.go index bd1ff0a4..e4ab1768 100644 --- a/router/faqHandler.go +++ b/router/faqHandler.go @@ -1,19 +1,24 @@ package router - -import( - "net/http" +import ( "html/template" + "net/http" + "github.com/gorilla/mux" ) +var faqTemplate = template.Must(template.New("FAQ").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/FAQ.html")) + +func init() { + // common + template.Must(faqTemplate.ParseGlob("templates/_*.html")) +} + func FaqHandler(w http.ResponseWriter, r *http.Request) { - var templates = template.Must(template.New("FAQ").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/FAQ.html")) - templates.ParseGlob("templates/_*.html") // common searchForm := NewSearchForm() - searchForm.HideAdvancedSearch = true - err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) - if err != nil { + searchForm.HideAdvancedSearch = true + err := faqTemplate.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } -} \ No newline at end of file +} diff --git a/router/homeHandler.go b/router/homeHandler.go index abc7620e..70a44af0 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -1,19 +1,24 @@ package router -import( - "net/http" - "html/template" - "github.com/gorilla/mux" +import ( "html" + "html/template" + "net/http" "strconv" + "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" + "github.com/gorilla/mux" ) +var homeTemplate = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) + +func init() { + template.Must(homeTemplate.ParseGlob("templates/_*.html")) // common +} + func HomeHandler(w http.ResponseWriter, r *http.Request) { -var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) - templates.ParseGlob("templates/_*.html") // common - vars := mux.Vars(r) + vars := mux.Vars(r) page := vars["page"] // db params url @@ -39,7 +44,7 @@ var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("te navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)} - err := templates.ExecuteTemplate(w, "index.html", htv) + err := homeTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/router/searchHandler.go b/router/searchHandler.go index 53a539e9..75f9f329 100644 --- a/router/searchHandler.go +++ b/router/searchHandler.go @@ -1,19 +1,24 @@ package router -import( - "github.com/ewhal/nyaa/util/search" - "net/http" - "html/template" - "github.com/gorilla/mux" +import ( "html" + "html/template" + "net/http" "strconv" + "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/util/search" + "github.com/gorilla/mux" ) +var searchTemplate = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) + +func init() { + template.Must(searchTemplate.ParseGlob("templates/_*.html")) // common +} + func SearchHandler(w http.ResponseWriter, r *http.Request) { - var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) - templates.ParseGlob("templates/_*.html") // common vars := mux.Vars(r) page := vars["page"] @@ -22,10 +27,10 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) { if pagenum == 0 { pagenum = 1 } - + b := []model.TorrentsJson{} - - search_param, torrents, nbTorrents := search.SearchByQuery( r, pagenum ) + + search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum) for i, _ := range torrents { res := torrents[i].ToJson() @@ -39,12 +44,12 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) { search_param.Category, search_param.Sort, search_param.Order, - false, + false, } htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)} - err := templates.ExecuteTemplate(w, "index.html", htv) + err := searchTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } -} \ No newline at end of file +} diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 999a909b..76b9d91c 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -1,14 +1,19 @@ package router import ( - "github.com/gorilla/mux" "html/template" "net/http" + + "github.com/gorilla/mux" ) +var uploadTemplate = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html")) + +func init() { + template.Must(uploadTemplate.ParseGlob("templates/_*.html")) // common +} + func UploadHandler(w http.ResponseWriter, r *http.Request) { - var templates = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html")) - templates.ParseGlob("templates/_*.html") // common var err error var uploadForm UploadForm if r.Method == "POST" { @@ -20,7 +25,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { } } else if r.Method == "GET" { htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} - err = templates.ExecuteTemplate(w, "index.html", htv) + err = uploadTemplate.ExecuteTemplate(w, "index.html", htv) } else { w.WriteHeader(http.StatusMethodNotAllowed) return diff --git a/router/viewTorrentHandler.go b/router/viewTorrentHandler.go index cf205338..ed1654c7 100644 --- a/router/viewTorrentHandler.go +++ b/router/viewTorrentHandler.go @@ -1,15 +1,20 @@ package router import ( - "github.com/ewhal/nyaa/service/torrent" - "github.com/gorilla/mux" "html/template" "net/http" + + "github.com/ewhal/nyaa/service/torrent" + "github.com/gorilla/mux" ) +var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) + +func init() { + template.Must(viewTemplate.ParseGlob("templates/_*.html")) // common +} + func ViewHandler(w http.ResponseWriter, r *http.Request) { - var templates = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) - templates.ParseGlob("templates/_*.html") // common vars := mux.Vars(r) id := vars["id"] @@ -18,7 +23,7 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) { htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} - err = templates.ExecuteTemplate(w, "index.html", htv) + err = viewTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }