Moar lifeHandler enhancements
Jes Olson j3s@c3f.net
Mon, 24 Jan 2022 07:06:49 -0600
2 files changed,
23 insertions(+),
7 deletions(-)
M
main.go
→
main.go
@@ -5,6 +5,7 @@ "html/template"
"path/filepath" "net/http" "log" + "strconv" "os" "fmt" "git.j3s.sh/existentialcrisis.sh/quote"@@ -65,22 +66,37 @@ }
func lifeHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") - // hardcoded for now - age := 29 + + ageraw, _ := r.URL.Query()["age"] + age, err := strconv.Atoi(ageraw[0]) + if err != nil { + http.Error(w, http.StatusText(500), 500) + return + } + + if ! (1 <= age && age <= 100) { + http.Error(w, http.StatusText(500), 500) + return + } + yearsTilDeath := 77 - age weeksToLive := yearsTilDeath * 52 - text := "weeks of your life:\n" + text := "saturdays of your life:\n\n" weeksOfLifeOnAverage := 4004 weekOfLifeYouAreOn := weeksOfLifeOnAverage - weeksToLive for i := 0; i < weeksOfLifeOnAverage; i++ { - if i % 100 == 0 { - text = text + "*\n" - } else if i == weekOfLifeYouAreOn { + if i == weekOfLifeYouAreOn { text = text + "\n[*] you are here\n" log.Println(i) + } else if i % 50 == 0 { + text = text + "*\n" } else { text = text + "*" + } + + if age >= 77 { + text = "any day now" } }
M
templates/api.html
→
templates/api.html
@@ -6,7 +6,7 @@ <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> +N = your age (1-100)</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>