summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-11-23 19:25:18 +0100
committerJo-Philipp Wich <jo@mein.io>2021-11-23 19:26:43 +0100
commit16aa142c29d027f1f6e33658dd6fddeaa4e04231 (patch)
tree227e90fe1ad13b0de1ae0f5bcbc1bdceca1c2229
parent3ceccd02d86bf4d6609f46d8b30963cc52034cc2 (diff)
examples: add ucode handler example
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--examples/ucode/dump-env.uc22
-rw-r--r--examples/ucode/handler.uc7
2 files changed, 29 insertions, 0 deletions
diff --git a/examples/ucode/dump-env.uc b/examples/ucode/dump-env.uc
new file mode 100644
index 0000000..6bd7317
--- /dev/null
+++ b/examples/ucode/dump-env.uc
@@ -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
index 0000000..e71ac26
--- /dev/null
+++ b/examples/ucode/handler.uc
@@ -0,0 +1,7 @@
+{%
+
+'use strict';
+
+global.handle_request = function(env) {
+ include("dump-env.uc", { env });
+};