small pixel drawing of a pufferfish j3s.sh

add atom feed! :D
Jesse Olson j3s@c3f.net
Sat, 16 Apr 2022 06:06:50 +0000
commit

1951fa9e8f80a806e9b5a804b3e2070015b6eb1b

parent

96223dce3619a36e64cafe0c92d0ab8d4abca7ec

A atom/atom.go

@@ -0,0 +1,125 @@

+package atom + +import ( + "bufio" + "fmt" + "log" + "net/http" + "os" + "path/filepath" + "strings" + "time" +) + +const maxFeed = 10 + +type post struct { + title string + updated string + link string + // starting with content disabled because a lot of my content is nonsensical + // to any reasonable xml parser tbh + // content string +} + +func Handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/atom+xml") + + posts, err := getPosts("thought/*") + if err != nil { + log.Println(err) + } + + // TODO: sort them by date, then + if len(posts) > maxFeed { + posts = posts[:maxFeed] + } + + var updated string + if len(posts) > 0 { + updated = posts[0].updated + } + + // SET HEADER TYPE TBH + fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?> +<feed xmlns="http://www.w3.org/2005/Atom"> + + <title>j3s.sh</title> + <link rel="self" href="https://j3s.sh/feed.atom" /> + <link href="https://j3s.sh/" /> + <updated>%s</updated> + <author> + <name>Jes Olson</name> + </author> + <id>https://j3s.sh/</id> +`, updated) + + for _, p := range posts { + // add content in future? idk prob not + fmt.Fprintf(w, ` + <entry> + <title>%s</title> + <link href="%s"/> + <id>%s</id> + <updated>%s</updated> + </entry> +`, p.title, p.link, p.link, p.updated) + } + fmt.Fprint(w, ` +</feed>`) +} + +func getPosts(dir string) ([]post, error) { + var posts []post + files, err := filepath.Glob(dir) + if err != nil { + return nil, err + } + + for _, f := range files { + post, err := fileToPost(f) + if err != nil { + // this can be failed intentionally for drafts or posts too small for rss updoots + // add a print statement here if blog posts mysteriously vanish from the atom feed + // + // lmao + continue + } + posts = append(posts, post) + } + + return posts, err +} + +func fileToPost(file string) (post, error) { + var p post + + f, err := os.Open(file) + if err != nil { + return p, err + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Scan() // this moves to the next token + // strip spaces + title := scanner.Text() + p.title = strings.TrimSpace(title) + scanner.Scan() + timestr := strings.TrimSpace(scanner.Text()) + updated, err := time.Parse(timeLayoutPost, timestr) + if err != nil { + return p, err + } + // for scanner.Scan() { + // p.content = p.content + scanner.Text() + // } + p.updated = updated.Format(timeLayoutAtom) + p.link = "https://j3s.sh/" + file + return p, err +} + +type TimeStr string + +var timeLayoutPost = "2006-01-02" +var timeLayoutAtom = "2006-01-02T15:04:05.000Z"
M main.gomain.go

@@ -14,6 +14,7 @@ "strings"

"filippo.io/age" "filippo.io/age/armor" + "git.j3s.sh/j3s.sh/atom" "git.j3s.sh/j3s.sh/feed" "github.com/SlyMarbo/rss" )

@@ -45,6 +46,7 @@

fs := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) + http.HandleFunc("/feed.atom", atom.Handler) http.HandleFunc("/age.html", serveAge) http.HandleFunc("/ip", ipHandler) http.HandleFunc("/review/", babyHandler)

@@ -101,10 +103,8 @@

func babyHandler(w http.ResponseWriter, r *http.Request) { lp := filepath.Join("templates", "simple-layout.html") fp := filepath.Join(strings.TrimPrefix(filepath.Clean(r.URL.Path), "/")) - // load the named .txt file for processing - txt := strings.ReplaceAll(fp, ".html", ".txt") - info, err := os.Stat(txt) + info, err := os.Stat(fp) if err != nil { if os.IsNotExist(err) { http.NotFound(w, r)

@@ -117,7 +117,7 @@ http.NotFound(w, r)

return } - content, err := os.ReadFile(txt) + content, err := os.ReadFile(fp) if err != nil { log.Println(err.Error()) http.Error(w, http.StatusText(500), 500)
D templates/git.html

@@ -1,6 +0,0 @@

-{{define "title"}}jes shit git{{end}} -{{define "body"}} -<p><i>my shitty git server embed</i></p> -<p><i>(also available at <a href="https://git.j3s.sh">git.j3s.sh</a>)</p></i> -<embed width=800 height=800 src="https://git.j3s.sh"></embed> -{{end}}
M templates/layout.htmltemplates/layout.html

@@ -33,7 +33,8 @@

<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{template "title"}}</title> - <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22><text y=%2226%22 font-size=%2226%22>🐈‍⬛</text></svg>"> + <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22><text y=%2226%22 font-size=%2226%22>🐈‍⬛</text></svg>" /> + <link rel="alternate" title="Sitewide Atom feed" type="application/atom+xml" href="/feed.atom" /> </head> <body class="grayscale">
M templates/thoughts.htmltemplates/thoughts.html

@@ -2,6 +2,7 @@ {{define "title"}}small thoughts and ideas{{end}}

{{define "body"}} <p><i>these are all tiny thoughts</p></i> <p><i>that don't deserve their own blog posts</p></i> +<p><i>i hear u want <a href="/feed.atom">rss???</a></i></p> <h4>2022</h4> <li><a href="/thought/my-website-is-one-binary.html">my personal website is one binary</a></li> <li><a href="/thought/there-is-beauty-in-the-minimalism-of-email.html">there is beauty in the minimalism of email</a></li>
M thought/alpine-rock64.txtthought/alpine-rock64.html

@@ -1,4 +1,4 @@

-# installing alpine linux on a rock64 +installing alpine linux on a rock64 (notes) note: these instructions do not work yet, this document is still alive (and may be for quite awhile).
M thought/building-a-fixed-gear-bicycle.txtthought/building-a-fixed-gear-bicycle.html

@@ -1,4 +1,5 @@

-# Building a Fixed Gear Bicycle +building a fixed gear bicycle +2020-12-20 originally posted 2020-07-02
M thought/fail2ban-sux.txtthought/fail2ban-sux.html

@@ -1,4 +1,5 @@

-Date: 2020/03/22 +fail2ban sucks +2020-03-22 -> fail2ban sucks
M thought/git-necronomicon.txtthought/git-necronomicon.html

@@ -1,4 +1,7 @@

-<h1># j3s’s git notes / guide / necronomicon</h1> +j3s's git necronomicon +DRAFT DRAFT DRAFT DRAFT +WARNING: THERE BE UNFINISHED HTML AHEAD + <p><em>for somewhat experienced computer-ers who want to git good</em></p> <p>git is a complicated system, but i hope to give you an understanding that is sensible. this guide is mainly for people who have a grasp on the command line and some fuzzy git concepts in their head, and want to turn that knowledge into a functional understanding.</p> <p>i don’t care about fully understanding git from head to toe, i am more concerned with the practical usage and application of the system than anything. most guides teach you about how git uses SHA1SUMs under the hood and blah blah blah. i don’t really care, i just want to show you how i use the system and go over some very common road bumps. you can read a 200 page thesis on the inner workings if you want - but i mostly don’t care.</p>

@@ -618,4 +621,4 @@ git commit -m &#39;change file1&#39;

git send-email --to=&quot;mailing-list@example.org&quot; HEAD^ # HEAD^ is a shortcut for &quot;the latest 1 commit&quot;</code></pre> <h2 id="i-accidentally-pushed-a-secret-to-a-public-repo">i accidentally pushed a secret to a public repo</h2> <p>public repos are scraped constantly, the best possible option is to rotate the credential <em>immediately</em> and leave the old credential in git.</p> -the second best option is to force overwrite history lololol but nobody does that, right? +the second best option is to force overwrite history lololol but nobody does that, right?
M thought/holy-is-thy-name.txtthought/holy-is-thy-name.html

@@ -1,4 +1,5 @@

HOLY IS THE NAME (OF MY RUTHLESS AXE) +2020-04-20 "All that pain. I thought to myself, I gotta fight, I'm gonna die. This guy is going to play with me, play with
M thought/my-website-is-one-binary.txtthought/my-website-is-one-binary.html

@@ -1,4 +1,5 @@

my website is one binary + 2022-04-01 ---------------------------- a.k.a. this one weird trick that inspires me to program creatively

@@ -283,3 +284,5 @@ WITH MUCH LOVE.

JES + +~2022-04-01
M thought/pumpernickel-bread.txtthought/pumpernickel-bread.html

@@ -1,3 +1,6 @@

+pumpernickel bread +2020-04-20 + dry: 400g rye flour 200g whole wheat flour
M thought/something-is-different.txtthought/something-is-different.html

@@ -1,3 +1,6 @@

+something is different +2020-04-20 + the smell of my bedroom the way his hair curls how my sheets in the morning
M thought/storing-passwords-with-age.txtthought/storing-passwords-with-age.html

@@ -1,8 +1,8 @@

-my finally final password encryption method - +storing passwords with age +2021-04-20 -MY QUEST FOR A PASSWORD STORE THAT WORKS FOR ME -=============================================== + MY QUEST FOR A PASSWORD STORE THAT WORKS FOR ME + =============================================== i have tried many times to use a tool that encrypts my passwords, and nearly every time i give in to convinience.
M thought/there-is-beauty-in-the-minimalism-of-email.txtthought/there-is-beauty-in-the-minimalism-of-email.html

@@ -1,3 +1,6 @@

+there is beauty in the minimalism of email +2022-03-23 + i have a quest for you - to make email something you cherish. something that brings anticipation, thought, and personality to your life.
M thought/using-discord-is-morally-lazy.txtthought/using-discord-is-morally-lazy.html

@@ -1,4 +1,4 @@

-<h3>Using Discord is Morally Lazy</h3> +using discord is morally lazy !unfinished article warning!