small pixel drawing of a pufferfish existentialcrisis.sh

In the beginning there was darkness
Jes Olson j3s@c3f.net
Mon, 24 Jan 2022 06:00:33 -0600
commit

cc990a12d31e184a5ea77be0d8dd837867f93b03

A go.mod

@@ -0,0 +1,3 @@

+module git.j3s.sh/existentialcrisis.sh + +go 1.16
A main.go

@@ -0,0 +1,63 @@

+package main + +import ( + "html/template" + "path/filepath" + "net/http" + "log" + "os" + "fmt" + "git.j3s.sh/existentialcrisis.sh/quote" +) + +func main() { + http.HandleFunc("/", serveTemplate) + http.HandleFunc("/api/v1/crisis/quote", quoteHandler) + + log.Println("listening on :4357 tbh") + err := http.ListenAndServe(":4357", nil) + if err != nil { + log.Fatal(err) + } +} + +func serveTemplate(w http.ResponseWriter, r *http.Request) { + lp := filepath.Join("templates", "layout.html") + if r.URL.Path == "/" { + r.URL.Path = "index.html" + } + fp := filepath.Join("templates", filepath.Clean(r.URL.Path)) + log.Println(r.URL.Path) + + info, err := os.Stat(fp) + if err != nil { + if os.IsNotExist(err) { + http.NotFound(w, r) + return + } + } + + if info.IsDir() { + http.NotFound(w, r) + return + } + + tmpl, err := template.ParseFiles(lp, fp) + if err != nil { + log.Println(err.Error()) + http.Error(w, http.StatusText(500), 500) + return + } + + err = tmpl.ExecuteTemplate(w, "layout", nil) + if err != nil { + log.Println(err.Error()) + http.Error(w, http.StatusText(500), 500) + } +} + +func quoteHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + randomQuote := quote.GetRandomQuote() + fmt.Fprintf(w, randomQuote) +}
A quote/quote.go

@@ -0,0 +1,40 @@

+package quote + +import ( + "math/rand" + "time" +) + +var quoteCollection []string + +func initQuoteCollection() { + quoteCollection = append(quoteCollection, "It is often said that before you die your life passes before your eyes. It is, in fact, true. It's called living.") + quoteCollection = append(quoteCollection, "Man always thinks about the past before he dies, as if he were frantically searching for proof that he lived.") + quoteCollection = append(quoteCollection, "There's no shame in dying for nothing. That's why most people die.") + quoteCollection = append(quoteCollection, "There are only two days in your entire life that are not 24 hours long.") + quoteCollection = append(quoteCollection, "And then one day you find ten years have got behind you. No one told you when to run, you missed the starting gun. And you run and you run to catch up with the sun but it's sinking. Racing around to come up behind you again. The sun is the same in a relative way, but you're older. Shorter of breath and one day closer to death.") + quoteCollection = append(quoteCollection, "Is all that we see or seem, but a dream within a dream?") + quoteCollection = append(quoteCollection, "Since we're all going to die it's obvious when and how don't matter.") + quoteCollection = append(quoteCollection, "There's one thing that's real clear to me: no one dies with dignity.") + quoteCollection = append(quoteCollection, "There is no meaning to life. Knowing that, be happy, be miserable, or both. You can try everything but nothing will make you something, because we are not anything.") + quoteCollection = append(quoteCollection, "you'll never be this young again.") + quoteCollection = append(quoteCollection, "tiger got to hunt. bird got to fly. man got to sit and wonder why? why? why? tiger got to sleep. bird got to land. man got to tell himself he understand.") + quoteCollection = append(quoteCollection, "I was assailed by memories of a life that wasn't mine anymore, but one in which I'd found the simplest and most lasting joys: the smells of summer, the part of town I loved, a certain evening sky, Marie's dresses and the way she laughed.") + quoteCollection = append(quoteCollection, "Every existing thing is born without reason, prolongs itself out of weakness and dies by chance.") + quoteCollection = append(quoteCollection, "Life has no meaning the moment you lose the illusion of being eternal.") + quoteCollection = append(quoteCollection, "All human activities are equivalent. All are on principle doomed to failure.") + quoteCollection = append(quoteCollection, "Time is too large. It can’t be filled up. Everything you plunge into it is stretched and disintegrates.") + quoteCollection = append(quoteCollection, "All the labor of all the ages, all the devotion, all the inspiration, all the noonday brightness of human genius are destined to extinction.") + quoteCollection = append(quoteCollection, "And yet sometimes we become the person we most dread. Or maybe we dread most the person we know we are to become.") + quoteCollection = append(quoteCollection, "") + quoteCollection = append(quoteCollection, "") + quoteCollection = append(quoteCollection, "") + quoteCollection = append(quoteCollection, "") + quoteCollection = append(quoteCollection, "") +} + +func GetRandomQuote() string { + initQuoteCollection() + rand.Seed(time.Now().UnixNano()) + return quoteCollection[rand.Intn(len(quoteCollection))] +}
A templates/api.html

@@ -0,0 +1,17 @@

+{{define "title"}}existentialcrisis.sh/api{{end}} +{{define "body"}} +<p>The below tools should all be very helpful in triggering an existential crisis. They are used inadvertently by the author every day.</p> +<h4>receive an existential quote that may trigger a crisis</h4> +<pre>GET <a href="/api/v1/crisis/quote">/api/v1/crisis/quote</a></pre> + +<h4>estimate the amount of time you have left alive on average, based on your age</h4> +<pre>GET <a href="/api/v1/crisis/life?age=28">/api/v1/crisis/life?age=N</a> +N = your age</pre> + +<h4>estimate the number of people who will remember you in a given number of years</h4> +<pre>GET <a href="/api/v1/crisis/remember?years=100">/api/v1/crisis/remember?years=N</a> +N = number of years</pre> + +<h4>estimate how valuable your legacy will be</h4> +<pre>GET <a href="/api/v1/crisis/legacy">/api/v1/crisis/legacy</a></pre> +{{end}}
A templates/features.html

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

+{{define "title"}}existentialcrisis.sh/features{{end}} +{{define "body"}} +<p>everyone will die, and everything will end. ultimately our features don't matter. +{{end}}
A templates/index.html

@@ -0,0 +1,7 @@

+{{define "title"}}existentialcrisis.sh{{end}} +{{define "body"}} +<p>existentialcrisis.sh is a powerful API toolkit that you may use if you'd like to have an existential crisis.</p> +<p>it's very simple to use - we offer a variety of methods for inducing dread.</p> +<p>see our <a href="/api.html">api docs</a>.</p> +<p>ugh</p> +{{end}}
A templates/layout.html

@@ -0,0 +1,15 @@

+{{define "layout"}} +<!doctype html> +<html> +<head> + <!-- a j3s.sh production --> + <meta charset="utf-8"> + <title>{{template "title"}}</title> +</head> +<body> + <h1><a href="/">😱</a> existentialcrisis.sh</h1> +<p><a href="/api.html">api</a> <a href="/features.html">features</a> <a href="/pricing.html">pricing</a> <a href="/why.html">WHY??</a></p> + {{template "body"}} +</body> +</html> +{{end}}
A templates/pricing.html

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

+{{define "title"}}existentialcrisis.sh/pricing{{end}} +{{define "body"}} +<p>what's the point of pricing anything?</p> +{{end}}