https://github.com/druids/duct.module.bidi
Duct module and router for the Bidi routing library
https://github.com/druids/duct.module.bidi
Last synced: 10 months ago
JSON representation
Duct module and router for the Bidi routing library
- Host: GitHub
- URL: https://github.com/druids/duct.module.bidi
- Owner: druids
- License: mit
- Created: 2018-05-03T12:30:30.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-08T06:47:20.000Z (about 8 years ago)
- Last Synced: 2025-03-24T22:41:31.396Z (over 1 year ago)
- Language: Clojure
- Size: 26.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Duct module.bidi
================
A [Duct](https://github.com/duct-framework/duct) module that sets [Bidi]() as the router for your application.
[](https://circleci.com/gh/druids/duct.module.bidi)
[](https://jarkeeper.com/druids/duct.module.bidi)
[](https://opensource.org/licenses/MIT)
Leiningen/Boot
--------------
To install, add the following to you project `:dependencies`:
```clojure
[duct.module.bidi "0.5.0"]
```
Usage
-----
To add this module to your configuration, add the `:duct.module/bidi` key. For example:
```clojure
{:duct.core/project-ns foo
:duct.module/bidi [{"/index" :index}]
:foo.handler/index {}}
```
And `foo/handler/index.clj` may looks like:
```clojure
(ns foo.handler.index
(:require
[integrant.core :as ig]))
(defmethod ig/init-key :foo.handler/index
[_ options]
(fn [request] ;; :bidi-route will be :index
{:status 200, :body "Index page", :headers {"Content-Type" "text/html"}})))
```
A route name is in a request under `:bidi-route` key.
Routes can grouped into one handler:
```clojure
{:duct.core/project-ns foo
:duct.module/bidi [{"/" :index
"/api/users" {"" :api/users-list
["/" :id] :api/users-detail}}]
:foo.handler/index {}
:foo.handler/api {}}
```
And `foo/handler/api.clj` may looks like:
```clojure
(ns foo.handler.api
(:require
[integrant.core :as ig]))
(defmethod ig/init-key :foo.handler/api
[_ options]
(fn [request]
(case (:bidi-route request)
:api/users-list {:status 200, :body []})
:api/users-detail {:status 200, :body {:id 1, ...
```
Routes can be composed from multiple components:
```clojure
{:duct.core/project-ns foo
:foo/private-routes {"/private" {"/foo" :foo, "/bar" :bar}}
:foo/public-routes {"/public" {"/biz" :biz, "/baz" :baz}}
:duct.module/bidi [#ig/ref :foo/private-routes
#ig/ref :foo/public-routes]}
```
This is useful e.g. for development only routes.
When a server started you can find a route composition under `:duct.router/routes` key.
E.g.
```clojure
(:duct.router/routes config)
[""
{"/private" {"/foo" :foo, "/bar" :bar}}
"/public" {"/biz" :biz, "/baz" :baz}}]
```
Thus you can reverse routes (e.g. when sending an e-mail message) by calling
```clojure
(require '[bidi.bidi :as bidi])
(bidi/route-for (:duct.router/routes config) :bar)
;; "/private/bar"
```
Testing
-------
For testing there is a function `route` that associates a matched route into a request, if a uri matches to any given
routes. Example:
```clojure
(require '[duct.routes.bidi-testing :as bidi])
(require '[integrant.core :as ig])
(require '[ring.mock.request :as mock])
(let [routes ["" {"/hello" {:get :api/hello}}]
handler (ig/init-key :myapp.handler/api {})
response (handler (-> :get (mock/request "/hello") (bidi/route routes)))]]
^--- it sets `:api/hello` under `:bidi-route` key
...)
```