// Example 26: Pure-Veko HTML5 Component DSL (`lib/html`), Safe Templates (`lib/template`) & Unified HTTP Client (`lib/net/client`)
// Demonstrates building reusable pure-Veko UI components (both homoiconic Hiccup blocks and
// typed builders), serving them via `web/page` & `web/view` with gzip compression and redirects,
// and consuming them with the 3-track pure-Veko `net/client` (`hc/get`, `hc/post-json`).
//
// Track A (ORO VM) : veko run --allow-net --allow-listen --allow-private-net examples/26_pure_vk_html_components_and_http_client.vk
// Track B (Wasm 3) : veko run --wasm --check-slabs --fuel=0 --allow-net --allow-listen --allow-private-net examples/26_pure_vk_html_components_and_http_client.vk
// Track C (Native) : veko compile --native examples/26_pure_vk_html_components_and_http_client.vk -o dist/26.exe ; ./dist/26.exe
use [
html
tpl = template
deflate = compress/deflate
web
hc = net/client
]
// Reusable pure-Veko UI components (auto-escaping XSS by default!)
badge-comp = fn [label str, kind str] block [
[.span { class = (str/join ['badge badge-' kind] '') } [label]]
]
service-card = fn [name str, status str, tags block] html/Node [
html/article { class = 'card' } [
(html/h2 {} [(html/text name)])
(badge-comp status (if status == 'healthy' [ 'ok' ] else [ 'warn' ]))
(html/ul { class = 'tags' } (map tags fn [t] [ [.li [t]] ]))
]
]
dashboard-page = fn [viewer str, services block] html/Node [
html/document 'Veko Control Plane' [
(html/style-el 'body{font-family:system-ui}.card{border:1px solid #ddd;padding:12px}')
] [
[.header { class = 'topbar' } [
[.h1 ['Veko Services']]
[.p ['Signed in as: ' viewer]]
]]
[.main { class = 'grid' } (map services fn [s] [
service-card s.name s.status s.tags
])]
]
]
// 1. Render component tree and verify automatic XSS escaping
xss_html = html/render (dashboard-page '<script>alert(1)</script>' [
{ name = 'gateway', status = 'healthy', tags = ['tls13-pqc' 'http2'] }
{ name = 'vault', status = 'healthy', tags = ['aes256-gcm' 'wal'] }
])
if (not (str/contains? xss_html '<script>alert(1)</script>')) or (not (str/contains? xss_html '<li>tls13-pqc</li>')) [
err 'example26: html component rendering failed'
]
println (str/join ['html_dsl=ok|bytes=' (mold (len xss_html))] '')
// 2. Safe HTML template rendering (`template/render-html`)
t_out = tpl/render-html '<div>{{ title }}: {{#each items}}[{{ . }}]{{/each}}</div>' {
title = 'Nodes & Peers'
items = ['eu-1' 'us-2']
}
if t_out != '<div>Nodes & Peers: [eu-1][us-2]</div>' [
err 'example26: template rendering failed'
]
println (str/join ['template=' t_out] '')
// 3. Live loopback `web/page` + `302` redirect + `gzip` response + `net/client` (`hc/get` & `hc/post-json`)
ln = net/listen-tcp .ip '127.0.0.1:0'
port = str/to-i64 (str/split (net/addr ln) ':')[1]
srv = spawn [
mut app = web/new-app
web/use-security-headers mut app
web/get mut app '/old' fn [req web/Request] web/Response [
_ = req
web/redirect 302 '/dashboard'
]
web/get mut app '/dashboard' fn [req web/Request] web/Response [
_ = req
doc = dashboard-page 'Ada' [
{ name = 'edge-proxy', status = 'healthy', tags = ['oro' 'pqc'] }
]
gz = deflate/gzip-compress (bytes/from-str (html/render doc))
mut r = web/response 200 'text/html; charset=utf-8' gz
mut hdrs = r.headers
map/set mut hdrs 'Content-Encoding' 'gzip'
put mut r .headers hdrs
r
]
web/post mut app '/api/echo' fn [req web/Request] web/Response [
body_m = web/bind-json req
web/json-resp 201 { echoed = body_m.msg, framework = 'veko/web' }
]
// Serve 3 sequential requests: GET /old -> GET /dashboard -> POST /api/echo
_ = web/serve-loop ln app 3
'served-3'
]
base = str/join ['http://127.0.0.1:' (mold port)] ''
r_dash = hc/get (str/join [base '/old'] '') (hc/RequestOpts { timeout = 5s })
dash_txt = hc/text-body r_dash
if r_dash.status != 200 or (not (str/contains? r_dash.final_url '/dashboard')) or (not (str/contains? dash_txt '<h2>edge-proxy</h2>')) [
err 'example26: net/client redirect + gzip html failed'
]
r_post = hc/post-json (str/join [base '/api/echo'] '') { msg = 'hello-pure-veko' } (hc/RequestOpts { timeout = 5s })
j_post = hc/json-body r_post
_ = task/await srv
_ = net/close ln
if r_post.status != 201 or j_post.echoed != 'hello-pure-veko' or j_post.framework != 'veko/web' [
err 'example26: net/client post-json failed'
]
println (str/join ['client_redirect_gzip=' (mold r_dash.status) '|final=/dashboard|post_json=' (mold r_post.status) ':' j_post.echoed] '')