Add DB indexes and request time to page footer
This commit is contained in:
parent
28111585ef
commit
80c4471017
|
@ -3,6 +3,7 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.nonshy.com/nonshy/website/pkg/config"
|
"code.nonshy.com/nonshy/website/pkg/config"
|
||||||
"code.nonshy.com/nonshy/website/pkg/log"
|
"code.nonshy.com/nonshy/website/pkg/log"
|
||||||
|
@ -18,6 +19,9 @@ func CSRF(handler http.Handler) http.Handler {
|
||||||
token := MakeCSRFCookie(r, w)
|
token := MakeCSRFCookie(r, w)
|
||||||
ctx := context.WithValue(r.Context(), session.CSRFKey, token)
|
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 it's a JSON post, allow it thru.
|
||||||
if r.Header.Get("Content-Type") == "application/json" {
|
if r.Header.Get("Content-Type") == "application/json" {
|
||||||
handler.ServeHTTP(w, r.WithContext(ctx))
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
|
|
@ -18,7 +18,7 @@ type Comment struct {
|
||||||
UserID uint64 `gorm:"index"`
|
UserID uint64 `gorm:"index"`
|
||||||
User User `json:"-"`
|
User User `json:"-"`
|
||||||
Message string
|
Message string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time `gorm:"index"`
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ type Friend struct {
|
||||||
Approved bool `gorm:"index"`
|
Approved bool `gorm:"index"`
|
||||||
Ignored bool
|
Ignored bool
|
||||||
CreatedAt time.Time
|
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.
|
// AddFriend sends a friend request or accepts one if there was already a pending one.
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
type Like struct {
|
type Like struct {
|
||||||
ID uint64 `gorm:"primaryKey"`
|
ID uint64 `gorm:"primaryKey"`
|
||||||
UserID uint64 `gorm:"index"` // who it belongs to
|
UserID uint64 `gorm:"index"` // who it belongs to
|
||||||
TableName string
|
TableName string `gorm:"index"`
|
||||||
TableID uint64
|
TableID uint64 `gorm:"index"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time `gorm:"index"`
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,10 +20,10 @@ type Photo struct {
|
||||||
Filesize int64
|
Filesize int64
|
||||||
Caption string
|
Caption string
|
||||||
Flagged bool // photo has been reported by the community
|
Flagged bool // photo has been reported by the community
|
||||||
Visibility PhotoVisibility
|
Visibility PhotoVisibility `gorm:"index"`
|
||||||
Gallery bool // photo appears in the public gallery (if public)
|
Gallery bool `gorm:"index"` // photo appears in the public gallery (if public)
|
||||||
Explicit bool // is an explicit photo
|
Explicit bool `gorm:"index"` // is an explicit photo
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time `gorm:"index"`
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ const (
|
||||||
ContextKey = "session"
|
ContextKey = "session"
|
||||||
CurrentUserKey = "current_user"
|
CurrentUserKey = "current_user"
|
||||||
CSRFKey = "csrf"
|
CSRFKey = "csrf"
|
||||||
|
RequestTimeKey = "req_time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a blank session object.
|
// New creates a blank session object.
|
||||||
|
|
|
@ -40,6 +40,7 @@ func TemplateFuncs(r *http.Request) template.FuncMap {
|
||||||
"ToHTML": ToHTML,
|
"ToHTML": ToHTML,
|
||||||
"PhotoURL": photo.URLPath,
|
"PhotoURL": photo.URLPath,
|
||||||
"Now": time.Now,
|
"Now": time.Now,
|
||||||
|
"RunTime": RunTime,
|
||||||
"PrettyTitle": func() template.HTML {
|
"PrettyTitle": func() template.HTML {
|
||||||
return template.HTML(fmt.Sprintf(
|
return template.HTML(fmt.Sprintf(
|
||||||
`<strong style="color: #0077FF">non</strong>` +
|
`<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.
|
// 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 {
|
func BlurExplicit(r *http.Request) func(*models.Photo) bool {
|
||||||
return func(photo *models.Photo) bool {
|
return func(photo *models.Photo) bool {
|
||||||
|
|
|
@ -362,6 +362,9 @@
|
||||||
<a href="/signup">Sign up</a>
|
<a href="/signup">Sign up</a>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user