From 7943865cc66b80eb54e06ec304bccef0d3a44151 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 29 Nov 2022 21:32:41 +0100 Subject: [PATCH] Move mediocregopher.com content to index of this site --- src/http/api.go | 2 +- src/http/index.go | 61 +-------------------------------- src/http/posts.go | 74 +++++++++++++++++++++++++++++++++++++++++ src/http/tpl/base.html | 6 ++-- src/http/tpl/index.html | 59 ++++++++++++++++++++------------ src/http/tpl/posts.html | 35 +++++++++++++++++++ 6 files changed, 153 insertions(+), 84 deletions(-) create mode 100644 src/http/tpl/posts.html diff --git a/src/http/api.go b/src/http/api.go index cbcd182..480f826 100644 --- a/src/http/api.go +++ b/src/http/api.go @@ -190,7 +190,7 @@ func (a *api) blogHandler() http.Handler { mux.Handle("/posts/", http.StripPrefix("/posts", apiutil.MethodMux(map[string]http.Handler{ - "GET": a.getPostHandler(), + "GET": a.getPostsHandler(), "EDIT": a.editPostHandler(false), "MANAGE": a.managePostsHandler(), "POST": a.postPostHandler(), diff --git a/src/http/index.go b/src/http/index.go index a392361..21c6c16 100644 --- a/src/http/index.go +++ b/src/http/index.go @@ -1,14 +1,10 @@ package http import ( - "fmt" "net/http" "path/filepath" "regexp" "strings" - - "github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil" - "github.com/mediocregopher/blog.mediocregopher.com/srv/post" ) func (a *api) renderIndexHandler() http.Handler { @@ -35,61 +31,6 @@ func (a *api) renderIndexHandler() http.Handler { return } - page, err := apiutil.StrToInt(r.FormValue("p"), 0) - if err != nil { - apiutil.BadRequest( - rw, r, fmt.Errorf("invalid page number: %w", err), - ) - return - } - - tag := r.FormValue("tag") - - var ( - posts []post.StoredPost - hasMore bool - ) - - if tag == "" { - posts, hasMore, err = a.params.PostStore.Get(page, pageCount) - } else { - posts, err = a.params.PostStore.GetByTag(tag) - } - - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), - ) - return - } - - tags, err := a.params.PostStore.GetTags() - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fething tags: %w", err), - ) - return - } - - tplPayload := struct { - Posts []post.StoredPost - PrevPage, NextPage int - Tags []string - }{ - Posts: posts, - PrevPage: -1, - NextPage: -1, - Tags: tags, - } - - if page > 0 { - tplPayload.PrevPage = page - 1 - } - - if hasMore { - tplPayload.NextPage = page + 1 - } - - executeTemplate(rw, r, tpl, tplPayload) + executeTemplate(rw, r, tpl, nil) }) } diff --git a/src/http/posts.go b/src/http/posts.go index 1950113..ec16e4b 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -123,6 +123,80 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, return tplPayload, nil } +func (a *api) getPostsHandler() http.Handler { + + tpl := a.mustParseBasedTpl("posts.html") + getPostHandler := a.getPostHandler() + const pageCount = 10 + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := filepath.Base(r.URL.Path) + + if id != "/" { + getPostHandler.ServeHTTP(rw, r) + return + } + + page, err := apiutil.StrToInt(r.FormValue("p"), 0) + if err != nil { + apiutil.BadRequest( + rw, r, fmt.Errorf("invalid page number: %w", err), + ) + return + } + + tag := r.FormValue("tag") + + var ( + posts []post.StoredPost + hasMore bool + ) + + if tag == "" { + posts, hasMore, err = a.params.PostStore.Get(page, pageCount) + } else { + posts, err = a.params.PostStore.GetByTag(tag) + } + + if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), + ) + return + } + + tags, err := a.params.PostStore.GetTags() + if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("fething tags: %w", err), + ) + return + } + + tplPayload := struct { + Posts []post.StoredPost + PrevPage, NextPage int + Tags []string + }{ + Posts: posts, + PrevPage: -1, + NextPage: -1, + Tags: tags, + } + + if page > 0 { + tplPayload.PrevPage = page - 1 + } + + if hasMore { + tplPayload.NextPage = page + 1 + } + + executeTemplate(rw, r, tpl, tplPayload) + }) +} + func (a *api) getPostHandler() http.Handler { tpl := a.mustParseBasedTpl("post.html") diff --git a/src/http/tpl/base.html b/src/http/tpl/base.html index 350bae3..d2399f4 100644 --- a/src/http/tpl/base.html +++ b/src/http/tpl/base.html @@ -105,11 +105,13 @@ mediocregopher's lil web corner

- Posts + Home +  //  + Posts  /  Follow (RSS) -  /  +  //  License diff --git a/src/http/tpl/index.html b/src/http/tpl/index.html index 9b03531..51805b1 100644 --- a/src/http/tpl/index.html +++ b/src/http/tpl/index.html @@ -1,34 +1,51 @@ {{ define "body" }} - {{ if ge .Payload.PrevPage 0 }}

- < < Previous Page + Hi! I'm Brian, and this here's my little corner of the web. Here I write + posts + about projects I'm working on and things that interest me (which you can + follow, + if you like). Beyond that I've linked to various related links related to me + below.

- {{ else }} -

- Welcome to the Mediocre Blog! Posts are listed in chronological order. If - you aren't sure of where to start I recommend picking at random. -

- {{ end }} +

Social

+ + +

Dev

- {{ if ge .Payload.NextPage 0 }} -

- Next Page > > -

- {{ end }} +

Art

+ + +
+ +

I'm not affiliated with these, but they're worth listing.

+ {{ end }} diff --git a/src/http/tpl/posts.html b/src/http/tpl/posts.html new file mode 100644 index 0000000..ef94ffb --- /dev/null +++ b/src/http/tpl/posts.html @@ -0,0 +1,35 @@ +{{ define "body" }} + + {{ if ge .Payload.PrevPage 0 }} +

+ < < Previous Page +

+ {{ else }} +

+ Posts are listed in chronological order. If you aren't sure of where to + start I recommend picking at random. +

+ {{ end }} + + + + {{ if ge .Payload.NextPage 0 }} +

+ Next Page > > +

+ {{ end }} + +{{ end }} + +{{ template "base.html" . }}