about summary refs log tree commit diff
path: root/users/tazjin/cursed/responder.nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@tvl.su>2024-08-26T12·28+0300
committertazjin <tazjin@tvl.su>2024-08-27T10·02+0000
commitd292203235ad69eea045febad7d59e2ac0e3a37d (patch)
tree20215ae8144ce2be25cdbbcf1dfe556d6ac1dbb8 /users/tazjin/cursed/responder.nix
parentb9a614a90fc33cfbd1fc76675390141724996b1e (diff)
feat(tazjin/cursed): add bubblegum demo from VolgaSprint talk r/8592
Change-Id: I4d9bd21e3dd6dbc24d46907e6270d49b97f50b99
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12352
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'users/tazjin/cursed/responder.nix')
-rw-r--r--users/tazjin/cursed/responder.nix76
1 files changed, 76 insertions, 0 deletions
diff --git a/users/tazjin/cursed/responder.nix b/users/tazjin/cursed/responder.nix
new file mode 100644
index 000000000000..9aa6a2d55807
--- /dev/null
+++ b/users/tazjin/cursed/responder.nix
@@ -0,0 +1,76 @@
+{ depot, ... }:
+
+let
+  inherit (depot.users.sterni.nix.html)
+    __findFile
+    esc
+    withDoctype
+    ;
+
+  # CGI envvars: https://www.instanet.com/cgi/env.html
+  method = builtins.getEnv "REQUEST_METHOD";
+  path = builtins.getEnv "PATH_INFO";
+
+  rawQuery = builtins.getEnv "QUERY_STRING";
+  query = with builtins; let
+    pairs = (filter (s: isString s && s != "") (split "&" rawQuery));
+    tuples = filter (l: length l > 0) (map (p: filter (s: isString s) (split "=" p)) pairs);
+    mkAttr = t: {
+      name = elemAt t 0;
+      value = elemAt t 1;
+    };
+  in
+  listToAttrs (map mkAttr tuples);
+
+  default = let {
+  hasQuery = if builtins.length (builtins.attrNames query) > 0 then "?" else "";
+  body = (withDoctype (<html> { lang = "en"; } [
+    (<head> { } [
+      (<title> { } "some cursed nix")
+    ])
+    (<body> { } [
+      (<p> { } "hello volgasprint")
+      (<p> { } [ method " " path hasQuery rawQuery ])
+      (<p> { } (builtins.toJSON query))
+    ])
+  ]));
+  };
+
+  greeter = withDoctype (<html> { lang = "en"; } [
+    (<head> { } [
+      (<title> { } "hello there")
+    ])
+    (<body> { } [
+      (<p> { } "hello ${query.name or "unknown"}")
+    ])
+  ]);
+
+  weather = let {
+  town = query.town or "Kazan";
+  w = builtins.fetchurl "https://wttr.in/${town}?";
+  rendered = with depot.third_party.nixpkgs; runCommand "weather-${town}" { } ''
+    cat ${w} | ${ansi2html}/bin/ansi2html > $out
+  '';
+
+  body = builtins.readFile "${rendered}";
+  };
+
+  routes = {
+    "/other" = (withDoctype (<html> { lang = "en"; } [
+      (<head> { } [
+        (<title> { } "other endpoint")
+      ])
+      (<body> { } [
+        (<p> { } "this is another route")
+      ])
+    ]));
+    "/greeter" = greeter;
+    "/weather" = weather;
+  }."${path}" or default;
+
+in
+depot.web.bubblegum.respond "OK"
+{
+  "Content-Type" = "text/html";
+}
+  routes