Created
May 6, 2025 22:54
-
-
Save tommy-mor/c2217c9fb6a6eb553a61a1af547c13ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns app.core | |
(:require [weave.core :as weave] | |
[hiccup.page :refer [html5]])) | |
;; Shared app state (in a real app this might be in a db or session) | |
(defonce counter (atom 0)) | |
;; Securely sign the continuation (this will get serialized and verified on the server) | |
(defn counter-click-handler [] | |
(weave/sign | |
(fn [] | |
(swap! counter inc) | |
(weave/push-html! | |
[:h1#label.text-2xl.font-bold | |
(str "Counter: " @counter)])))) | |
;; Main view function | |
(defn view [] | |
[:div.p-6 | |
[:h1#label.text-2xl.font-bold | |
(str "Counter: " @counter)] | |
[:button.bg-blue-500.text-white.px-4.py-2.rounded | |
;; Embed signed continuation into Datastar-compatible click action | |
{:data-on-click (str "@post('/eval?blob=" (counter-click-handler) "')")} | |
"Increment"]]) | |
(def eval-route | |
(POST "/eval" (fn [req] | |
(let [{:strs [blob sig]} (:query-params req) | |
bytes (b64-decode blob)] | |
(if (not (verify-signature? bytes sig)) | |
{:status 403} | |
(eval (read-string (String. bytes "UTF-8")))))))) | |
(defn -main [] | |
(weave/run view {:port 8080})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment