2017-05-07 13:51:33 +02:00
package router
import (
"html/template"
"path/filepath"
)
2017-05-11 13:42:26 +02:00
var TemplateDir = "templates"
2017-05-07 13:51:33 +02:00
2017-05-13 00:17:34 +02:00
var homeTemplate , searchTemplate , faqTemplate , uploadTemplate , viewTemplate , viewRegisterTemplate , viewLoginTemplate , viewRegisterSuccessTemplate , viewVerifySuccessTemplate , viewProfileTemplate , viewProfileEditTemplate , viewUserDeleteTemplate , notFoundTemplate , changeLanguageTemplate * template . Template
2017-05-07 13:51:33 +02:00
2017-05-13 17:29:21 +02:00
var panelIndex , panelTorrentList , panelUserList , panelCommentList , panelTorrentEd , panelTorrentReportList , panelTorrentReassign * template . Template
2017-05-11 12:49:05 +02:00
2017-05-07 13:51:33 +02:00
type templateLoader struct {
2017-05-11 12:49:05 +02:00
templ * * template . Template
file string
indexFile string
name string
2017-05-07 13:51:33 +02:00
}
// ReloadTemplates reloads templates on runtime
func ReloadTemplates ( ) {
2017-05-11 12:49:05 +02:00
pubTempls := [ ] templateLoader {
2017-05-07 13:51:33 +02:00
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" ,
} ,
2017-05-07 14:53:01 +02:00
templateLoader {
templ : & viewRegisterTemplate ,
name : "user_register" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "register.html" ) ,
2017-05-07 14:53:01 +02:00
} ,
templateLoader {
templ : & viewRegisterSuccessTemplate ,
name : "user_register_success" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "signup_success.html" ) ,
2017-05-07 14:53:01 +02:00
} ,
2017-05-07 22:00:45 +02:00
templateLoader {
templ : & viewVerifySuccessTemplate ,
name : "user_verify_success" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "verify_success.html" ) ,
2017-05-07 22:00:45 +02:00
} ,
2017-05-07 14:53:01 +02:00
templateLoader {
templ : & viewLoginTemplate ,
name : "user_login" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "login.html" ) ,
2017-05-07 14:53:01 +02:00
} ,
2017-05-09 03:36:48 +02:00
templateLoader {
templ : & viewProfileTemplate ,
name : "user_profile" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "profile.html" ) ,
2017-05-09 03:36:48 +02:00
} ,
2017-05-09 17:47:06 +02:00
templateLoader {
templ : & viewProfileEditTemplate ,
name : "user_profile" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "profile_edit.html" ) ,
2017-05-09 17:47:06 +02:00
} ,
templateLoader {
templ : & viewUserDeleteTemplate ,
name : "user_delete" ,
2017-05-11 12:49:05 +02:00
file : filepath . Join ( "user" , "delete_success.html" ) ,
2017-05-09 17:47:06 +02:00
} ,
2017-05-09 04:12:32 +02:00
templateLoader {
templ : & notFoundTemplate ,
name : "404" ,
file : "404.html" ,
} ,
2017-05-13 00:17:34 +02:00
templateLoader {
templ : & changeLanguageTemplate ,
name : "change_language" ,
file : "change_language.html" ,
} ,
2017-05-07 13:51:33 +02:00
}
2017-05-11 12:49:05 +02:00
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" ) ,
} ,
2017-05-13 17:29:21 +02:00
templateLoader {
templ : & panelTorrentReassign ,
name : "torrent_reassign" ,
file : filepath . Join ( "admin" , "reassign.html" ) ,
} ,
2017-05-11 12:49:05 +02:00
}
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 ... )
2017-05-07 13:51:33 +02:00
for _ , templ := range templs {
2017-05-11 12:49:05 +02:00
t := template . Must ( template . New ( templ . name ) . Funcs ( FuncMap ) . ParseFiles ( templ . indexFile , filepath . Join ( TemplateDir , templ . file ) ) )
2017-05-11 01:15:36 +02:00
t = template . Must ( t . ParseGlob ( filepath . Join ( TemplateDir , "_*.html" ) ) )
2017-05-07 13:51:33 +02:00
* templ . templ = t
}
2017-05-11 12:49:05 +02:00
2017-05-07 13:51:33 +02:00
}