Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Small ring router using core.mach in babashka
(require '[clojure.core.match :refer [match]]
'[clojure.string :as str]
'[hiccup2.core :refer [html]]
'[org.httpkit.server :as server])
(defn router [req]
(let [paths (vec (rest (str/split (:uri req) #"/")))]
(match [(:request-method req) paths]
[:get ["users" id]] {:body (str (html [:div id]))}
:else {:body (str (html [:html "Welcome!"]))})))
(server/run-server router {:port 8090})
@(promise)
@frankitox

This comment has been minimized.

Copy link

@frankitox frankitox commented Mar 15, 2021

There's a little bug if (:uri req) is "/". You can rewrite it as (vec (rest (str/split "/" #"/"))).

@borkdude

This comment has been minimized.

Copy link
Owner Author

@borkdude borkdude commented Mar 15, 2021

Fixed, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment