diff --git a/.gitignore b/.gitignore index f4d432a..ccc1755 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ - +.DS_Store diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a5a4b37 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tulpa.dev/cadey/todayinmarch2020 + +go 1.16 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0f25d0d --- /dev/null +++ b/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "embed" + "encoding/json" + "flag" + "html/template" + "log" + "math/rand" + "net" + "net/http" + "os" + "time" +) + +var ( + port = flag.String("port", "23698", "TCP port to listen on") + sockPath = flag.String("socket", "", "Unix socket to listen on") + + //go:embed templates/* quips.json + content embed.FS + + quips []string +) + +func main() { + flag.Parse() + + tmpl := template.Must(template.ParseFS(content, "templates/index.html")) + + fin, err := content.Open("quips.json") + if err != nil { + log.Fatal(err) + } + + err = json.NewDecoder(fin).Decode(&quips) + if err != nil { + log.Fatal(err) + } + + mux := http.NewServeMux() + mux.Handle("/", index(tmpl)) + + var l net.Listener + if *sockPath != "" { + os.Remove(*sockPath) + l, err = net.Listen("unix", *sockPath) + } else { + l, err = net.Listen("tcp", ":"+*port) + } + + if err != nil { + log.Fatal(err) + } + + srv := &http.Server{ + Handler: mux, + } + log.Fatal(srv.Serve(l)) +} + +func index(tmpl *template.Template) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "text/html") + + then := time.Date(2020, time.March, 1, 0, 0, 0, 0, time.UTC) + now := time.Now().UTC() + dur := now.Sub(then) + + tmpl.Execute(w, struct { + Day int + Quip string + }{ + Day: int(dur.Hours() / 24), + Quip: quips[rand.Intn(len(quips))], + }) + }) +} diff --git a/quips.json b/quips.json new file mode 100644 index 0000000..5db8edf --- /dev/null +++ b/quips.json @@ -0,0 +1,14 @@ +[ + "Is today gonna be the day that time comes back to us?", + "The year that's felt like a decade.", + "How deep are you into your Netflix backlog?", + "Now without JavaScript!", + "Green Leader, standing by.", + "We've strayed into a zone with a high magical index.", + "Each second that goes by feels like an eternity and nothing.", + "Never enter an arsekicking contest with a systems developer.", + "Developers didn't kill ordinary people because a) they seldom noticed them, b) it wasn't considered sporting and c) who'd do all the cooking and growing food and things.", + "Some people think this is paranoia, but it isn't. Paranoids only think everyone is out to get them. Developers know it.", + "Weather forecast for tomorrow: RIVERS OF BLOOD ALL DAY", + "A systems programmer will know what to do when society breaks down, because the systems programmer already lives in a world without law." +] \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..64392ef --- /dev/null +++ b/templates/index.html @@ -0,0 +1,121 @@ + + + + + What Day of March, 2020 Is It? + + + + + +
+
+ Today is March {{.Day}}, 2020 + +
+
+
+
+ +

{{.Quip}}

+ +
+
+ + +
+
+ + + \ No newline at end of file