2017-05-07 13:51:33 +02:00
package router
import (
"html/template"
"path/filepath"
)
2017-05-25 21:54:58 +02:00
// TemplateDir : Variable to the template directory
var TemplateDir = "templates" // FIXME: Need to be a constant!
2017-05-07 13:51:33 +02:00
2017-05-25 21:54:58 +02:00
// ModeratorDir : Variable to the admin template sub directory
2017-05-20 01:10:16 +02:00
const ModeratorDir = "admin"
2017-05-19 04:55:59 +02:00
var homeTemplate ,
searchTemplate ,
faqTemplate ,
uploadTemplate ,
viewTemplate ,
viewRegisterTemplate ,
viewLoginTemplate ,
viewRegisterSuccessTemplate ,
viewVerifySuccessTemplate ,
viewProfileTemplate ,
2017-05-21 00:02:57 +02:00
viewProfileNotifTemplate ,
2017-05-19 04:55:59 +02:00
viewProfileEditTemplate ,
viewUserDeleteTemplate ,
2017-05-23 04:05:33 +02:00
userTorrentEd ,
2017-05-19 04:55:59 +02:00
notFoundTemplate ,
2017-05-27 19:08:47 +02:00
changePublicSettingsTemplate ,
2017-05-19 04:55:59 +02:00
databaseDumpTemplate * template . Template
2017-05-07 13:51:33 +02:00
2017-05-19 04:55:59 +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
}
2017-05-25 21:54:58 +02:00
// ReloadTemplates : reloads templates on runtime
2017-05-07 13:51:33 +02:00
func ReloadTemplates ( ) {
2017-05-11 12:49:05 +02:00
pubTempls := [ ] templateLoader {
2017-05-24 09:15:07 +02:00
{
2017-05-11 05:06:12 +02:00
templ : & databaseDumpTemplate ,
name : "dump" ,
file : "dumps.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 13:51:33 +02:00
templ : & homeTemplate ,
name : "home" ,
file : "home.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 13:51:33 +02:00
templ : & searchTemplate ,
name : "search" ,
file : "home.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 13:51:33 +02:00
templ : & uploadTemplate ,
name : "upload" ,
file : "upload.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 13:51:33 +02:00
templ : & faqTemplate ,
name : "FAQ" ,
file : "FAQ.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 13:51:33 +02:00
templ : & viewTemplate ,
name : "view" ,
file : "view.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 14:53:01 +02:00
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
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-07 14:53:01 +02:00
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-24 09:15:07 +02:00
{
2017-05-07 22:00:45 +02:00
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-24 09:15:07 +02:00
{
2017-05-07 14:53:01 +02:00
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-24 09:15:07 +02:00
{
2017-05-09 03:36:48 +02:00
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-24 09:15:07 +02:00
{
2017-05-21 00:02:57 +02:00
templ : & viewProfileNotifTemplate ,
name : "user_profile" ,
file : filepath . Join ( "user" , "profile_notifications.html" ) ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-09 17:47:06 +02:00
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
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-09 17:47:06 +02:00
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-24 09:15:07 +02:00
{
2017-05-23 04:05:33 +02:00
templ : & userTorrentEd ,
name : "user_torrent_edit" ,
file : filepath . Join ( "user" , "torrent_edit.html" ) ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-09 04:12:32 +02:00
templ : & notFoundTemplate ,
name : "404" ,
file : "404.html" ,
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-27 19:08:47 +02:00
templ : & changePublicSettingsTemplate ,
name : "change_settings" ,
file : "public_settings.html" ,
2017-05-13 00:17:34 +02:00
} ,
2017-05-27 19:08:47 +02:00
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 {
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelTorrentList ,
name : "torrentlist" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "torrentlist.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelUserList ,
name : "userlist" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "userlist.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelCommentList ,
name : "commentlist" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "commentlist.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelIndex ,
name : "indexPanel" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "panelindex.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelTorrentEd ,
name : "torrent_ed" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "paneltorrentedit.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-11 12:49:05 +02:00
templ : & panelTorrentReportList ,
name : "torrent_report" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "torrent_report.html" ) ,
2017-05-11 12:49:05 +02:00
} ,
2017-05-24 09:15:07 +02:00
{
2017-05-13 17:29:21 +02:00
templ : & panelTorrentReassign ,
name : "torrent_reassign" ,
2017-05-20 01:10:16 +02:00
file : filepath . Join ( ModeratorDir , "reassign.html" ) ,
2017-05-13 17:29:21 +02:00
} ,
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
}
}