   [req]
   (str (java.util.UUID/randomUUID)))
 
-(defn start [{:keys [msg-handler path sente-config announce-fn]}]
-  {:pre [msg-handler]}
+(defn sente-routes
+  "Return the Ring routes required for Sente."
+  [path chan-sock-atom]
+  (let [ajax-get-or-ws-handshake-fn (delay
+                                     (:ajax-get-or-ws-handshake-fn
+                                      @chan-sock-atom))
+        ajax-post-fn (delay (:ajax-post-fn @chan-sock-atom))]
+    (routes
+     (GET  path request (@ajax-get-or-ws-handshake-fn request))
+     (POST path request (@ajax-post-fn request)))))
+
+(defn start [{:keys [msg-handler path sente-config announce-fn]} chan-sock-atom]
+  {:pre [msg-handler chan-sock-atom]}
   (let [chan-sock (sente/make-channel-socket! path sente-config)
         router (sente/start-chsk-router-loop!
                 msg-handler (:ch-recv chan-sock))]
+    (reset! chan-sock-atom chan-sock)
     (when announce-fn
       (announce-fn chan-sock))
     {:router router



(defrecord Sente
    [config channel-socket router]
  ILifecycle
  (start [component]
    (if channel-socket
      component
      (merge component (start config))))
  (stop [component]
    (if channel-socket
      (do
        (stop channel-socket router)
        (assoc component :channel-socket nil :router nil))
      component)))

(defn sente
  "Return a sente component, routing messages to `msg-handler`.
  `announce-fn` will be called on start with the channel socket as an
  argument."
  [{:keys [path msg-handler announce-fn sente-config]}]
  (map->Sente
   {:config {:path (or path "/chsk")
             :msg-handler msg-handler
             :announce-fn announce-fn
             :sente-config (merge {:type :auto
                                   :user-id-fn random-id}
                                  sente-config)}}))

(ns com.palletops.bakery.sente
  "A sente component.
  Requires [com.taoensso/sente \"0.15.1\"] and [compojure \"1.1.8\"]."
  (:require
   [clojure.core.async :as async :refer [chan close! go-loop]]
   [com.palletops.leaven.protocols :refer [ILifecycle]]
   [compojure.core :refer [GET POST routes]]
   [compojure.route :refer [resources]]
   [taoensso.sente :as sente]
   [taoensso.timbre :as timbre]))

