Browse Source

examples: add ucode handler example

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Jo-Philipp Wich 2 years ago
parent
commit
16aa142c29
2 changed files with 29 additions and 0 deletions
  1. 22 0
      examples/ucode/dump-env.uc
  2. 7 0
      examples/ucode/handler.uc

+ 22 - 0
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 %}

+ 7 - 0
examples/ucode/handler.uc

@@ -0,0 +1,7 @@
+{%
+
+'use strict';
+
+global.handle_request = function(env) {
+	include("dump-env.uc", { env });
+};