       (timbre/debug "Handling request:" req)
       (handler req))))
 
+(defn wrap-authenticate-user
+  [handler]
+
+  (fn [req]
+    (handler (merge req
+                    (auth/authenticate-user (get-in req [:headers "authorization"]))))))
+
 (defn make-handler [db-conn]
   (-> (make-router db-conn)
-      (wrap-log-request)
+      wrap-log-request
       (wrap-restful-format :formats [:edn :transit-json])
+      wrap-authenticate-user
       (rmd/wrap-defaults rmd/api-defaults)))
 
 

            [ring.middleware.defaults :as rmd]
            [meetdown.data :as data]
            [org.httpkit.server :refer [run-server]]
            [taoensso.timbre :as timbre]))

(defn- handle-query
  [db-conn]
  (fn [{req-body :body-params}]
    (let [db (data/database db-conn)]
      (case (:type req-body)
        :get-events   {:body (data/get-events db)}
        :create-event {:body {:db/id (:db/id (data/create-entity db-conn (:txn-data req-body)))}}
        :get-event    {:body (->> (get-in req-body [:txn-data :db/id])
                                  (data/to-ent db))}
        :create-user  {:body {:db/id (:db/id (data/create-entity db-conn (:txn-data req-body)))}}
        {:status 415 :body (str "Unsupported type - " (:type req-body))}))))

(def home-page


(defn make-router [db-conn]
  (routes
    (resources "/")
    (GET "/" [] home-page)
    (POST "/q" []
      (handle-query db-conn))))

(def default-config
  {:params {:urlencoded true
