mod panel template cleanup, make mod panel templates reload on SIGHUP
Cette révision appartient à :
Parent
ca93024f49
révision
33fe211d3f
2 fichiers modifiés avec 70 ajouts et 40 suppressions
|
@ -3,9 +3,7 @@ package router
|
|||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/ewhal/nyaa/db"
|
||||
|
@ -23,23 +21,6 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, panelTorrentReportList *template.Template
|
||||
|
||||
func init() {
|
||||
panelTorrentList = template.Must(template.New("torrentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/torrentlist.html")))
|
||||
panelTorrentList = template.Must(panelTorrentList.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
panelUserList = template.Must(template.New("userlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/userlist.html")))
|
||||
panelUserList = template.Must(panelUserList.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
panelCommentList = template.Must(template.New("commentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/commentlist.html")))
|
||||
panelCommentList = template.Must(panelCommentList.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
panelIndex = template.Must(template.New("indexPanel").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/panelindex.html")))
|
||||
panelIndex = template.Must(panelIndex.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
panelTorrentEd = template.Must(template.New("torrent_ed").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/paneltorrentedit.html")))
|
||||
panelTorrentEd = template.Must(panelTorrentEd.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
panelTorrentReportList = template.Must(template.New("torrent_report").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/torrent_report.html")))
|
||||
panelTorrentReportList = template.Must(panelTorrentReportList.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
}
|
||||
|
||||
func IndexModPanel(w http.ResponseWriter, r *http.Request) {
|
||||
currentUser := GetUser(r)
|
||||
if userPermission.HasAdmin(currentUser) {
|
||||
|
@ -217,19 +198,19 @@ func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) {
|
|||
err := form.NewErrors()
|
||||
infos := form.NewInfos()
|
||||
torrent, _ := torrentService.GetTorrentById(id)
|
||||
if (torrent.ID > 0) {
|
||||
if torrent.ID > 0 {
|
||||
errUp := uploadForm.ExtractEditInfo(r)
|
||||
if errUp != nil {
|
||||
err["errors"] = append(err["errors"], "Failed to update torrent!")
|
||||
}
|
||||
if (len(err) == 0) {
|
||||
if len(err) == 0 {
|
||||
// update some (but not all!) values
|
||||
torrent.Name = uploadForm.Name
|
||||
torrent.Category = uploadForm.CategoryID
|
||||
torrent.Name = uploadForm.Name
|
||||
torrent.Category = uploadForm.CategoryID
|
||||
torrent.SubCategory = uploadForm.SubCategoryID
|
||||
torrent.Status = uploadForm.Status
|
||||
torrent.Status = uploadForm.Status
|
||||
torrent.Description = uploadForm.Description
|
||||
torrent.Uploader = nil // GORM will create a new user otherwise (wtf?!)
|
||||
torrent.Uploader = nil // GORM will create a new user otherwise (wtf?!)
|
||||
db.ORM.Save(&torrent)
|
||||
infos["infos"] = append(infos["infos"], "Torrent details updated.")
|
||||
}
|
||||
|
|
|
@ -5,19 +5,22 @@ import (
|
|||
"path/filepath"
|
||||
)
|
||||
|
||||
var TemplateDir = "templates"
|
||||
const TemplateDir = "templates"
|
||||
|
||||
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template
|
||||
|
||||
var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, panelTorrentReportList *template.Template
|
||||
|
||||
type templateLoader struct {
|
||||
templ **template.Template
|
||||
file string
|
||||
name string
|
||||
templ **template.Template
|
||||
file string
|
||||
indexFile string
|
||||
name string
|
||||
}
|
||||
|
||||
// ReloadTemplates reloads templates on runtime
|
||||
func ReloadTemplates() {
|
||||
templs := []templateLoader{
|
||||
pubTempls := []templateLoader{
|
||||
templateLoader{
|
||||
templ: &homeTemplate,
|
||||
name: "home",
|
||||
|
@ -46,37 +49,37 @@ func ReloadTemplates() {
|
|||
templateLoader{
|
||||
templ: &viewRegisterTemplate,
|
||||
name: "user_register",
|
||||
file: "user/register.html",
|
||||
file: filepath.Join("user", "register.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewRegisterSuccessTemplate,
|
||||
name: "user_register_success",
|
||||
file: "user/signup_success.html",
|
||||
file: filepath.Join("user", "signup_success.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewVerifySuccessTemplate,
|
||||
name: "user_verify_success",
|
||||
file: "user/verify_success.html",
|
||||
file: filepath.Join("user", "verify_success.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewLoginTemplate,
|
||||
name: "user_login",
|
||||
file: "user/login.html",
|
||||
file: filepath.Join("user", "login.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewProfileTemplate,
|
||||
name: "user_profile",
|
||||
file: "user/profile.html",
|
||||
file: filepath.Join("user", "profile.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewProfileEditTemplate,
|
||||
name: "user_profile",
|
||||
file: "user/profile_edit.html",
|
||||
file: filepath.Join("user", "profile_edit.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewUserDeleteTemplate,
|
||||
name: "user_delete",
|
||||
file: "user/delete_success.html",
|
||||
file: filepath.Join("user", "delete_success.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: ¬FoundTemplate,
|
||||
|
@ -84,10 +87,56 @@ func ReloadTemplates() {
|
|||
file: "404.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(TemplateDir, "_*.html")))
|
||||
for idx := range pubTempls {
|
||||
pubTempls[idx].indexFile = filepath.Join(TemplateDir, "index.html")
|
||||
}
|
||||
|
||||
modTempls := []templateLoader{
|
||||
templateLoader{
|
||||
templ: &panelTorrentList,
|
||||
name: "torrentlist",
|
||||
file: filepath.Join("admin", "torrentlist.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &panelUserList,
|
||||
name: "userlist",
|
||||
file: filepath.Join("admin", "userlist.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &panelCommentList,
|
||||
name: "commentlist",
|
||||
file: filepath.Join("admin", "commentlist.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &panelIndex,
|
||||
name: "indexPanel",
|
||||
file: filepath.Join("admin", "panelindex.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &panelTorrentEd,
|
||||
name: "torrent_ed",
|
||||
file: filepath.Join("admin", "paneltorrentedit.html"),
|
||||
},
|
||||
templateLoader{
|
||||
templ: &panelTorrentReportList,
|
||||
name: "torrent_report",
|
||||
file: filepath.Join("admin", "torrent_report.html"),
|
||||
},
|
||||
}
|
||||
|
||||
for idx := range modTempls {
|
||||
modTempls[idx].indexFile = filepath.Join(TemplateDir, "admin_index.html")
|
||||
}
|
||||
|
||||
templs := make([]templateLoader, 0, len(modTempls)+len(pubTempls))
|
||||
|
||||
templs = append(templs, pubTempls...)
|
||||
templs = append(templs, modTempls...)
|
||||
|
||||
for _, templ := range templs {
|
||||
t := template.Must(template.New(templ.name).Funcs(FuncMap).ParseFiles(templ.indexFile, filepath.Join(TemplateDir, templ.file)))
|
||||
t = template.Must(t.ParseGlob(filepath.Join(TemplateDir, "_*.html")))
|
||||
*templ.templ = t
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket