Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

fix up templates, get reading for Reload template on SIGHUP

Cette révision appartient à :
Jeff Becker 2017-05-07 07:51:33 -04:00
Parent 47bf337fbc
révision b599bb1ef2
7 fichiers modifiés avec 69 ajouts et 45 suppressions

Voir le fichier

@ -1,19 +1,11 @@
package router
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) {
searchForm := NewSearchForm()
searchForm.HideAdvancedSearch = true

Voir le fichier

@ -3,19 +3,13 @@ package router
import (
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
"github.com/gorilla/mux"
"html"
"html/template"
"net/http"
"strconv"
)
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) {
vars := mux.Vars(r)
page := vars["page"]
@ -45,7 +39,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
err := homeTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Errorf("HomeHandler(): %s", err)
}
}

5
router/init.go Fichier normal
Voir le fichier

@ -0,0 +1,5 @@
package router
func init() {
ReloadTemplates()
}

Voir le fichier

@ -1,21 +1,14 @@
package router
import (
"html"
"html/template"
"net/http"
"strconv"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util/search"
"github.com/gorilla/mux"
"html"
"net/http"
"strconv"
)
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) {
vars := mux.Vars(r)
page := vars["page"]

53
router/template.go Fichier normal
Voir le fichier

@ -0,0 +1,53 @@
package router
import (
"html/template"
"path/filepath"
)
var TemplateDir = "templates"
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate *template.Template
type templateLoader struct {
templ **template.Template
file string
name string
}
// ReloadTemplates reloads templates on runtime
func ReloadTemplates() {
templs := []templateLoader{
templateLoader{
templ: &homeTemplate,
name: "home",
file: "home.html",
},
templateLoader{
templ: &searchTemplate,
name: "search",
file: "home.html",
},
templateLoader{
templ: &uploadTemplate,
name: "upload",
file: "upload.html",
},
templateLoader{
templ: &faqTemplate,
name: "FAQ",
file: "FAQ.html",
},
templateLoader{
templ: &viewTemplate,
name: "view",
file: "view.html",
},
}
for _, templ := range templs {
t := template.Must(template.New(templ.name).Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "index.html"), filepath.Join(TemplateDir, templ.file)))
t = template.Must(t.ParseGlob(filepath.Join("templates", "_*.html")))
*templ.templ = t
}
}

Voir le fichier

@ -2,7 +2,6 @@ package router
import (
"fmt"
"html/template"
"net/http"
"strconv"
"time"
@ -13,12 +12,6 @@ import (
"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 err error
var uploadForm UploadForm

Voir le fichier

@ -1,34 +1,28 @@
package router
import (
"html/template"
"net/http"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
"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) {
vars := mux.Vars(r)
id := vars["id"]
torrent, err := torrentService.GetTorrentById(id)
if err != nil {
NotFoundHandler(w, r)
return
}
if err != nil {
NotFoundHandler(w, r)
return
}
b := torrent.ToJson()
htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
err = viewTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Errorf("ViewHandler(): %s", err)
}
}