aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe <me@christine.website>2022-12-29 15:22:49 -0500
committerXe <me@christine.website>2022-12-29 15:22:49 -0500
commit8f74c3629a846c020dbccf77d49c7c792fb559ad (patch)
tree3e4e272cfcb3a9d6044c783236dea96b3e1b8fa6 /cmd
parent3bf8b46e8c8add50dfcd0594d194b318eb580606 (diff)
downloadx-8f74c3629a846c020dbccf77d49c7c792fb559ad.tar.xz
x-8f74c3629a846c020dbccf77d49c7c792fb559ad.zip
add the todayinmarch2020 service
Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/todayinmarch2020/.gitignore18
-rw-r--r--cmd/todayinmarch2020/README.md3
-rw-r--r--cmd/todayinmarch2020/go.mod3
-rw-r--r--cmd/todayinmarch2020/main.go97
-rw-r--r--cmd/todayinmarch2020/quips.json15
-rw-r--r--cmd/todayinmarch2020/templates/index.html123
6 files changed, 259 insertions, 0 deletions
diff --git a/cmd/todayinmarch2020/.gitignore b/cmd/todayinmarch2020/.gitignore
new file mode 100644
index 0000000..adb0257
--- /dev/null
+++ b/cmd/todayinmarch2020/.gitignore
@@ -0,0 +1,18 @@
+# ---> Go
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+.DS_Store
+result
diff --git a/cmd/todayinmarch2020/README.md b/cmd/todayinmarch2020/README.md
new file mode 100644
index 0000000..61f5444
--- /dev/null
+++ b/cmd/todayinmarch2020/README.md
@@ -0,0 +1,3 @@
+# todayinmarch2020
+
+Shows what day it is in March 2020 \ No newline at end of file
diff --git a/cmd/todayinmarch2020/go.mod b/cmd/todayinmarch2020/go.mod
new file mode 100644
index 0000000..a5a4b37
--- /dev/null
+++ b/cmd/todayinmarch2020/go.mod
@@ -0,0 +1,3 @@
+module tulpa.dev/cadey/todayinmarch2020
+
+go 1.16
diff --git a/cmd/todayinmarch2020/main.go b/cmd/todayinmarch2020/main.go
new file mode 100644
index 0000000..18df1b6
--- /dev/null
+++ b/cmd/todayinmarch2020/main.go
@@ -0,0 +1,97 @@
+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))
+ mux.Handle("/api", api(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 api(tmpl *template.Template) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Add("Content-Type", "application/json")
+
+ then := time.Date(2020, time.March, 1, 0, 0, 0, 0, time.UTC)
+ now := time.Now().UTC()
+ dur := now.Sub(then)
+
+ json.NewEncoder(w).Encode(struct {
+ Day int `json:"day"`
+ Quip string `json:"quip"`
+ }{
+ Day: int(dur.Hours()/24) + 1,
+ Quip: quips[rand.Intn(len(quips))],
+ })
+ })
+}
+
+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))],
+ })
+ })
+}
diff --git a/cmd/todayinmarch2020/quips.json b/cmd/todayinmarch2020/quips.json
new file mode 100644
index 0000000..239fe3b
--- /dev/null
+++ b/cmd/todayinmarch2020/quips.json
@@ -0,0 +1,15 @@
+[
+ "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.",
+ "It's not DNS, there's no way it was DNS, it was DNS."
+]
diff --git a/cmd/todayinmarch2020/templates/index.html b/cmd/todayinmarch2020/templates/index.html
new file mode 100644
index 0000000..1192223
--- /dev/null
+++ b/cmd/todayinmarch2020/templates/index.html
@@ -0,0 +1,123 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <title>What Day of March 2020 Is It?</title>
+ <style>
+ main {
+ font-family: monospace, monospace;
+ max-width: 38rem;
+ padding: 2rem 0;
+ margin: auto;
+ }
+
+ @media only screen and (max-device-width: 736px) {
+ main {
+ padding: 0rem;
+ }
+ }
+
+ ::selection {
+ background: #d3869b;
+ }
+
+ body {
+ background: #282828;
+ color: #ebdbb2;
+ }
+
+ main {
+ text-align: center;
+ }
+
+ pre {
+ background-color: #3c3836;
+ padding: 1em;
+ border: 0;
+ }
+
+ a,
+ a:active,
+ a:visited {
+ color: #b16286;
+ background-color: #1d2021;
+ }
+
+ h1,
+ h2,
+ h3,
+ h4,
+ h5 {
+ margin-bottom: .1rem;
+ }
+
+ blockquote {
+ border-left: 1px solid #bdae93;
+ margin: 0.5em 10px;
+ padding: 0.5em 10px;
+ }
+
+ footer {
+ align: center;
+ }
+
+ @media (prefers-color-scheme: light) {
+ body {
+ background: #fbf1c7;
+ color: #3c3836;
+ }
+
+ pre {
+ background-color: #ebdbb2;
+ padding: 1em;
+ border: 0;
+ }
+
+ a,
+ a:active,
+ a:visited {
+ color: #b16286;
+ background-color: #f9f5d7;
+ }
+
+ h1,
+ h2,
+ h3,
+ h4,
+ h5 {
+ margin-bottom: .1rem;
+ }
+
+ blockquote {
+ border-left: 1px solid #655c54;
+ margin: 0.5em 10px;
+ padding: 0.5em 10px;
+ }
+ }
+ </style>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+</head>
+
+<body id="top">
+ <main>
+ <big>Today is March <big>{{.Day}}</big>, 2020</big>
+
+ <br />
+ <br />
+ <br />
+ <br />
+
+ <p>{{.Quip}}</p>
+
+ <br />
+ <br />
+
+ <footer>
+ <p>From <a href="https://christine.website">Within</a> - <a
+ href="https://twitter.com/theprincessxena">@theprincessxena</a> - <a
+ href="https://tulpa.dev/cadey/todayinmarch2020">Source Code</a></p>
+ </footer>
+ </main>
+</body>
+
+</html>