Add archive helpers and harden task runtime
This commit is contained in:
@@ -37,6 +37,8 @@ RENDER(Request& context)
|
||||
<div class="grid-heading">Data Types & Parsing</div>
|
||||
<? render_card("dtree.uce", "DTree", "Dynamic hierarchical data tree"); ?>
|
||||
<? render_card("json.uce", "JSON", "Parse and encode JSON data"); ?>
|
||||
<? render_card("xml.uce", "XML", "Structural XML encode/decode with DTree"); ?>
|
||||
<? render_card("yaml.uce", "YAML", "Concise config files with DTree"); ?>
|
||||
<? render_card("preprocessor-comments.uce", "Preprocessor Comments", "Regression coverage for comment parsing in templates"); ?>
|
||||
<? render_card("regex.uce", "Regular Expressions", "PCRE2 matching, captures, replacement, and splitting"); ?>
|
||||
<? render_card("string.uce", "String", "String operations"); ?>
|
||||
@@ -54,6 +56,7 @@ RENDER(Request& context)
|
||||
|
||||
<div class="grid-heading">Storage & I/O</div>
|
||||
<? render_card("fileio.uce", "File I/O", "Read and write files"); ?>
|
||||
<? if(allow_server_demos) { render_card("zip.uce", "ZIP", "Create, list, read, and extract ZIP archives"); } ?>
|
||||
<? if(allow_server_demos) { render_card("file_append.uce", "File Append", "Append data to files"); } ?>
|
||||
<? if(allow_server_demos) { render_card("shell.uce", "Shell", "Execute shell commands"); } ?>
|
||||
<? if(allow_server_demos) { render_card("memcached.uce", "Memcached", "Memcached key-value store"); } ?>
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ RENDER(Request& context)
|
||||
|
||||
<pre style="white-space: pre-wrap"><?
|
||||
|
||||
print("New Task ID: ", task("example-task", []() {
|
||||
print("New Task ID: ", task(task_name, []() {
|
||||
|
||||
sleep(10);
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ RENDER(Request& context)
|
||||
|
||||
<pre style="white-space: pre-wrap"><?
|
||||
|
||||
print("New Task ID: ", task_repeat("example-task", 5, []() {
|
||||
print("New Task ID: ", task_repeat(task_name, 5, []() {
|
||||
|
||||
sleep(1);
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "demo_guard.h"
|
||||
|
||||
RENDER(Request& context)
|
||||
{
|
||||
DTree book;
|
||||
book["name"] = "book";
|
||||
book["attrs"]["id"] = "b1";
|
||||
book["attrs"]["type"] = "reference";
|
||||
|
||||
DTree title;
|
||||
title["name"] = "title";
|
||||
title["text"] = "UCE & XML";
|
||||
book["children"].push(title);
|
||||
|
||||
DTree chapter;
|
||||
chapter["name"] = "chapter";
|
||||
chapter["attrs"]["number"] = "1";
|
||||
chapter["text"] = "Structural conversion without schema validation.";
|
||||
book["children"].push(chapter);
|
||||
|
||||
String encoded = xml_encode(book);
|
||||
DTree decoded = xml_decode(encoded);
|
||||
|
||||
DTree simple;
|
||||
simple["title"] = "Hello";
|
||||
simple["count"] = "3";
|
||||
|
||||
String incoming = "<note priority=\"high\"><to>UCE</to><body><![CDATA[5 < 6]]></body><symbol>AB</symbol></note>";
|
||||
DTree incoming_tree = xml_decode(incoming);
|
||||
|
||||
<>
|
||||
<link rel="stylesheet" href='style.css'></link>
|
||||
<h1>
|
||||
<a href="index.uce">UCE Test</a>:
|
||||
XML
|
||||
</h1>
|
||||
|
||||
<p><code>xml_encode()</code> and <code>xml_decode()</code> convert between XML strings and element-shaped <code>DTree</code> values without schema validation.</p>
|
||||
|
||||
<h2>Encoded Element Tree</h2>
|
||||
<pre><?= encoded ?></pre>
|
||||
|
||||
<h2>Decoded DTree</h2>
|
||||
<pre><?= json_encode(decoded) ?></pre>
|
||||
|
||||
<h2>Simple Map Encoding</h2>
|
||||
<pre><?= xml_encode(simple, "payload") ?></pre>
|
||||
|
||||
<h2>Decode Existing XML</h2>
|
||||
<p>Input XML:</p>
|
||||
<pre><?= incoming ?></pre>
|
||||
<p>Decoded DTree:</p>
|
||||
<pre><?= json_encode(incoming_tree) ?></pre>
|
||||
|
||||
<p>
|
||||
<a href="../doc/index.uce?p=xml_encode">xml_encode() docs</a>
|
||||
|
|
||||
<a href="../doc/index.uce?p=xml_decode">xml_decode() docs</a>
|
||||
</p>
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "demo_guard.h"
|
||||
|
||||
RENDER(Request& context)
|
||||
{
|
||||
String source = "# UCE app config\n"
|
||||
"app:\n"
|
||||
" name: UCE Starter\n"
|
||||
" debug: true\n"
|
||||
" port: 8080\n"
|
||||
" paths:\n"
|
||||
" - site\n"
|
||||
" - cache\n"
|
||||
"message: |\n"
|
||||
" Keep config files readable.\n"
|
||||
" Load them as DTree values.\n";
|
||||
|
||||
DTree cfg = yaml_decode(source);
|
||||
String encoded = yaml_encode(cfg);
|
||||
DTree roundtrip = yaml_decode(encoded);
|
||||
|
||||
DTree generated;
|
||||
generated["database"]["host"] = "localhost";
|
||||
generated["database"]["port"] = (f64)3306;
|
||||
generated["features"]["components"].set_bool(true);
|
||||
generated["features"]["markdown"].set_bool(true);
|
||||
|
||||
DTree theme;
|
||||
theme = "clean";
|
||||
generated["themes"].push(theme);
|
||||
theme = "compact";
|
||||
generated["themes"].push(theme);
|
||||
|
||||
<>
|
||||
<link rel="stylesheet" href='style.css'></link>
|
||||
<h1>
|
||||
<a href="index.uce">UCE Test</a>:
|
||||
YAML
|
||||
</h1>
|
||||
|
||||
<p><code>yaml_encode()</code> and <code>yaml_decode()</code> convert concise config-style YAML to and from <code>DTree</code> values.</p>
|
||||
|
||||
<h2>Config Source</h2>
|
||||
<pre><?= source ?></pre>
|
||||
|
||||
<h2>Decoded DTree</h2>
|
||||
<pre><?= json_encode(cfg) ?></pre>
|
||||
|
||||
<h2>Encoded Again</h2>
|
||||
<pre><?= encoded ?></pre>
|
||||
|
||||
<h2>Generated Config</h2>
|
||||
<pre><?= yaml_encode(generated) ?></pre>
|
||||
|
||||
<h2>Round Trip Reads</h2>
|
||||
<pre><?
|
||||
print("app.name = ", roundtrip["app"]["name"].to_string(), "\n");
|
||||
print("app.debug = ", roundtrip["app"]["debug"].to_bool() ? "true" : "false", "\n");
|
||||
print("app.port = ", std::to_string(roundtrip["app"]["port"].to_s64()), "\n");
|
||||
print("app.paths[1] = ", roundtrip["app"]["paths"]["1"].to_string(), "\n");
|
||||
?></pre>
|
||||
|
||||
<p>
|
||||
<a href="../doc/index.uce?p=yaml_encode">yaml_encode() docs</a>
|
||||
|
|
||||
<a href="../doc/index.uce?p=yaml_decode">yaml_decode() docs</a>
|
||||
</p>
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "demo_guard.h"
|
||||
|
||||
RENDER(Request& context)
|
||||
{
|
||||
if(!test_demo_request_allowed(context))
|
||||
{
|
||||
test_demo_render_restricted_html(context, "ZIP Demo", "create and extract temporary server-side archive files");
|
||||
return;
|
||||
}
|
||||
|
||||
String base = "/tmp/uce-demo-zip";
|
||||
String archive = path_join(base, "demo.zip");
|
||||
String extract_dir = path_join(base, "extract");
|
||||
mkdir(base);
|
||||
mkdir(extract_dir);
|
||||
|
||||
DTree entries;
|
||||
entries["hello.txt"] = "Hello from a generated ZIP archive.\n";
|
||||
entries["notes/readme.txt"] = "zip_create(), zip_list(), zip_read(), and zip_extract() are available to UCE pages.\n";
|
||||
zip_create(archive, entries);
|
||||
|
||||
DTree listing = zip_list(archive);
|
||||
String hello = zip_read(archive, "hello.txt");
|
||||
zip_extract(archive, extract_dir);
|
||||
String extracted = file_get_contents(path_join(extract_dir, "notes/readme.txt"));
|
||||
String gz_source = "This string was compressed with gz_compress() and restored with gz_uncompress().";
|
||||
String gz_body = gz_compress(gz_source);
|
||||
String gz_roundtrip = gz_uncompress(gz_body);
|
||||
|
||||
?><html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
|
||||
<link rel="stylesheet" href="style.css?v=<?= time() ?>"></link>
|
||||
</head>
|
||||
<body>
|
||||
<h1><a href="index.uce">UCE Test Suite</a> / ZIP</h1>
|
||||
<p>This demo creates a temporary archive at <code><?= archive ?></code>, lists it, reads one member, and extracts it under <code><?= extract_dir ?></code>.</p>
|
||||
<h2>zip_list()</h2>
|
||||
<pre><?= json_encode(listing) ?></pre>
|
||||
<h2>zip_read()</h2>
|
||||
<pre><?= hello ?></pre>
|
||||
<h2>Extracted File</h2>
|
||||
<pre><?= extracted ?></pre>
|
||||
<h2>gzip Helpers</h2>
|
||||
<p>Source bytes: <?= std::to_string((u64)gz_source.size()) ?>; compressed bytes: <?= std::to_string((u64)gz_body.size()) ?></p>
|
||||
<pre><?= gz_roundtrip ?></pre>
|
||||
</body>
|
||||
</html><?
|
||||
}
|
||||
Reference in New Issue
Block a user