diff --git a/main.go b/main.go index 6b191fb3..7c18a42c 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/util/log" + "github.com/ewhal/nyaa/util/signals" "net/http" "os" @@ -54,6 +55,7 @@ func main() { } db.ORM, _ = db.GormInit(conf) initI18N() + go signals.Handle() RunServer(conf) } } diff --git a/router/faqHandler.go b/router/faqHandler.go index e4ab1768..31b1e4c5 100644 --- a/router/faqHandler.go +++ b/router/faqHandler.go @@ -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 diff --git a/router/homeHandler.go b/router/homeHandler.go index 1b2a26aa..f8a643a6 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -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) } } diff --git a/router/init.go b/router/init.go new file mode 100644 index 00000000..383631ad --- /dev/null +++ b/router/init.go @@ -0,0 +1,5 @@ +package router + +func init() { + ReloadTemplates() +} diff --git a/router/searchHandler.go b/router/searchHandler.go index f104e705..c44bf29e 100644 --- a/router/searchHandler.go +++ b/router/searchHandler.go @@ -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"] diff --git a/router/template.go b/router/template.go new file mode 100644 index 00000000..402e6980 --- /dev/null +++ b/router/template.go @@ -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 + } +} diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 536c57a6..701d51fe 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -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 diff --git a/router/viewTorrentHandler.go b/router/viewTorrentHandler.go index 7a093b73..67a9c4ce 100644 --- a/router/viewTorrentHandler.go +++ b/router/viewTorrentHandler.go @@ -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) } } diff --git a/util/signals/unix.go b/util/signals/unix.go new file mode 100644 index 00000000..091bb25e --- /dev/null +++ b/util/signals/unix.go @@ -0,0 +1,38 @@ +// +build !win32 + +package signals + +import ( + "github.com/ewhal/nyaa/router" + "github.com/ewhal/nyaa/util/log" + "os" + "os/signal" + "syscall" +) + +func handleReload() { + log.Info("Got SIGHUP") + router.ReloadTemplates() + log.Info("reloaded templates") + +} + +// Handle signals +// returns when done +func Handle() { + chnl := make(chan os.Signal) + signal.Notify(chnl, syscall.SIGHUP) + for { + sig, ok := <-chnl + if !ok { + break + } + switch sig { + case syscall.SIGHUP: + handleReload() + break + default: + break + } + } +} diff --git a/util/signals/win32.go b/util/signals/win32.go new file mode 100644 index 00000000..9554cce2 --- /dev/null +++ b/util/signals/win32.go @@ -0,0 +1,7 @@ +// +build win32 + +package signals + +func Handle() { + // windows has no sighup LOOOOL, this does nothing +}