Continue to polish up posts pages

This commit is contained in:
Brian Picciano 2023-01-21 16:46:11 +01:00
parent 7878db5c95
commit 293655452c
5 changed files with 92 additions and 11 deletions

View File

@ -119,6 +119,21 @@ func (a *api) Shutdown(ctx context.Context) error {
return a.srv.Shutdown(ctx)
}
func indexMiddleware(h gemini.Handler) gemini.Handler {
return gemini.HandlerFunc(func(
ctx context.Context,
rw gemini.ResponseWriter,
r *gemini.Request,
) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path += "index.gmi"
}
h.ServeGemini(ctx, rw, r)
})
}
func postsMiddleware(tplHandler gemini.Handler) gemini.Handler {
return gemini.HandlerFunc(func(
@ -156,7 +171,10 @@ func (a *api) handler() (gemini.Handler, error) {
mux.Handle("/posts/", postsMiddleware(tplHandler))
mux.Handle("/", tplHandler)
h := mux
var h gemini.Handler
h = mux
h = indexMiddleware(h)
// TODO logging
// TODO caching

View File

@ -25,6 +25,11 @@ type rendererGetPostsRes struct {
HasMore bool
}
type rendererGetPostSeriesNextPreviousRes struct {
Next *post.StoredPost
Previous *post.StoredPost
}
type renderer struct {
url *url.URL
postStore post.Store
@ -43,6 +48,41 @@ func (r renderer) GetPostByID(id string) (post.StoredPost, error) {
return p, nil
}
func (r renderer) GetPostSeriesNextPrevious(p post.StoredPost) (rendererGetPostSeriesNextPreviousRes, error) {
seriesPosts, err := r.postStore.GetBySeries(p.Series)
if err != nil {
return rendererGetPostSeriesNextPreviousRes{}, fmt.Errorf(
"fetching posts for series %q: %w", p.Series, err,
)
}
var (
res rendererGetPostSeriesNextPreviousRes
foundThis bool
)
for i := range seriesPosts {
seriesPost := seriesPosts[i]
if seriesPost.ID == p.ID {
foundThis = true
continue
}
if !foundThis {
res.Next = &seriesPost
continue
}
res.Previous = &seriesPost
break
}
return res, nil
}
func (r renderer) GetQueryValue(key, def string) string {
v := r.url.Query().Get(key)
if v == "" {
@ -97,10 +137,6 @@ func (a *api) tplHandler() (gemini.Handler, error) {
r *gemini.Request,
) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path += "index.gmi"
}
tplPath := strings.TrimPrefix(r.URL.Path, "/")
ctx = mctx.Annotate(ctx,

View File

@ -1,3 +1,3 @@
# Index
# mediocregopher's lil web corner
=> /posts/index.gmi See all posts
=> /posts/ See all posts

View File

@ -1,10 +1,10 @@
# mediocregopher's Posts
{{ $page := .GetQueryIntValue "page" 0 -}}
{{ $getPostsRes := .GetPosts $page 20 -}}
{{ $getPostsRes := .GetPosts $page 15 -}}
{{ if gt $page 0 -}}
=> /posts.gmi?page={{ .Add $page -1 }} Previous Page
=> /posts/?page={{ .Add $page -1 }} Previous Page
{{ end -}}
@ -14,5 +14,8 @@
{{ end -}}
{{ if $getPostsRes.HasMore -}}
=> /posts.gmi?page={{ .Add $page 1 }} Next page
{{ end -}}
=> /posts/?page={{ .Add $page 1 }} Next page
{{ end }}
================================================================================
=> / Home

View File

@ -7,3 +7,27 @@
{{ end -}}
{{ $post.Body }}
--------------------------------------------------------------------------------
Published {{ $post.PublishedAt.Format "2006-01-02" }}
{{- if $post.Series }}
This post is part of a series!
{{ $seriesNextPrev := .GetPostSeriesNextPrevious $post -}}
{{ if $seriesNextPrev.Next -}}
=> /posts/{{ $seriesNextPrev.Next.ID }}.gmi Next: {{ $seriesNextPrev.Next.Title }}
{{ end -}}
{{ if $seriesNextPrev.Previous -}}
=> /posts/{{ $seriesNextPrev.Previous.ID }}.gmi Previously: {{ $seriesNextPrev.Previous.Title }}
{{ end -}}
{{ end }}
================================================================================
=> /posts/ More posts
=> / Home