From 8406f31be2ba3397734c76eb03d0143779e43484 Mon Sep 17 00:00:00 2001 From: James Reeves Date: Tue, 7 Jan 2025 22:59:05 +0000 Subject: [PATCH] Replace :default-handler with :handlers option --- README.md | 18 +++++++++++++++--- src/duct/handler/reitit.clj | 6 ++++++ src/duct/router/reitit.clj | 4 ++-- test/duct/router/reitit_test.clj | 14 ++++++++------ 4 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/duct/handler/reitit.clj diff --git a/README.md b/README.md index 19877a0..8f23844 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ There are four top level options available: - `:routes` - the Reitit routing data - `:middleware` - a vector of middleware to apply to the Ring handler - `:data` - data to add to every Reitit route -- `:default-handler` - a default handlers for error conditions +- `:handlers` - a vector of handlers to fall back The `:data` key takes a map and acts as it does in Reitit, except for the following keys: @@ -46,13 +46,25 @@ the following keys: These keys will automatically add relevant middleware. -The `:default-handler` key holds a map that takes the same keys as the -Reitit `create-default-handler` function: +The `:duct.handler.reitit/default` key will initiate into a handler +using the Reitit `create-default-handler` function. It takes the +following options: - `:not-found` - a handler for 404 HTTP errors - `:method-not-allowed` - a handler for 405 HTTP errors - `:not-acceptable` - a handler for 406 HTTP errors +This can be referenced in the `:handlers` vector to provide a default +handler: + +```edn +{:duct.router/reitit + {:routes ["/" {:get #ig/ref :example.handler/root}] + :handlers [#ig/ref :duct.handler.reitit/default]} + :duct.handler.reitit/default {} + :example.handler/root {}} +``` + ## License Copyright © 2024 James Reeves diff --git a/src/duct/handler/reitit.clj b/src/duct/handler/reitit.clj new file mode 100644 index 0000000..df6c10c --- /dev/null +++ b/src/duct/handler/reitit.clj @@ -0,0 +1,6 @@ +(ns duct.handler.reitit + (:require [integrant.core :as ig] + [reitit.ring :as ring])) + +(defmethod ig/init-key ::default [_ opts] + (ring/create-default-handler opts)) diff --git a/src/duct/router/reitit.clj b/src/duct/router/reitit.clj index 92ab72f..66b30d4 100644 --- a/src/duct/router/reitit.clj +++ b/src/duct/router/reitit.clj @@ -52,6 +52,6 @@ (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 (:handlers opts))] + (ring/ring-handler router (apply ring/routes handlers) opts) (ring/ring-handler router opts)))) diff --git a/test/duct/router/reitit_test.clj b/test/duct/router/reitit_test.clj index f5b77fc..f69aaf9 100644 --- a/test/duct/router/reitit_test.clj +++ b/test/duct/router/reitit_test.clj @@ -1,7 +1,8 @@ (ns duct.router.reitit-test (:require [clojure.test :refer [deftest is]] [integrant.core :as ig] - [duct.router.reitit :as reitit])) + [duct.router.reitit] + [duct.handler.reitit])) (deftest hierarchy-test (ig/load-hierarchy) @@ -50,11 +51,12 @@ {:duct.router/reitit {:routes {"/" {:get {:handler handler}} "/406" {:handler (constantly nil)}} - :default-handler - {:not-found (constantly {:status 404, :body "404"}) - :method-not-allowed (constantly {:status 405, :body "405"}) - :not-acceptable (constantly {:status 406, :body "406"})}}} - router (:duct.router/reitit (ig/init config))] + :handlers [(ig/ref :duct.handler.reitit/default)]} + :duct.handler.reitit/default + {:not-found (constantly {:status 404, :body "404"}) + :method-not-allowed (constantly {:status 405, :body "405"}) + :not-acceptable (constantly {:status 406, :body "406"})}} + router (:duct.router/reitit (ig/init config))] (is (= {:status 200, :body "Hello World"} (router {:request-method :get, :uri "/"}))) (is (= {:status 404, :body "404"}