examples: add ucode handler example
authorJo-Philipp Wich <jo@mein.io>
Tue, 23 Nov 2021 18:25:18 +0000 (19:25 +0100)
committerJo-Philipp Wich <jo@mein.io>
Tue, 23 Nov 2021 18:26:43 +0000 (19:26 +0100)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
examples/ucode/dump-env.uc [new file with mode: 0644]
examples/ucode/handler.uc [new file with mode: 0644]

diff --git a/examples/ucode/dump-env.uc b/examples/ucode/dump-env.uc
new file mode 100644 (file)
index 0000000..6bd7317
--- /dev/null
@@ -0,0 +1,22 @@
+Status: 200 OK
+Content-Type: text/html
+
+<h1>Headers</h1>
+
+{% for (let k, v in env.headers): %}
+<strong>{{ replace(k, /(^|-)(.)/g, (m0, d, c) => d + uc(c)) }}</strong>: {{ v }}<br>
+{% endfor %}
+
+<h1>Environment</h1>
+
+{% for (let k, v in env): if (type(v) == 'string'): %}
+<code>{{ k }}={{ v }}</code><br>
+{% endif; endfor %}
+
+{% if (env.CONTENT_LENGTH > 0): %}
+<h1>Body Contents</h1>
+
+{% for (let chunk = uhttpd.recv(64); chunk != null; chunk = uhttpd.recv(64)): %}
+<code>{{ replace(chunk, /[^[:graph:]]/g, '.') }}</code><br>
+{% endfor %}
+{% endif %}
diff --git a/examples/ucode/handler.uc b/examples/ucode/handler.uc
new file mode 100644 (file)
index 0000000..e71ac26
--- /dev/null
@@ -0,0 +1,7 @@
+{%
+
+'use strict';
+
+global.handle_request = function(env) {
+       include("dump-env.uc", { env });
+};