Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Don't reparse templates on every fucking request

Cette révision appartient à :
bakape 2017-05-06 20:01:15 +03:00
Parent e447b7566a
révision 2cb605a027
5 fichiers modifiés avec 64 ajouts et 39 suppressions

Voir le fichier

@ -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)
}
}
}

Voir le fichier

@ -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)
}

Voir le fichier

@ -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)
}
}
}

Voir le fichier

@ -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

Voir le fichier

@ -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)
}