Skip to content

Instantly share code, notes, and snippets.

@mowat27
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save mowat27/11207416 to your computer and use it in GitHub Desktop.

Select an option

Save mowat27/11207416 to your computer and use it in GitHub Desktop.
Modular Notes

component/Lifecycle

  • System construction and desctruction only
  • Once your compnents have been created, they are on their own
    • i.e. it does not automatically add anything to a request/opartaion

Add something to every request

  • Create a RingHandlerProvider designed to run just after the web server
  • Intercept the handler
  • Add wahtever you like to the request map and call the handler
(defrecord RingBinder []
  component/Lifecycle
  (start [this] this)
  (stop [this] this)

  RingHandlerProvider
  (handler [this] 
    (let [ring-handler-provider (find-ring-handler-provider this)
          ring-handler          (handler ring-handler-provider)
          ring-bindings         (get-ring-bindings this)]
      (fn [req] 
        (ring-handler (assoc req :params ring-bindings))))))
  • Update the dependency map
(defn new-system []
 (let [system-map (component/system-map 
                   :webserver          (new-webserver :port 3000)
                   :ring-binder        (new-ring-binder)
                   :router             (new-bidi-ring-handler-provider)
                   :application-layout (new-layout :template "views/layouts/application.html.mustache")
                   :web                (new-web {:templates 
                                                      {:index (slurp "resources/views/index.mustache")}}))]
   (component/system-using system-map 
                           (-> {:webserver {:router :router}
                                :router    {:web :web}
                                :ring-binder {:web :web}} 
                               (interpose-component :webserver :application-layout)
                               (interpose-component :webserver :ring-binder)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment