 (defn start-jetty [handler port]
   (jetty/run-jetty handler {:port (Integer. port) :join? false}))
 
-(defrecord Server [db port jetty]
+(defrecord Server [session-store db port jetty]
   component/Lifecycle
   (start [component]
     (println "Start server at port:" port)
-    (assoc component :jetty (start-jetty (app-handler db) port)))
+    (assoc component :jetty (start-jetty (app-handler session-store db) port)))
   (stop [component]
     (println "Stop server")
     (when jetty

            [ring.middleware.params :as params]
            [ring.middleware.edn :refer [wrap-edn-params]]
            [ring.middleware.session :as session]
            [ring.util.response :as response]
            [ring.adapter.jetty :as jetty]
            [compojure.core :refer :all]

      (handler req)
      (catch Exception e
        {:status 500
         :body (web/error-page (:session req) (pr-str e))}))))

(extend-type MemoryStore
  component/Lifecycle
  (start [c] c)
  (stop [c]
    (reset! (:session-map c) {})
    c))

(defn new-session-store []
  (memory-store/memory-store (atom {})))

;; ======================================================================
;; Server

(defn app-handler [session-store db]
  (-> (main-routes db)
      (session/wrap-session {:store session-store})
      wrap-edn-params
      params/wrap-params
      wrap-exceptions))
