diff options
-rw-r--r-- | src/http/api.go | 2 | ||||
-rw-r--r-- | src/http/index.go | 61 | ||||
-rw-r--r-- | src/http/posts.go | 74 | ||||
-rw-r--r-- | src/http/tpl/base.html | 6 | ||||
-rw-r--r-- | src/http/tpl/index.html | 59 | ||||
-rw-r--r-- | src/http/tpl/posts.html | 35 |
6 files changed, 153 insertions, 84 deletions
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 @@ <strong>mediocregopher</strong>'s lil web corner <br/> <br/> - <a href="{{ BlogURL "/" }}">Posts</a> + <a href="{{ BlogURL "/" }}">Home</a> + // + <a href="{{ BlogURL "/posts" }}">Posts</a> / <a href="{{ BlogURL "follow" }}">Follow</a> (<a href="{{ BlogURL "feed.xml" }}">RSS</a>) - / + // <a href="{{ StaticURL "wtfpl.txt" }}">License</a> </header> 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 }} <p> - <a href="?p={{ .Payload.PrevPage}}">< < Previous Page</a> + Hi! I'm Brian, and this here's my little corner of the web. Here I write + <a href="{{ BlogURL "posts" }}">posts</a> + about projects I'm working on and things that interest me (which you can + <a href="{{ BlogURL "follow" }}">follow</a>, + if you like). Beyond that I've linked to various related links related to me + below. </p> - {{ else }} - <p> - 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. - </p> - {{ end }} + <h2>Social</h2> + <ul> + <li><a href="https://matrix.to/#/@mediocregopher:waffle.farm">@mediocregopher:waffle.farm</a> is my matrix handle.</li> + <li><a href="https://social.cryptic.io/@mediocregopher">@mediocregopher@social.cryptic.io</a> is my mastodon handle.</li> + <li><a href="https://bgpicciano.com">bgpicciano.com</a> is my cover site/resume.</li> + <li><a href="mailto:mediocregopher@gmail.com">mediocregopher@gmail.com</a> is my email.</li> + <li><a href="https://news.cryptic.io">Cryptic News</a> aggregates interesting blogs.</li> + </ul> + + <h2>Dev</h2> <ul> - {{ range .Payload.Posts }} <li> - <strong><a href="{{ PostURL .ID }}"> - {{ DateTimeFormat .PublishedAt }} / {{ .Title }} - </a></strong> - {{ if .Description }} - <br/><em>{{ .Description }}</em> - {{ end }} + <a href="https://code.betamike.com/mediocregopher">Gitea</a> + and + <a href="https://github.com/mediocregopher">GitHub</a> + are for open-source code I've written. </li> - {{ end }} + <li><a href="https://github.com/mediocregopher/radix">radix</a> is for using redis with go.</li> + <li><a href="https://code.betamike.com/cryptic-io/cryptic-net">cryptic-net</a> implements the foundation of a community infrastucture.</li> + <li><a href="https://code.betamike.com/mediocregopher/mediocre-blog">mediocre-blog</a> is the CMS I designed from scratch to run this site.</li> </ul> - {{ if ge .Payload.NextPage 0 }} - <p> - <a href="?p={{ .Payload.NextPage}}">Next Page > ></a> - </p> - {{ end }} + <h2>Art</h2> + <ul> + <li><a href="https://opensea.io/mediocregopher?tab=created">OpenSea</a> lists some NFTs I've made.</li> + <li><a href="https://exchange.art/artists/mediocregopher/series">Exchange.art</a> lists others.</li> + </ul> + + <hr/> + + <p>I'm not affiliated with these, but they're worth listing.</p> + <ul> + <li><a href="https://search.marginalia.nu/">Marginalia</a> reminds me of the old internet.</li> + <li><a href="https://drewdevault.com/2020/11/01/What-is-Gemini-anyway.html">Gemini</a> is a protocol I soon hope to add to my blog.</li> + <li><a href="https://www.nts.live/">NTS</a> is a great internet radio station.</li> + <li><a href="https://yamakan.place/palestine/#">Radio alHara</a> is another great internet radio station.</li> + </ul> {{ 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 }} + <p> + <a href="?p={{ .Payload.PrevPage}}">< < Previous Page</a> + </p> + {{ else }} + <p> + Posts are listed in chronological order. If you aren't sure of where to + start I recommend picking at random. + </p> + {{ end }} + + <ul> + {{ range .Payload.Posts }} + <li> + <strong><a href="{{ PostURL .ID }}"> + {{ DateTimeFormat .PublishedAt }} / {{ .Title }} + </a></strong> + {{ if .Description }} + <br/><em>{{ .Description }}</em> + {{ end }} + </li> + {{ end }} + </ul> + + {{ if ge .Payload.NextPage 0 }} + <p> + <a href="?p={{ .Payload.NextPage}}">Next Page > ></a> + </p> + {{ end }} + +{{ end }} + +{{ template "base.html" . }} |