Skip to content

Commit

Permalink
Add wrap-hiccup middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Nov 29, 2024
1 parent a3f44a4 commit 79e784b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[org.duct-framework/server.http.jetty "0.3.0"]
[org.duct-framework/router.reitit "0.2.0"]
[integrant "0.13.1"]
[hiccup "2.0.0-RC4"]
[org.slf4j/slf4j-nop "2.0.16"]
[org.webjars/normalize.css "5.0.0"]
[ring/ring-core "1.13.0"]
Expand Down
28 changes: 28 additions & 0 deletions src/duct/middleware/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.java.io :as io]
[duct.logger :as logger]
[integrant.core :as ig]
[hiccup2.core :as hic]
[ring.middleware.defaults :refer [wrap-defaults]]
[ring.middleware.stacktrace :refer [wrap-stacktrace]]
[ring.middleware.webjars :refer [wrap-webjars]]
Expand Down Expand Up @@ -67,6 +68,30 @@
(catch Throwable _
(respond (internal-error (error-handler request))))))))

(defn- hiccup-response [response hiccup-renderer]
(let [response (if (vector? response)
{:status 200, :headers {}, :body response}
response)]
(if (vector? (:body response))
(-> response
(update :body #(str "<!DOCTYPE html>\n" (hiccup-renderer %)))
(assoc-in [:headers "Content-Type"] "text/html;charset=UTF-8")
(update :status #(or % 200)))
response)))

(defn wrap-hiccup
"Middleware that renders vectors as HTML. The vector can either be the only
response from the handler, or on the body key of the response map. Requires
a function that converts Hiccup vectors into HTML."
([handler]
(wrap-hiccup handler #(hic/html {:mode :html} %)))
([handler hiccup-renderer]
(fn
([request]
(some-> (handler request) (hiccup-response hiccup-renderer)))
([request respond raise]
(handler request (comp respond hiccup-response) raise)))))

(defmethod ig/init-key ::log-requests
[_ {:keys [logger options]}]
#(wrap-log-requests % logger (dissoc options :logger)))
Expand All @@ -90,3 +115,6 @@

(defmethod ig/init-key ::stacktrace [_ options]
#(wrap-stacktrace % options))

(defmethod ig/init-key ::hiccup [_ {:keys [hiccup-renderer]}]
(if hiccup-renderer #(wrap-hiccup % hiccup-renderer) wrap-hiccup))
3 changes: 3 additions & 0 deletions src/duct/module/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
:duct.router/reitit
{:routes ~routes
~@(when api? [:muuntaja {}]) ~@[]
~@(when site?
[:data {:middleware [(ig/ref :duct.middleware.web/hiccup)]}]) ~@[]
:middleware
[~@(when site? [(ig/ref :duct.middleware.web/webjars)])
~(ig/ref :duct.middleware.web/defaults)
Expand Down Expand Up @@ -102,6 +104,7 @@

~@(when api? [:duct.middleware.web/format {}]) ~@[]
~@(when site? [:duct.middleware.web/webjars {}]) ~@[]
~@(when site? [:duct.middleware.web/hiccup {}]) ~@[]

~@(->> (route-data-seq routes)
(mapcat find-handlers-in-route-data)
Expand Down
18 changes: 18 additions & 0 deletions test/duct/middleware/web_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,21 @@
{:static {:files [(str tempdir)]}})
(is (.exists (.toFile tempdir)))
(Files/deleteIfExists tempdir)))

(deftest test-wrap-hiccup
(is (= {:status 200
:headers {"Content-Type" "text/html;charset=UTF-8"}
:body "<!DOCTYPE html>\n<title>foo</title>"}
((wrap-hiccup (constantly [:title "foo"])) {})))
(is (= {:status 200
:headers {"Content-Type" "text/html;charset=UTF-8"}
:body "<!DOCTYPE html>\n<title>foo</title>"}
((wrap-hiccup (constantly {:body [:title "foo"]})) {})))
(is (= {:status 201
:headers {"Content-Type" "text/html;charset=UTF-8"}
:body "<!DOCTYPE html>\n<title>foo</title>"}
((wrap-hiccup (constantly {:status 201, :body [:title "foo"]})) {})))
(let [response {:status 200
:headers {"Content-Type" "text/plain;charset=UTF-8"}
:body "Foobar"}]
(is (= response ((wrap-hiccup (constantly response)) {})))))
2 changes: 2 additions & 0 deletions test/duct/module/web_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
(deftest site-module-test
(is (= {:duct.router/reitit
{:routes []
:data {:middleware [(ig/ref :duct.middleware.web/hiccup)]}
:middleware
[(ig/ref :duct.middleware.web/webjars)
(ig/ref :duct.middleware.web/defaults)
Expand Down Expand Up @@ -136,6 +137,7 @@
:handler (ig/ref :duct/router)
:logger (ig/refset :duct/logger)}
:duct.middleware.web/webjars {}
:duct.middleware.web/hiccup {}
:duct.middleware.web/stacktrace {}
:duct.handler.static/bad-request
{:headers {"Content-Type" "text/html; charset=UTF-8"}
Expand Down

0 comments on commit 79e784b

Please sign in to comment.