Skip to content

Commit

Permalink
Add :file-handers and :resource-handlers options
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Dec 11, 2024
1 parent fdbecad commit fdb99b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/duct/router/reitit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@
(-> (update :coercion (comp var-get requiring-resolve coercion-engines))
(update :middleware #(into coercion-middleware %)))))

(defn- path-map->options [path-map]
(for [[path opts] (sort-by key path-map)]
(assoc opts :path path)))

(defn- create-handlers
[{:keys [default-handler file-handlers resource-handlers]}]
(concat
(when file-handlers
(->> (path-map->options file-handlers)
(map ring/create-file-handler)))
(when resource-handlers
(->> (path-map->options resource-handlers)
(map ring/create-resource-handler)))
(when default-handler
(list (ring/create-default-handler default-handler)))))

(defmethod ig/init-key :duct.router/reitit [_ options]
(let [opts (-> options
(update-data convert-coercion)
(update-data convert-muuntaja))
router (ring/router (:routes opts) opts)]
(if-some [handler (:default-handler opts)]
(ring/ring-handler router (ring/create-default-handler handler) opts)
(if-some [handlers (seq (create-handlers opts))]
(ring/ring-handler router (apply ring/routes handlers) opts)
(ring/ring-handler router opts))))
1 change: 1 addition & 0 deletions test/duct/router/files/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
1 change: 1 addition & 0 deletions test/duct/router/files/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
35 changes: 35 additions & 0 deletions test/duct/router/reitit_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns duct.router.reitit-test
(:require [clojure.test :refer [deftest is]]
[clojure.java.io :as io]
[integrant.core :as ig]
[duct.router.reitit :as reitit]))

Expand Down Expand Up @@ -63,3 +64,37 @@
(router {:request-method :post, :uri "/"})))
(is (= {:status 406, :body "406"}
(router {:request-method :post, :uri "/406"})))))

(deftest file-handler-test
(let [handler (constantly {:status 200, :body "Hello World"})
config
{:duct.router/reitit
{:routes {"/" {:get {:handler handler}}}
:file-handlers {"/" {:root "test/duct/router/files"}}}}
router (:duct.router/reitit (ig/init config))]
(is (= {:status 200, :body "Hello World"}
(router {:request-method :get, :uri "/"})))
(is (= {:status 200, :body (io/file "test/duct/router/files/foo.txt")}
(-> (router {:request-method :get, :uri "/foo.txt"})
(dissoc :headers))))
(is (= {:status 200, :body (io/file "test/duct/router/files/bar.txt")}
(-> (router {:request-method :get, :uri "/bar.txt"})
(dissoc :headers))))))

(deftest resource-handler-test
(let [handler (constantly {:status 200, :body "Hello World"})
config
{:duct.router/reitit
{:routes {"/" {:get {:handler handler}}}
:resource-handlers {"/" {:root "duct/router/files"}}}}
router (:duct.router/reitit (ig/init config))]
(is (= {:status 200, :body "Hello World"}
(router {:request-method :get, :uri "/"})))
(is (= {:status 200, :body "foo\n"}
(-> (router {:request-method :get, :uri "/foo.txt"})
(dissoc :headers)
(update :body slurp))))
(is (= {:status 200, :body "bar\n"}
(-> (router {:request-method :get, :uri "/bar.txt"})
(dissoc :headers)
(update :body slurp))))))

0 comments on commit fdb99b1

Please sign in to comment.