diff options
| author | Xe <me@christine.website> | 2022-12-29 15:22:49 -0500 |
|---|---|---|
| committer | Xe <me@christine.website> | 2022-12-29 15:22:49 -0500 |
| commit | 8f74c3629a846c020dbccf77d49c7c792fb559ad (patch) | |
| tree | 3e4e272cfcb3a9d6044c783236dea96b3e1b8fa6 | |
| parent | 3bf8b46e8c8add50dfcd0594d194b318eb580606 (diff) | |
| download | x-8f74c3629a846c020dbccf77d49c7c792fb559ad.tar.xz x-8f74c3629a846c020dbccf77d49c7c792fb559ad.zip | |
add the todayinmarch2020 service
Signed-off-by: Xe <me@christine.website>
| -rw-r--r-- | cmd/todayinmarch2020/.gitignore | 18 | ||||
| -rw-r--r-- | cmd/todayinmarch2020/README.md | 3 | ||||
| -rw-r--r-- | cmd/todayinmarch2020/go.mod | 3 | ||||
| -rw-r--r-- | cmd/todayinmarch2020/main.go | 97 | ||||
| -rw-r--r-- | cmd/todayinmarch2020/quips.json | 15 | ||||
| -rw-r--r-- | cmd/todayinmarch2020/templates/index.html | 123 | ||||
| -rw-r--r-- | flake.nix | 113 |
7 files changed, 316 insertions, 56 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> @@ -19,8 +19,7 @@ }; }; - outputs = { self, nixpkgs, utils, gomod2nix, ckiee, rust-overlay - }@attrs: + outputs = { self, nixpkgs, utils, gomod2nix, ckiee, rust-overlay }@attrs: utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" @@ -73,6 +72,7 @@ in { packages = rec { default = everything; + license = copyFile { pname = "license"; path = "xlicense"; @@ -82,17 +82,14 @@ path = "make-mastodon-app"; }; aegis = copyFile { pname = "aegis"; }; - prefix = copyFile { pname = "prefix"; }; - quickserv = copyFile { pname = "quickserv"; }; - within-website = copyFile { pname = "within.website"; }; - johaus = copyFile { pname = "johaus"; }; cadeybot = copyFile { pname = "cadeybot"; }; + johaus = copyFile { pname = "johaus"; }; mainsanow = copyFile { pname = "mainsanow"; }; + prefix = copyFile { pname = "prefix"; }; + quickserv = copyFile { pname = "quickserv"; }; + todayinmarch2020 = copyFile { pname = "todayinmarch2020"; }; uploud = copyFile { pname = "uploud"; }; - importer = copyFile { - pname = "importer"; - path = "cadeybot-importer"; - }; + within-website = copyFile { pname = "within.website"; }; }; nixosModules = { @@ -150,65 +147,69 @@ }; }; - robocadey = { config, lib, pkgs, ... }: + todayinmarch2020 = { config, pkgs, lib, ... }: with lib; let system = pkgs.system; - cfg = config.xeserv.services.robocadey; selfpkgs = self.packages.${system}; + cfg = config.xeserv.services.todayinmarch2020; in { - options.xeserv.services.robocadey = { - enable = mkEnableOption "Activates the printerfacts server"; + options.xeserv.services.todayinmarch2020 = { + enable = + mkEnableOption "Lets you find out what day in 2020 today is"; + useACME = mkEnableOption "Enables ACME for cert stuff"; - pathToModel = mkOption { + domain = mkOption { type = types.str; - default = "/srv/models/robocadey_gpt2.raw"; - description = "model squashfs volume location"; + default = "todayinmarch2020.xn--sz8hf6d.ws"; + example = "todayinmarch2020.xn--sz8hf6d.ws"; + description = + "The domain name that nginx should check against for HTTP hostnames"; + }; + + sockPath = mkOption rec { + type = types.str; + default = "/srv/within/run/todayinmarch2020.sock"; + example = default; + description = + "The unix domain socket that printerfacts should listen on"; }; }; config = mkIf cfg.enable { - systemd.mounts = [{ - type = "squashfs"; - what = cfg.pathToModel; - where = "/var/lib/private/xeserv.robocadey-gpt2/checkpoint"; - options = "ro,relatime,errors=continue"; - }]; - systemd.services = { - "robocadey" = { - wantedBy = [ "multi-user.target" ]; - description = "RoboCadey"; - after = [ "robocadey-gpt2.socket" ]; - - serviceConfig = { - Restart = "always"; - DynamicUser = "true"; - ExecStart = "${selfpkgs.robocadey}/bin/robocadey"; - WorkingDirectory = "/var/lib/private/xeserv.robocadey"; - StateDirectory = "xeserv.robocadey"; - CacheDirectory = "xeserv.robocadey"; - }; - }; - "robocadey-gpt2" = { - wantedBy = [ "multi-user.target" ]; - description = "RoboCadey GPT2 sidecar"; - - serviceConfig = { - Restart = "always"; - DynamicUser = "true"; - ExecStart = - "${selfpkgs.robocadey-gpt2}/bin/robocadey-gpt2"; - WorkingDirectory = - "/var/lib/private/xeserv.robocadey-gpt2"; - StateDirectory = "xeserv.robocadey-gpt2"; - CacheDirectory = "xeserv.robocadey-gpt2"; - }; + users.users.todayinmarch2020 = { + createHome = true; + description = "tulpa.dev/cadey/todayinmarch2020"; + isSystemUser = true; + group = "within"; + home = "/srv/within/todayinmarch2020"; + }; + + systemd.services.todayinmarch2020 = { + wantedBy = [ "multi-user.target" ]; + after = [ "within-homedir.service" ]; + + serviceConfig = { + User = "todayinmarch2020"; + Group = "within"; + Restart = "on-failure"; + WorkingDirectory = "/srv/within/todayinmarch2020"; + RestartSec = "30s"; + UMask = "007"; }; + + script = '' + exec ${selfpkgs.todayinmarch2020}/bin/todayinmarch2020 -socket=${cfg.sockPath} + ''; }; - systemd.sockets."robocadey-gpt2" = { - description = "RoboCadey GPT-2 activation socket"; - partOf = [ "robocadey-gpt2.service" ]; - listenStreams = [ "/run/robocadey-gpt2.sock" ]; + + services.nginx.virtualHosts."todayinmarch2020" = { + serverName = "${cfg.domain}"; + locations."/".proxyPass = "http://unix:${cfg.sockPath}"; + enableACME = true; + extraConfig = '' + access_log /var/log/nginx/todayinmarch2020.access.log; + ''; }; }; }; |
