From 314ae26e1816313bde8e0226b1b3b15cc22365f1 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 28 Apr 2021 22:34:22 -0500 Subject: [PATCH] Fix feed pagination And tweak perf log output --- src/auth/session.go | 2 -- src/perf/perf.go | 4 +++- src/website/feed.go | 8 +++++--- src/website/requesthandling.go | 8 ++------ src/website/routes.go | 7 +++++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/auth/session.go b/src/auth/session.go index a41e7e5f..ad81e118 100644 --- a/src/auth/session.go +++ b/src/auth/session.go @@ -118,8 +118,6 @@ func PeriodicallyDeleteExpiredSessions(ctx context.Context, conn *pgxpool.Pool) if err == nil { if n > 0 { logging.Info().Int64("num deleted sessions", n).Msg("Deleted expired sessions") - } else { - logging.Debug().Msg("no sessions to delete") } } else { logging.Error().Err(err).Msg("Failed to delete expired sessions") diff --git a/src/perf/perf.go b/src/perf/perf.go index e1eeaaca..b374b395 100644 --- a/src/perf/perf.go +++ b/src/perf/perf.go @@ -7,15 +7,17 @@ import ( type RequestPerf struct { Route string + Path string // the path actually matched Start time.Time End time.Time Blocks []PerfBlock } -func MakeNewRequestPerf(route string) *RequestPerf { +func MakeNewRequestPerf(route string, path string) *RequestPerf { return &RequestPerf{ Start: time.Now(), Route: route, + Path: path, } } diff --git a/src/website/feed.go b/src/website/feed.go index b41629ad..39f98a1f 100644 --- a/src/website/feed.go +++ b/src/website/feed.go @@ -1,6 +1,7 @@ package website import ( + "fmt" "math" "net/http" "strconv" @@ -8,7 +9,6 @@ import ( "git.handmade.network/hmn/hmn/src/db" "git.handmade.network/hmn/hmn/src/hmnurl" - "git.handmade.network/hmn/hmn/src/logging" "git.handmade.network/hmn/hmn/src/models" "git.handmade.network/hmn/hmn/src/oops" "git.handmade.network/hmn/hmn/src/templates" @@ -62,7 +62,10 @@ func Feed(c *RequestContext) ResponseData { Current: page, Total: numPages, - // TODO: urls + FirstUrl: hmnurl.Url("/feed", nil), + LastUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", numPages), nil), + NextUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", page+1), nil), + PreviousUrl: hmnurl.Url(fmt.Sprintf("/feed/%d", page-1), nil), } var currentUserId *int @@ -128,7 +131,6 @@ func Feed(c *RequestContext) ResponseData { } parents := postResult.Cat.GetHierarchy(c.Context(), c.Conn) - logging.Debug().Interface("parents", parents).Msg("") var breadcrumbs []templates.Breadcrumb breadcrumbs = append(breadcrumbs, templates.Breadcrumb{ diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index bf3463a9..845fe05f 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -68,11 +68,7 @@ func (rb *RouteBuilder) POST(regexStr string, h Handler) { } func (rb *RouteBuilder) StdHandler(regexStr string, h http.Handler) { - rb.Router.Routes = append(rb.Router.Routes, Route{ - Method: "", - Regex: regexp.MustCompile(regexStr), - Handler: WrapStdHandler(h), - }) + rb.Handle("", regexStr, WrapStdHandler(h)) } func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { @@ -88,7 +84,7 @@ func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } c := &RequestContext{ - Route: "", // TODO + Route: route.Regex.String(), Logger: logging.GlobalLogger(), Req: req, } diff --git a/src/website/routes.go b/src/website/routes.go index e684aecd..7079bab3 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -72,6 +72,9 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt }) mainRoutes.GET(`^/feed(/(?P.+)?)?$`, Feed) + // mainRoutes.GET(`^/(?Pforums(/cat)*)$`, Category) + // mainRoutes.GET(`^/(?Pforums(/cat)*)/t/(?P\d+)/p/(?P\d+)$`, ForumPost) + mainRoutes.GET("^/assets/project.css$", ProjectCSS) mainRoutes.AnyMethod("", FourOhFour) @@ -212,7 +215,7 @@ func getCurrentUser(c *RequestContext, sessionId string) (*models.User, error) { } func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (after func()) { - c.Perf = perf.MakeNewRequestPerf(c.Route) + c.Perf = perf.MakeNewRequestPerf(c.Route, c.Req.URL.Path) return func() { c.Perf.EndRequest() log := logging.Info() @@ -224,7 +227,7 @@ func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (aft log.Str(fmt.Sprintf("At %9.2fms", c.Perf.MsFromStart(&block)), fmt.Sprintf("%*.s[%s] %s (%.4fms)", len(blockStack)*2, "", block.Category, block.Description, block.DurationMs())) blockStack = append(blockStack, block.End) } - log.Msg(fmt.Sprintf("Served %s in %.4fms", c.Perf.Route, float64(c.Perf.End.Sub(c.Perf.Start).Nanoseconds())/1000/1000)) + log.Msg(fmt.Sprintf("Served %s in %.4fms", c.Perf.Path, float64(c.Perf.End.Sub(c.Perf.Start).Nanoseconds())/1000/1000)) perfCollector.SubmitRun(c.Perf) } }