Skip to content

Commit

Permalink
Replace :default-handler with :handlers option
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Jan 7, 2025
1 parent fdbecad commit 8406f31
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/duct/handler/reitit.clj
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 2 additions & 2 deletions src/duct/router/reitit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
14 changes: 8 additions & 6 deletions test/duct/router/reitit_test.clj
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit 8406f31

Please sign in to comment.