Move mediocregopher.com content to index of this site

This commit is contained in:
Brian Picciano 2022-11-29 21:32:41 +01:00
parent 1f3ae665ed
commit 7943865cc6
6 changed files with 155 additions and 86 deletions

View File

@ -190,7 +190,7 @@ func (a *api) blogHandler() http.Handler {
mux.Handle("/posts/", http.StripPrefix("/posts", mux.Handle("/posts/", http.StripPrefix("/posts",
apiutil.MethodMux(map[string]http.Handler{ apiutil.MethodMux(map[string]http.Handler{
"GET": a.getPostHandler(), "GET": a.getPostsHandler(),
"EDIT": a.editPostHandler(false), "EDIT": a.editPostHandler(false),
"MANAGE": a.managePostsHandler(), "MANAGE": a.managePostsHandler(),
"POST": a.postPostHandler(), "POST": a.postPostHandler(),

View File

@ -1,14 +1,10 @@
package http package http
import ( import (
"fmt"
"net/http" "net/http"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
) )
func (a *api) renderIndexHandler() http.Handler { func (a *api) renderIndexHandler() http.Handler {
@ -35,61 +31,6 @@ func (a *api) renderIndexHandler() http.Handler {
return return
} }
page, err := apiutil.StrToInt(r.FormValue("p"), 0) executeTemplate(rw, r, tpl, nil)
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)
}) })
} }

View File

@ -123,6 +123,80 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload,
return tplPayload, nil 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 { func (a *api) getPostHandler() http.Handler {
tpl := a.mustParseBasedTpl("post.html") tpl := a.mustParseBasedTpl("post.html")

View File

@ -105,11 +105,13 @@
<strong>mediocregopher</strong>'s lil web corner <strong>mediocregopher</strong>'s lil web corner
<br/> <br/>
<br/> <br/>
<a href="{{ BlogURL "/" }}">Posts</a> <a href="{{ BlogURL "/" }}">Home</a>
&nbsp;//&nbsp;
<a href="{{ BlogURL "/posts" }}">Posts</a>
&nbsp;/&nbsp; &nbsp;/&nbsp;
<a href="{{ BlogURL "follow" }}">Follow</a> <a href="{{ BlogURL "follow" }}">Follow</a>
(<a href="{{ BlogURL "feed.xml" }}">RSS</a>) (<a href="{{ BlogURL "feed.xml" }}">RSS</a>)
&nbsp;/&nbsp; &nbsp;//&nbsp;
<a href="{{ StaticURL "wtfpl.txt" }}">License</a> <a href="{{ StaticURL "wtfpl.txt" }}">License</a>
</header> </header>

View File

@ -1,34 +1,51 @@
{{ define "body" }} {{ define "body" }}
{{ if ge .Payload.PrevPage 0 }}
<p> <p>
<a href="?p={{ .Payload.PrevPage}}">&lt; &lt; 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> </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> <ul>
{{ range .Payload.Posts }} <li><a href="https://matrix.to/#/@mediocregopher:waffle.farm">@mediocregopher:waffle.farm</a> is my matrix handle.</li>
<li> <li><a href="https://social.cryptic.io/@mediocregopher">@mediocregopher@social.cryptic.io</a> is my mastodon handle.</li>
<strong><a href="{{ PostURL .ID }}"> <li><a href="https://bgpicciano.com">bgpicciano.com</a> is my cover site/resume.</li>
{{ DateTimeFormat .PublishedAt }} / {{ .Title }} <li><a href="mailto:mediocregopher@gmail.com">mediocregopher@gmail.com</a> is my email.</li>
</a></strong> <li><a href="https://news.cryptic.io">Cryptic News</a> aggregates interesting blogs.</li>
{{ if .Description }}
<br/><em>{{ .Description }}</em>
{{ end }}
</li>
{{ end }}
</ul> </ul>
{{ if ge .Payload.NextPage 0 }} <h2>Dev</h2>
<p> <ul>
<a href="?p={{ .Payload.NextPage}}">Next Page &gt; &gt;</a> <li>
</p> <a href="https://code.betamike.com/mediocregopher">Gitea</a>
{{ end }} and
<a href="https://github.com/mediocregopher">GitHub</a>
are for open-source code I've written.
</li>
<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>
<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 }} {{ end }}

35
src/http/tpl/posts.html Normal file
View File

@ -0,0 +1,35 @@
{{ define "body" }}
{{ if ge .Payload.PrevPage 0 }}
<p>
<a href="?p={{ .Payload.PrevPage}}">&lt; &lt; 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 &gt; &gt;</a>
</p>
{{ end }}
{{ end }}
{{ template "base.html" . }}