small pixel drawing of a pufferfish existentialcrisis.sh

main.go

// test
package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"time"

	"git.j3s.sh/existentialcrisis.sh/quote"
)

func main() {
	http.HandleFunc("/", serveTemplate)
	http.HandleFunc("/api/v1/crisis/quote", quoteHandler)
	http.HandleFunc("/api/v1/crisis/saturday", saturdayHandler)
	http.HandleFunc("/api/v1/crisis/remember", rememberHandler)
	http.HandleFunc("/api/v1/crisis/percentage", percentageHandler)

	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)
}

func saturdayHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")

	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
	weeksLived := age * 52

	var text string
	if age > 77 {
		text = ""
	} else {
		text = fmt.Sprintf("you have lived through ~%d saturdays\n\n~%d saturdays remain", weeksLived, weeksToLive)
	}

	fmt.Fprintf(w, text)
}

func rememberHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")

	yearsraw, _ := r.URL.Query()["years"]
	years, err := strconv.Atoi(yearsraw[0])
	if err != nil {
		http.Error(w, http.StatusText(500), 500)
		return
	}
	ageraw, _ := r.URL.Query()["age"]
	age, err := strconv.Atoi(ageraw[0])
	if err != nil {
		http.Error(w, http.StatusText(500), 500)
		return
	}

	if !(1 <= years && years <= 1000) {
		http.Error(w, http.StatusText(500), 500)
		return
	}
	if !(1 <= age && age <= 100) {
		http.Error(w, http.StatusText(500), 500)
		return
	}

	t := time.Now()
	nowYear := t.Year()
	predictionYear := nowYear + years
	predictionAge := age + years

	var firstTense string
	var secondTense string
	var thirdTense string
	var peopleWhoKnowYou int
	var peopleWhoLoveYouDearly int
	var peopleWhoKnewYouThatDied int
	var peopleWhoLovedYouThatDied int
	var status string
	if predictionAge < 77 {
		status = "alive"
	} else {
		status = "dead"
		yearsPostDeath := predictionAge - 77
		peopleWhoKnewYouThatDied = yearsPostDeath * 10
		peopleWhoLovedYouThatDied = yearsPostDeath * 1
	}

	if status == "alive" {
		firstTense = "you are"
		secondTense = "know you"
		thirdTense = "love"
		peopleWhoKnowYou = predictionAge * 10
		peopleWhoLoveYouDearly = predictionAge * 1
	} else {
		firstTense = "you would have been"
		secondTense = "remember you"
		thirdTense = "loved"
		peopleWhoKnowYou = 770 - peopleWhoKnewYouThatDied
		peopleWhoLoveYouDearly = 77 - peopleWhoLovedYouThatDied
	}

	if peopleWhoKnowYou < 0 {
		peopleWhoKnowYou = 0
	}

	if peopleWhoLoveYouDearly < 0 {
		peopleWhoLoveYouDearly = 0
	}

	var text string
	if peopleWhoKnowYou == 0 && peopleWhoLoveYouDearly == 0 {
		text = fmt.Sprintf("in the year %d you are %s. nobody remembers you.", predictionYear, status)
	} else {
		text = fmt.Sprintf("in the year %d you are %s. %s %d. %d people %s, and %d of them %s you dearly.", predictionYear, status, firstTense, predictionAge, peopleWhoKnowYou, secondTense, peopleWhoLoveYouDearly, thirdTense)
	}

	fmt.Fprintf(w, text)
}

func percentageHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")

	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
	}
	agef := float64(age)

	percentageCompleted := agef / 77.0 * 100

	text := fmt.Sprintf("%f", percentageCompleted)

	fmt.Fprintf(w, text)
}