Replaced RunThemeTemplate and GetThemeTemplate with methods on *Theme and refactored their implementations to make them more predictable.

Added the Elapsed1 route timer.
This commit is contained in:
Azareal 2018-12-08 10:45:27 +10:00
parent 21cf956a26
commit 99012e8961
9 changed files with 18 additions and 13 deletions

View File

@ -357,7 +357,7 @@ func handleErrorTemplate(w http.ResponseWriter, r *http.Request, pi ErrorPage) {
if RunPreRenderHook("pre_render_error", w, r, &pi.Header.CurrentUser, &pi) { if RunPreRenderHook("pre_render_error", w, r, &pi.Header.CurrentUser, &pi) {
return return
} }
err := RunThemeTemplate(pi.Header.Theme.Name, "error", pi, w) err := pi.Header.Theme.RunTmpl("error", pi, w)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }

View File

@ -587,7 +587,7 @@ func InitTemplates() error {
fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} { fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} {
header := headerInt.(*Header) header := headerInt.(*Header)
err := RunThemeTemplate(header.Theme.Name, nameInt.(string), pageInt, header.Writer) err := header.Theme.RunTmpl(nameInt.(string), pageInt, header.Writer)
if err != nil { if err != nil {
return err return err
} }

View File

@ -967,7 +967,7 @@ ArgLoop:
// TODO: Refactor this // TODO: Refactor this
// TODO: Call the template function directly rather than going through RunThemeTemplate to eliminate a round of indirection? // TODO: Call the template function directly rather than going through RunThemeTemplate to eliminate a round of indirection?
out = "{\nerr := common.RunThemeTemplate(" + headParam + ".Theme.Name," + nameParam + "," + pageParam + ",w)\n" out = "{\nerr := " + headParam + ".Theme.RunTmpl(" + nameParam + "," + pageParam + ",w)\n"
out += "if err != nil {\nreturn err\n}\n}\n" out += "if err != nil {\nreturn err\n}\n}\n"
literal = true literal = true
break ArgLoop break ArgLoop

View File

@ -23,6 +23,7 @@ var Themes ThemeList = make(map[string]*Theme) // ? Refactor this into a store?
var DefaultThemeBox atomic.Value var DefaultThemeBox atomic.Value
var ChangeDefaultThemeMutex sync.Mutex var ChangeDefaultThemeMutex sync.Mutex
// TODO: Fallback to a random theme if this doesn't exist, so admins can remove themes they don't use
// TODO: Use this when the default theme doesn't exist // TODO: Use this when the default theme doesn't exist
var fallbackTheme = "cosora" var fallbackTheme = "cosora"
var overridenTemplates = make(map[string]bool) // ? What is this used for? var overridenTemplates = make(map[string]bool) // ? What is this used for?
@ -301,14 +302,14 @@ func (w GzipResponseWriter) Write(b []byte) (int, error) {
// NEW method of doing theme templates to allow one user to have a different theme to another. Under construction. // NEW method of doing theme templates to allow one user to have a different theme to another. Under construction.
// TODO: Generate the type switch instead of writing it by hand // TODO: Generate the type switch instead of writing it by hand
// TODO: Cut the number of types in half // TODO: Cut the number of types in half
func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer) error { func (theme *Theme) RunTmpl(template string, pi interface{}, w io.Writer) error {
// Unpack this to avoid an indirect call // Unpack this to avoid an indirect call
gzw, ok := w.(GzipResponseWriter) gzw, ok := w.(GzipResponseWriter)
if ok { if ok {
w = gzw.Writer w = gzw.Writer
} }
var getTmpl = GetThemeTemplate(theme, template) var getTmpl = theme.GetTmpl(template)
switch tmplO := getTmpl.(type) { switch tmplO := getTmpl.(type) {
case *func(CustomPagePage, io.Writer) error: case *func(CustomPagePage, io.Writer) error:
var tmpl = *tmplO var tmpl = *tmplO
@ -366,7 +367,7 @@ func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer
case func(Page, io.Writer) error: case func(Page, io.Writer) error:
return tmplO(pi.(Page), w) return tmplO(pi.(Page), w)
case nil, string: case nil, string:
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap[template] mapping, ok := theme.TemplatesMap[template]
if !ok { if !ok {
mapping = template mapping = template
} }
@ -392,10 +393,10 @@ func RunThemeTemplate(theme string, template string, pi interface{}, w io.Writer
} }
// GetThemeTemplate attempts to get the template for a specific theme, otherwise it falls back on the default template pointer, which if absent will fallback onto the template interpreter // GetThemeTemplate attempts to get the template for a specific theme, otherwise it falls back on the default template pointer, which if absent will fallback onto the template interpreter
func GetThemeTemplate(theme string, template string) interface{} { func (theme *Theme) GetTmpl(template string) interface{} {
// TODO: Figure out why we're getting a nil pointer here when transpiled templates are disabled, I would have assumed that we would just fall back to !ok on this // TODO: Figure out why we're getting a nil pointer here when transpiled templates are disabled, I would have assumed that we would just fall back to !ok on this
// Might have something to do with it being the theme's TmplPtr map, investigate. // Might have something to do with it being the theme's TmplPtr map, investigate.
tmpl, ok := Themes[theme].TmplPtr[template] tmpl, ok := theme.TmplPtr[template]
if ok { if ok {
return tmpl return tmpl
} }

View File

@ -186,7 +186,7 @@ func (widget *Widget) Build(hvars interface{}) (string, error) {
} }
var header = hvars.(*Header) var header = hvars.(*Header)
err := RunThemeTemplate(header.Theme.Name, widget.Body, hvars, header.Writer) err := header.Theme.RunTmpl(widget.Body, hvars, header.Writer)
return "", err return "", err
} }

View File

@ -189,7 +189,7 @@ func RouteGuildList(w http.ResponseWriter, r *http.Request, user common.User) co
} }
pi := ListPage{"Guild List", user, header, guildList} pi := ListPage{"Guild List", user, header, guildList}
err = common.RunThemeTemplate(header.Theme.Name, "guilds_guild_list", pi, w) err = header.Theme.RunTmpl("guilds_guild_list", pi, w)
if err != nil { if err != nil {
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }

View File

@ -147,7 +147,7 @@ func AccountLoginMFAVerify(w http.ResponseWriter, r *http.Request, user common.U
if common.RunPreRenderHook("pre_render_login_mfa_verify", w, r, &user, &pi) { if common.RunPreRenderHook("pre_render_login_mfa_verify", w, r, &user, &pi) {
return nil return nil
} }
err = common.RunThemeTemplate(header.Theme.Name, "login_mfa_verify", pi, w) err = header.Theme.RunTmpl("login_mfa_verify", pi, w)
if err != nil { if err != nil {
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/Azareal/Gosora/common" "github.com/Azareal/Gosora/common"
) )
@ -20,10 +21,13 @@ func ParseSEOURL(urlBit string) (slug string, id int, err error) {
} }
func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, header *common.Header, pi interface{}) common.RouteError { func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, header *common.Header, pi interface{}) common.RouteError {
if header.CurrentUser.IsAdmin {
header.Elapsed1 = time.Since(header.StartedAt).String()
}
if common.RunPreRenderHook("pre_render_"+tmplName, w, r, &header.CurrentUser, pi) { if common.RunPreRenderHook("pre_render_"+tmplName, w, r, &header.CurrentUser, pi) {
return nil return nil
} }
err := common.RunThemeTemplate(header.Theme.Name, tmplName, pi, w) err := header.Theme.RunTmpl(tmplName, pi, w)
if err != nil { if err != nil {
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }

View File

@ -31,7 +31,7 @@ func IPSearch(w http.ResponseWriter, r *http.Request, user common.User, header *
if common.RunPreRenderHook("pre_render_ip_search", w, r, &user, &pi) { if common.RunPreRenderHook("pre_render_ip_search", w, r, &user, &pi) {
return nil return nil
} }
err = common.RunThemeTemplate(header.Theme.Name, "ip_search", pi, w) err = header.Theme.RunTmpl("ip_search", pi, w)
if err != nil { if err != nil {
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }