Merge pull request #139 from majestrate/master
SIGHUP reloads templates on runtime
Cette révision appartient à :
révision
752ac9bcdc
10 fichiers modifiés avec 116 ajouts et 45 suppressions
2
main.go
2
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
5
router/init.go
Fichier normal
|
@ -0,0 +1,5 @@
|
|||
package router
|
||||
|
||||
func init() {
|
||||
ReloadTemplates()
|
||||
}
|
|
@ -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
53
router/template.go
Fichier normal
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
38
util/signals/unix.go
Fichier normal
38
util/signals/unix.go
Fichier normal
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
7
util/signals/win32.go
Fichier normal
7
util/signals/win32.go
Fichier normal
|
@ -0,0 +1,7 @@
|
|||
// +build win32
|
||||
|
||||
package signals
|
||||
|
||||
func Handle() {
|
||||
// windows has no sighup LOOOOL, this does nothing
|
||||
}
|
Référencer dans un nouveau ticket