Shows what day it is in March 2020 http://todayinmarch2020.xn--sz8hf6d.ws/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
1.3 KiB

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) + 1,
Quip: quips[rand.Intn(len(quips))],
})
})
}