Regaining my compojure#
Thu, 19 Feb 2015 11:16:22 +0000
Picking up $secret_project which I put down in November to do Sledge and Yablog, I find that the routing of http requests is a horrendous mess based on substring matching and ad hoc argument parsing, and would really benefit from some Compojure
(Now, that is, that I've actually got my head around how Compojure works)
Because I'm using Hiccup to compose pages out of parts, I thought it would be neat if I could return hiccup page bodies directly as responses so that they get uniformly topped and tailed and turned into response maps. Turns out to be fairly straightforward:
- define a type for the hiccup response
- extend the
compojure.response/Renderable
protocol to deal with the new type
(deftype Hiccupage [body]) (defn page-surround [page] (let [head [:head [:link {:rel "stylesheet" :type "text/css" :href "/static/default.css"}] [:script {:src "/static/stuff.js"}] [:title "Hey you"]]] (into head (.body page)))) (extend-protocol compojure.response/Renderable Hiccupage (render [page req] {:status 200 :headers {"Content-Type" "text/html; charset=utf-8"} :body (hiccup.page/html5 (page-surround page))}))
Now we can return Hiccupage (hiccup page? geddit? I'm still looking for a better name, yes) objects directly from our routes
(defroutes app (GET "/hello/:name" [name] (Hiccupage. [[:h1 "hello"] [:p "hello " name] [:p "This is text. I like text"]])) ... )