From 80c4471017e720a45d4ded5e4e89a010169d5fe1 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 3 Mar 2024 17:58:18 -0800 Subject: [PATCH] Add DB indexes and request time to page footer --- pkg/middleware/csrf.go | 4 ++++ pkg/models/comment.go | 2 +- pkg/models/friend.go | 2 +- pkg/models/like.go | 10 +++++----- pkg/models/photo.go | 10 +++++----- pkg/session/session.go | 1 + pkg/templates/template_funcs.go | 10 ++++++++++ web/templates/base.html | 3 +++ 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/pkg/middleware/csrf.go b/pkg/middleware/csrf.go index 398e91d..ed58549 100644 --- a/pkg/middleware/csrf.go +++ b/pkg/middleware/csrf.go @@ -3,6 +3,7 @@ package middleware import ( "context" "net/http" + "time" "code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/log" @@ -18,6 +19,9 @@ func CSRF(handler http.Handler) http.Handler { token := MakeCSRFCookie(r, w) ctx := context.WithValue(r.Context(), session.CSRFKey, token) + // Store the request start time. + ctx = context.WithValue(ctx, session.RequestTimeKey, time.Now()) + // If it's a JSON post, allow it thru. if r.Header.Get("Content-Type") == "application/json" { handler.ServeHTTP(w, r.WithContext(ctx)) diff --git a/pkg/models/comment.go b/pkg/models/comment.go index c71242e..bf4d79c 100644 --- a/pkg/models/comment.go +++ b/pkg/models/comment.go @@ -18,7 +18,7 @@ type Comment struct { UserID uint64 `gorm:"index"` User User `json:"-"` Message string - CreatedAt time.Time + CreatedAt time.Time `gorm:"index"` UpdatedAt time.Time } diff --git a/pkg/models/friend.go b/pkg/models/friend.go index e5341e5..5e91060 100644 --- a/pkg/models/friend.go +++ b/pkg/models/friend.go @@ -16,7 +16,7 @@ type Friend struct { Approved bool `gorm:"index"` Ignored bool CreatedAt time.Time - UpdatedAt time.Time + UpdatedAt time.Time `gorm:"index"` } // AddFriend sends a friend request or accepts one if there was already a pending one. diff --git a/pkg/models/like.go b/pkg/models/like.go index 021c3d8..62bd02b 100644 --- a/pkg/models/like.go +++ b/pkg/models/like.go @@ -9,11 +9,11 @@ import ( // Like table. type Like struct { - ID uint64 `gorm:"primaryKey"` - UserID uint64 `gorm:"index"` // who it belongs to - TableName string - TableID uint64 - CreatedAt time.Time + ID uint64 `gorm:"primaryKey"` + UserID uint64 `gorm:"index"` // who it belongs to + TableName string `gorm:"index"` + TableID uint64 `gorm:"index"` + CreatedAt time.Time `gorm:"index"` UpdatedAt time.Time } diff --git a/pkg/models/photo.go b/pkg/models/photo.go index 969e4e3..c490e6d 100644 --- a/pkg/models/photo.go +++ b/pkg/models/photo.go @@ -19,11 +19,11 @@ type Photo struct { CroppedFilename string // if cropped, e.g. for profile photo Filesize int64 Caption string - Flagged bool // photo has been reported by the community - Visibility PhotoVisibility - Gallery bool // photo appears in the public gallery (if public) - Explicit bool // is an explicit photo - CreatedAt time.Time + Flagged bool // photo has been reported by the community + Visibility PhotoVisibility `gorm:"index"` + Gallery bool `gorm:"index"` // photo appears in the public gallery (if public) + Explicit bool `gorm:"index"` // is an explicit photo + CreatedAt time.Time `gorm:"index"` UpdatedAt time.Time } diff --git a/pkg/session/session.go b/pkg/session/session.go index 515dd35..28953ac 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -32,6 +32,7 @@ const ( ContextKey = "session" CurrentUserKey = "current_user" CSRFKey = "csrf" + RequestTimeKey = "req_time" ) // New creates a blank session object. diff --git a/pkg/templates/template_funcs.go b/pkg/templates/template_funcs.go index 5dd0cdb..011eac9 100644 --- a/pkg/templates/template_funcs.go +++ b/pkg/templates/template_funcs.go @@ -40,6 +40,7 @@ func TemplateFuncs(r *http.Request) template.FuncMap { "ToHTML": ToHTML, "PhotoURL": photo.URLPath, "Now": time.Now, + "RunTime": RunTime, "PrettyTitle": func() template.HTML { return template.HTML(fmt.Sprintf( `non` + @@ -89,6 +90,15 @@ func InputCSRF(r *http.Request) func() template.HTML { } } +// RunTime returns the elapsed time between the HTTP request start and now, as a formatted string. +func RunTime(r *http.Request) string { + if rt, ok := r.Context().Value(session.RequestTimeKey).(time.Time); ok { + duration := time.Since(rt) + return duration.Round(time.Millisecond).String() + } + return "ERROR" +} + // BlurExplicit returns true if the current user has the blur_explicit setting on and the given Photo is Explicit. func BlurExplicit(r *http.Request) func(*models.Photo) bool { return func(photo *models.Photo) bool { diff --git a/web/templates/base.html b/web/templates/base.html index ed16985..cd73df5 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -362,6 +362,9 @@ Sign up {{end}} +
+ {{RunTime .Request}} +