Skip to content

Instantly share code, notes, and snippets.

@astynax
Last active October 4, 2024 18:10
Show Gist options
  • Save astynax/3af4de3edaa8fb0404617ede6f43bdd4 to your computer and use it in GitHub Desktop.
Save astynax/3af4de3edaa8fb0404617ede6f43bdd4 to your computer and use it in GitHub Desktop.
Babashka, Scittle, Fullstack!

Для запуска нужна только Babashka!

Установите, передайте .clj-файл в качестве аргумента (bb main.clj).

Когда сервер запустится, откройте http://localhost:8000 в браузере. Приложение работает!

(ns main)
(require '[org.httpkit.server :as http])
(require '[hiccup.core :as h])
(defn handler [{:keys [request-method uri]}]
(case [request-method uri]
[:get "/"]
{:status 200
:headers {"Content-Type" "text/html"}
:body
(h/html
[:html {:lang "EN"}
[:head
[:script {:src "https://cdn.jsdelivr.net/npm/[email protected]/dist/scittle.js"}]
[:link {:rel "stylesheet"
:href "https://cdn.jsdelivr.net/npm/@picocss/[email protected]/css/pico.min.css"}]]
[:body
[:main.container
[:h1 "Hello from Clojure!"]
[:h2#clock]
[:progress#progress {:value 0 :max 59}]
]
[:script {:type "application/x-scittle"} "
(def tag (.querySelector js/document \"#clock\"))
(def prog (.querySelector js/document \"#progress\"))
(defn update []
(let [d (js/Date.)
t (.toLocaleTimeString d)
s (.getSeconds d)]
(set! (.-innerText tag) t)
(.setAttribute prog \"value\" (str s))))
(update)
(.setInterval js/window update 1000)
"]]])
}
{:status 404
:body "Page not found"}
))
(defonce app (atom nil))
(defn stop []
(when @app
(println "Stopping...")
(@app)))
(defn start []
(stop) ; just in case
(println "Starting on http://localhost:8000 ...")
(reset! app (http/run-server #'handler {:ip "localhost" :port 8000})))
#_(start)
#_(stop)
(when (= *file* (System/getProperty "babashka.file"))
(start)
@(promise))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment