blob: 9aa6a2d5580785e1de879ddf515c951f9eedcf19 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
|