/ck.routing

Routing module for Conskit

Primary LanguageClojure

ck.routing Build Status Dependencies Status Clojars Project

Routing module for conskit

Installation

Add the dependency in the clojars badge above in your project.clj.

bidi support

Add the classifier "bidi" and the bidi library.

Usage

Add the following to your bootstrap.cfg:

ck.routing/router

When the router starts up it looks for all actions that specify a :route annotation and creates a vector of maps with the id and route value i.e.

(ns foo-app)
...
(action
  ^{:route "/"}
  action1
  [req]
  ;; logic
  )
(action
  ^{:route "/page"}
  action2
  [req]
  ;; logic
  )
  
;; Produces
[{:id :foo-app/action1 :route "/"} {:id :foo-app/action2 :route "/page"}]

To access this vector of routes simply call the get-routes method by adding the dependency in your serivice.

You can also create a ring handler using the make-ring-hanlder method

(defservice
  my-service
  [[:CKRouter get-routes make-ring-hanlder]]
  (init [this context]
    ...
    (some-ring-based-web-server 
      (make-ring-handler :bidi))
  ...)

make-ring-handler is called with a provider (:bidi).

Alternatives

If Bidi is not your cup of tea you can always implement your own provider by providing a method that extends the ck.routing/make-ring-handler* multimethod

(defmethod make-ring-handler* :my-special-provider
  [{:keys [routes get-action]}]
  ;; logic
  )
  
;; Within service
(make-ring-hanlder :my-special-provide)

where routes is the same vector created when the router is started and get-action is the method provided by the :ActionRegistry that can retrieve an action instance from its id. The action instance can then be called via conskit.protocols/invoke (see bidi/ck/routing/bidi.clj for an example).

License

Copyright © 2016 Jason Murphy

Distributed under the Apache License, Version 2.0.