Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created December 19, 2025 16:16
Show Gist options
  • Select an option

  • Save cablehead/f5cd12eb7ff2b6d10b4f3ea8603bd3ee to your computer and use it in GitHub Desktop.

Select an option

Save cablehead/f5cd12eb7ff2b6d10b4f3ea8603bd3ee to your computer and use it in GitHub Desktop.
use xs.nu *
use http-nu/router *
use http-nu/datastar *
use http-nu/html *
def escape []: string -> string {
$in | str replace -a '&' '&amp;' | str replace -a '<' '&lt;' | str replace -a '>' '&gt;'
}
def _quote [frame] {
_div {
id: quote
style: {
font-style: italic
padding: 5rem
}
} { .cas $frame.hash | escape }
}
const use_cases = [
"static file server (vs python -m http.server & npx serve)"
"expose a shell script"
"Dashboards"
"receive some stuff"
"careful though (tbd: sanitize html inputs)"
]
{|req|
dispatch $req [
(
route {method: GET path: "/_sse"} {|req ctx|
.cat -ft --pulse 41 | each {|frame|
match $frame {
{topic: "serve.nu"} => ("location.reload()" | to dstar-execute-script)
{topic: "quote"} => (_quote $frame | to dstar-patch-element)
{topic: "xs.pulse"} => (
_div {id: now} {
date now | format date "%Y-%m-%d %H:%M:%S%.3f" | $in + " -- 24FPS &#x1F680;"
} | to dstar-patch-element
)
}
} | compact | to sse
}
)
(
route {method: POST path: "/quote"} {|req ctx|
$in | .append quote
}
)
(
route {method: GET path: "/"} {|req ctx|
(
_html
(
_head
(_meta {charset: "UTF-8"})
(_meta {name: "viewport" content: "width=device-width, initial-scale=1.0"})
(_title "Demo")
(
_style r#'
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
font: 1.25rem/1.6 ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono',
'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro',
'Fira Mono', 'Droid Sans Mono', monospace;
}
body { padding: 1rem; }
ul, ol { margin-left: 0; padding-left: 1.25em; }
'#
)
(_script {type: "module" src: $DATASTAR_CDN_URL})
)
(
_body {data-init: "@get('/_sse')"}
(_h1 "Hello New Stadium!")
(_div {id: now} "...")
(_quote (.head quote))
(_h2 "a demo of http-nu :: use cases ->")
(_ul { $use_cases | each { _li $in } })
)
)
}
)
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment