Add DB indexes and request time to page footer

This commit is contained in:
Noah Petherbridge 2024-03-03 17:58:18 -08:00
parent 28111585ef
commit 80c4471017
8 changed files with 30 additions and 12 deletions

View File

@ -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))

View File

@ -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
}

View File

@ -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.

View File

@ -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
}

View File

@ -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
}

View File

@ -32,6 +32,7 @@ const (
ContextKey = "session"
CurrentUserKey = "current_user"
CSRFKey = "csrf"
RequestTimeKey = "req_time"
)
// New creates a blank session object.

View File

@ -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(
`<strong style="color: #0077FF">non</strong>` +
@ -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 {

View File

@ -362,6 +362,9 @@
<a href="/signup">Sign up</a>
</div>
{{end}}
<div class="column">
<small class="has-text-grey is-size-7"><i class="fa-regular fa-clock"></i> {{RunTime .Request}}</small>
</div>
</div>
</div>
</div>