Add page footer with request time display

This commit is contained in:
2026-05-07 08:19:39 +02:00
parent 0a30325b96
commit 2787c15d40
5 changed files with 41 additions and 1 deletions
+19
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"embed"
"flag"
"html/template"
@@ -12,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"
)
//go:embed assets
@@ -94,7 +96,23 @@ type handler struct {
authKey []byte
}
// reqStartKey marks the request start time stored in the request context
// so HTML templates can render total server-side processing time.
type reqStartKeyT struct{}
var reqStartKey = reqStartKeyT{}
// elapsedMS returns the milliseconds since the request entered ServeHTTP.
func elapsedMS(r *http.Request) int64 {
if start, ok := r.Context().Value(reqStartKey).(time.Time); ok {
return time.Since(start).Milliseconds()
}
return 0
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), reqStartKey, time.Now()))
if !h.checkAuth(w, r) {
return
}
@@ -234,6 +252,7 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
if editMode {
t = editTmpl
}
data.RenderMS = elapsedMS(r)
if err := t.ExecuteTemplate(w, "layout", data); err != nil {
log.Printf("template error: %v", err)
}