Defer setting headers to renderTemplate to avoid wasting resources for JSON requests and for future optimisations.
Error pages now use routes.renderTemplate bringing them in line with the other standard routes. Every route should use a renderTemplate function now instead of calling RunTmpl. Merged some pi and renderTemplate lines in routes to reduce the amount of vertical space used.
This commit is contained in:
parent
b487ad0e5a
commit
42b9f27c45
|
@ -129,7 +129,6 @@ func errorHeader(w http.ResponseWriter, user User, title string) *Header {
|
|||
header := DefaultHeader(w, user)
|
||||
header.Title = title
|
||||
header.Zone = "error"
|
||||
prepResources(&user, header, header.Theme)
|
||||
return header
|
||||
}
|
||||
|
||||
|
@ -304,10 +303,7 @@ func LoginRequiredJS(w http.ResponseWriter, r *http.Request, user User) RouteErr
|
|||
func SecurityError(w http.ResponseWriter, r *http.Request, user User) RouteError {
|
||||
w.WriteHeader(403)
|
||||
pi := ErrorPage{errorHeader(w, user, phrases.GetErrorPhrase("security_error_title")), phrases.GetErrorPhrase("security_error_body")}
|
||||
if RunPreRenderHook("pre_render_security_error", w, r, &user, &pi) {
|
||||
return nil
|
||||
}
|
||||
err := pi.Header.Theme.RunTmpl("error", pi, w)
|
||||
err := RenderTemplateAlias("error", "security_error", w, r, pi.Header, pi)
|
||||
if err != nil {
|
||||
LogError(err)
|
||||
}
|
||||
|
@ -381,12 +377,11 @@ func writeJsonError(errmsg string, w http.ResponseWriter) {
|
|||
}
|
||||
|
||||
func handleErrorTemplate(w http.ResponseWriter, r *http.Request, pi ErrorPage) {
|
||||
// TODO: What to do about this hook?
|
||||
if RunPreRenderHook("pre_render_error", w, r, &pi.Header.CurrentUser, &pi) {
|
||||
return
|
||||
}
|
||||
err := pi.Header.Theme.RunTmpl("error", pi, w)
|
||||
err := RenderTemplateAlias("error", "error", w, r, pi.Header, pi)
|
||||
if err != nil {
|
||||
LogError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Alias of routes.renderTemplate
|
||||
var RenderTemplateAlias func(tmplName string, hookName string, w http.ResponseWriter, r *http.Request, header *Header, pi interface{}) error
|
|
@ -222,11 +222,11 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
|
|||
header.StartedAt = time.Now()
|
||||
}
|
||||
|
||||
prepResources(user,header,theme)
|
||||
//PrepResources(user,header,theme)
|
||||
return header, nil
|
||||
}
|
||||
|
||||
func prepResources(user *User, header *Header, theme *Theme) {
|
||||
func PrepResources(user *User, header *Header, theme *Theme) {
|
||||
header.AddSheet(theme.Name + "/main.css")
|
||||
|
||||
if len(theme.Resources) > 0 {
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
|
||||
c "github.com/Azareal/Gosora/common"
|
||||
"github.com/Azareal/Gosora/routes"
|
||||
)
|
||||
|
||||
// A blank list to fill out that parameter in Page for routes which don't use it
|
||||
|
@ -189,11 +190,7 @@ func RouteGuildList(w http.ResponseWriter, r *http.Request, user c.User) c.Route
|
|||
}
|
||||
|
||||
pi := ListPage{"Guild List", user, header, guildList}
|
||||
err = header.Theme.RunTmpl("guilds_guild_list", pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return routes.RenderTemplate("guilds_guild_list", w, r, header, pi)
|
||||
}
|
||||
|
||||
func MiddleViewGuild(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
||||
|
@ -231,12 +228,7 @@ func RouteCreateGuild(w http.ResponseWriter, r *http.Request, user c.User) c.Rou
|
|||
}
|
||||
CommonAreaWidgets(header)
|
||||
|
||||
pi := c.Page{header, tList, nil}
|
||||
err := header.Theme.RunTmpl("guilds_create_guild", pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return routes.RenderTemplate("guilds_create_guild", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
func RouteCreateGuildSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
||||
|
@ -384,7 +376,7 @@ func PreRenderViewForum(w http.ResponseWriter, r *http.Request, user *c.User, da
|
|||
guildItem := guildData.(*Guild)
|
||||
|
||||
guildpi := Page{pi.Title, pi.Header, pi.ItemList, pi.Forum, guildItem, pi.Page, pi.LastPage}
|
||||
err := header.Theme.RunTmpl("guilds_view_guild", guildpi, w)
|
||||
err := routes.RenderTemplate("guilds_view_guild", w, r, header, guildpi)
|
||||
if err != nil {
|
||||
c.LogError(err)
|
||||
return false
|
||||
|
|
6
main.go
6
main.go
|
@ -27,6 +27,7 @@ import (
|
|||
c "github.com/Azareal/Gosora/common"
|
||||
"github.com/Azareal/Gosora/common/counters"
|
||||
"github.com/Azareal/Gosora/common/phrases"
|
||||
"github.com/Azareal/Gosora/routes"
|
||||
"github.com/Azareal/Gosora/query_gen"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -41,6 +42,11 @@ type Globs struct {
|
|||
stmts *Stmts
|
||||
}
|
||||
|
||||
// Temporary alias for renderTemplate
|
||||
func init() {
|
||||
c.RenderTemplateAlias = routes.RenderTemplate
|
||||
}
|
||||
|
||||
// Experimenting with a new error package here to try to reduce the amount of debugging we have to do
|
||||
// TODO: Dynamically register these items to avoid maintaining as much code here?
|
||||
func afterDBInit() (err error) {
|
||||
|
|
|
@ -28,8 +28,7 @@ func AccountLogin(w http.ResponseWriter, r *http.Request, user c.User, header *c
|
|||
return c.LocalError("You're already logged in.", w, r, user)
|
||||
}
|
||||
header.Title = phrases.GetTitlePhrase("login")
|
||||
pi := c.Page{header, tList, nil}
|
||||
return renderTemplate("login", w, r, header, pi)
|
||||
return renderTemplate("login", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
// TODO: Log failed attempted logins?
|
||||
|
@ -161,15 +160,7 @@ func AccountLoginMFAVerify(w http.ResponseWriter, r *http.Request, user c.User,
|
|||
return c.LocalError("Invalid session", w, r, user)
|
||||
}
|
||||
|
||||
pi := c.Page{header, tList, nil}
|
||||
if c.RunPreRenderHook("pre_render_login_mfa_verify", w, r, &user, &pi) {
|
||||
return nil
|
||||
}
|
||||
err = header.Theme.RunTmpl("login_mfa_verify", pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return renderTemplate("login_mfa_verify", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
func AccountLoginMFAVerifySubmit(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
||||
|
@ -202,8 +193,7 @@ func AccountRegister(w http.ResponseWriter, r *http.Request, user c.User, header
|
|||
}
|
||||
header.Title = phrases.GetTitlePhrase("register")
|
||||
header.LooseCSP = true
|
||||
pi := c.Page{header, tList, nil}
|
||||
return renderTemplate("register", w, r, header, pi)
|
||||
return renderTemplate("register", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
func isNumeric(data string) (numeric bool) {
|
||||
|
@ -392,8 +382,7 @@ func AccountEdit(w http.ResponseWriter, r *http.Request, user c.User, header *c.
|
|||
//edit_password
|
||||
func AccountEditPassword(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
||||
accountEditHead("account_password", w, r, &user, header)
|
||||
pi := c.Page{header, tList, nil}
|
||||
return renderTemplate("account_own_edit_password", w, r, header, pi)
|
||||
return renderTemplate("account_own_edit_password", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
// TODO: Require re-authentication if the user hasn't logged in in a while
|
||||
|
@ -759,8 +748,7 @@ func LevelList(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
|
|||
levels[i] = c.LevelListItem{i, iScore, status, perc * 2}
|
||||
}
|
||||
|
||||
pi := c.LevelListPage{header, levels[1:]}
|
||||
return renderTemplate("level_list", w, r, header, pi)
|
||||
return renderTemplate("level_list", w, r, header, c.LevelListPage{header, levels[1:]})
|
||||
}
|
||||
|
||||
func Alerts(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
||||
|
|
|
@ -76,6 +76,20 @@ func doPush(w http.ResponseWriter, header *c.Header) {
|
|||
}
|
||||
|
||||
func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) c.RouteError {
|
||||
return renderTemplate2(tmplName, tmplName, w, r, header, pi)
|
||||
}
|
||||
|
||||
func renderTemplate2(tmplName string, hookName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) c.RouteError {
|
||||
err := renderTemplate3(tmplName, tmplName, w, r, header, pi)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func renderTemplate3(tmplName string, hookName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) error {
|
||||
c.PrepResources(&header.CurrentUser, header, header.Theme)
|
||||
|
||||
if header.CurrentUser.Loggedin {
|
||||
header.MetaDesc = ""
|
||||
header.OGDesc = ""
|
||||
|
@ -98,12 +112,11 @@ func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, hea
|
|||
if header.CurrentUser.IsAdmin {
|
||||
header.Elapsed1 = time.Since(header.StartedAt).String()
|
||||
}
|
||||
if c.RunPreRenderHook("pre_render_"+tmplName, w, r, &header.CurrentUser, pi) {
|
||||
if c.RunPreRenderHook("pre_render_"+hookName, w, r, &header.CurrentUser, pi) {
|
||||
return nil
|
||||
}
|
||||
err := header.Theme.RunTmpl(tmplName, pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return header.Theme.RunTmpl(tmplName, pi, w)
|
||||
}
|
||||
|
||||
// TODO: Rename renderTemplate to RenderTemplate instead of using this hack to avoid breaking things
|
||||
var RenderTemplate = renderTemplate3
|
||||
|
|
|
@ -49,6 +49,5 @@ func ForumList(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
|
|||
}
|
||||
}
|
||||
|
||||
pi := c.ForumsPage{header, forumList}
|
||||
return renderTemplate("forums", w, r, header, pi)
|
||||
return renderTemplate("forums", w, r, header, c.ForumsPage{header, forumList})
|
||||
}
|
||||
|
|
|
@ -50,8 +50,7 @@ func StaticFile(w http.ResponseWriter, r *http.Request) {
|
|||
func Overview(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
||||
header.Title = phrases.GetTitlePhrase("overview")
|
||||
header.Zone = "overview"
|
||||
pi := c.Page{header, tList, nil}
|
||||
return renderTemplate("overview", w, r, header, pi)
|
||||
return renderTemplate("overview", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
func CustomPage(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header, name string) c.RouteError {
|
||||
|
@ -60,8 +59,7 @@ func CustomPage(w http.ResponseWriter, r *http.Request, user c.User, header *c.H
|
|||
page, err := c.Pages.GetByName(name)
|
||||
if err == nil {
|
||||
header.Title = page.Title
|
||||
pi := c.CustomPagePage{header, page}
|
||||
return renderTemplate("custom_page", w, r, header, pi)
|
||||
return renderTemplate("custom_page", w, r, header, c.CustomPagePage{header, page})
|
||||
} else if err != sql.ErrNoRows {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
|
@ -72,16 +70,8 @@ func CustomPage(w http.ResponseWriter, r *http.Request, user c.User, header *c.H
|
|||
}
|
||||
|
||||
header.Title = phrases.GetTitlePhrase("page")
|
||||
pi := c.Page{header, tList, nil}
|
||||
// TODO: Pass the page name to the pre-render hook?
|
||||
if c.RunPreRenderHook("pre_render_tmpl_page", w, r, &user, &pi) {
|
||||
return nil
|
||||
}
|
||||
err = header.Theme.RunTmpl("page_"+name, pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return renderTemplate2("page_"+name, "tmpl_page", w, r, header, c.Page{header, tList, nil})
|
||||
}
|
||||
|
||||
// TODO: Set the cookie domain
|
||||
|
|
|
@ -26,14 +26,5 @@ func IPSearch(w http.ResponseWriter, r *http.Request, user c.User, header *c.Hea
|
|||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
|
||||
pi := c.IPSearchPage{header, userList, ip}
|
||||
if c.RunPreRenderHook("pre_render_ip_search", w, r, &user, &pi) {
|
||||
return nil
|
||||
}
|
||||
err = header.Theme.RunTmpl("ip_search", pi, w)
|
||||
if err != nil {
|
||||
return c.InternalError(err, w, r)
|
||||
}
|
||||
return nil
|
||||
return renderTemplate("ip_search", w, r, header, c.IPSearchPage{header, userList, ip})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue