Include time taken to render in footer

This commit is contained in:
Tom Wiesing 2023-11-10 20:34:20 +01:00
parent 7fefb689c7
commit 733aa237d1
No known key found for this signature in database
4 changed files with 17 additions and 1 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/tkw1536/pkglib/contextx" "github.com/tkw1536/pkglib/contextx"
"github.com/tkw1536/pkglib/httpx" "github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/timewrap"
"github.com/tkw1536/pkglib/mux" "github.com/tkw1536/pkglib/mux"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
@ -113,6 +114,8 @@ func (server *Server) Server(ctx context.Context, progress io.Writer) (public ht
public = WithCSP(public, models.ContentSecurityPolicyDistilery) public = WithCSP(public, models.ContentSecurityPolicyDistilery)
internal = WithCSP(internal, models.ContentSecurityPolicyNothing) internal = WithCSP(internal, models.ContentSecurityPolicyNothing)
public = timewrap.Wrap(public)
err = nil err = nil
return return
} }

View file

@ -13,6 +13,7 @@ import (
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/tkw1536/pkglib/httpx" "github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/timewrap"
) )
//go:embed "src/base.html" //go:embed "src/base.html"
@ -44,6 +45,7 @@ func (tpl *Template[C]) context(r *http.Request, funcs ...FlagFunc) (ctx *tConte
// setup the basic properties // setup the basic properties
ctx.ctx = r.Context() ctx.ctx = r.Context()
ctx.Runtime.RequestURI = r.URL.RequestURI() ctx.Runtime.RequestURI = r.URL.RequestURI()
ctx.Runtime.StartedAt = timewrap.Start(r).UTC()
ctx.Runtime.GeneratedAt = time.Now().UTC() ctx.Runtime.GeneratedAt = time.Now().UTC()
ctx.Runtime.CSRF = csrf.TemplateField(r) ctx.Runtime.CSRF = csrf.TemplateField(r)
ctx.Runtime.Menu = tpl.templating.buildMenu(r) ctx.Runtime.Menu = tpl.templating.buildMenu(r)

View file

@ -37,10 +37,21 @@ type RuntimeFlags struct {
RequestURI string // request uri of the current page RequestURI string // request uri of the current page
Menu []component.MenuItem // menu at the top of the page Menu []component.MenuItem // menu at the top of the page
StartedAt time.Time // time the request started to generate
GeneratedAt time.Time // time the underlying data returned GeneratedAt time.Time // time the underlying data returned
CSRF template.HTML // csrf data (if any) CSRF template.HTML // csrf data (if any)
} }
// Returns how long this request took to render
func (rf RuntimeFlags) Took() time.Duration {
return time.Since(rf.StartedAt)
}
func (rf RuntimeFlags) TookHTML() template.HTML {
took := rf.Took()
return template.HTML(fmt.Sprintf("<time datetime=\"P%.3f\">%s</time>", took.Seconds(), took))
}
var runtimeFlagsName = reflectx.TypeFor[RuntimeFlags]().Name() var runtimeFlagsName = reflectx.TypeFor[RuntimeFlags]().Name()
// Clone clones this flags // Clone clones this flags

View file

@ -1,3 +1,3 @@
<p> <p>
Generated <time format="{{ .GeneratedAt.Format "2006-01-02T15:04:05Z" }}">{{ .GeneratedAt.Format "2006-01-02T15:04:05Z07:00" }}</time> by <a href="https://github.com/FAU-CDI/wisski-distillery" target="_blank" rel="noopener noreferer">WissKI Distillery</a>. This site might use cookies for essential purposes, see also <a href="/legal/">Legal Notices</a>. Generated at <time datetime="{{ .GeneratedAt.Format "2006-01-02T15:04:05Z" }}">{{ .GeneratedAt.Format "2006-01-02T15:04:05Z07:00" }}</time> in {{ .TookHTML }} by <a href="https://github.com/FAU-CDI/wisski-distillery" target="_blank" rel="noopener noreferer">WissKI Distillery</a>. This site might use cookies for essential purposes, see also <a href="/legal/">Legal Notices</a>.
</p> </p>