 ; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ; server connection, sending / receiving
 
-(declare register-channel find-channel channels app)
+(declare register-channel find-channel channels app
+         register-service-handler close-connection-service-handler
+         send-message-impl)
 
 (defrecord MessengerImpl [host port stop-fn receive-channel]
   m/IReceiver

                 (close! receive-channel)
                 (stop-fn :timeout 100))
  m/ISender
  (send-message [this con msg]
                (if-let [chan (or (-> msg :target find-channel)
                                  (:channel m/*current-connection*)
                                  con)]
                  (try
                    (http/send! chan (json/write-str msg))
                    (catch Exception e (println e)))
                  (throw (Exception. (str "Cannot find channel for target " (:target msg)))))))

(defn create-messenger
  [& {:keys [port host], :or {port 8081 host "0.0.0.0"} :as opts}]
  (let [stop (http/run-server #'app {:port port :host host})
        messenger (m/create-messenger "server-messenger"
                                      (->MessengerImpl host port stop (chan)))]
    
    (m/add-service messenger "register"
                   (fn [server {:keys [sender data] :as msg}]
                     (if m/*current-connection*
                       (do
                         (register-channel
                          server (:channel m/*current-connection*)
                          sender data)
                         (m/answer server msg :OK false))
                       (m/answer server msg
                                 {:error (str "Cannot register connection for" sender)}
                                 false))))
    
    (m/add-service messenger "close-connection"
                   (fn [server {{id :id} :data :as msg}]
                     (if-let [con (if id
                                    (find-channel id)
                                    (:channel m/*current-connection*))]
                       (do
                         (go
                          (<! (timeout 200))
                          (-> con :channel (.serverClose 0)))
                         (m/answer server msg :OK false))
                       (m/answer server msg
                                 {:error (str "cannot find channel for" id)}
                                 false))))
    messenger))

; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; routing


(defonce log (agent []))

(def debug false)

(defn log-req
  [app]
