          :where [?e :db/ident ?name]]
        (d/db (:connection database))))
 
+(defn sum [req]
+  {:status 200
+   :body (pr-str (find-countries (::database req)))
+   :headers {"Content-Type" "application/edn"}})
 
-(defrecord WebServer [port]
+(defn wrap-app-component [f database]
+  "Middleware that adds component awareness"
+  (fn [req]
+    (f (assoc req ::database database))))
+
+(defn make-handler [database]
+  (-> (routes
+        (resources "/")
+        (resources "/react" {:root "react"})
+        (GET "/sum" [req] sum)
+        (GET "/*" [req] (page)))
+      (wrap-app-component database)
+      api))
+
+(defrecord WebServer [github-crawler database port]
   component/Lifecycle
   (start [this]
     (do
       (if is-dev? (start-figwheel))
       (let [port (Integer. (or port (env :port) 10555))]
         (print "Starting web server on port" port ".\n")
-        (assoc this :http-server (run-jetty http-handler {:port port
-                                                          :join? false}))))
-    )
+        (assoc this :jetty (run-jetty (make-handler database) {:port port :join? false})))))
   (stop [this]
-    (.stop (:http-server this)))
-  )
+    (.stop (:jetty this))))
 
 (defn new-database [uri]
   (Database. uri nil))

(ns communitics.server
  (:require [clojure.java.io :as io]
            [communitics.dev :refer [is-dev? inject-devmode-html browser-repl start-figwheel]]
            [compojure.core :refer [GET defroutes]]
            [compojure.route :refer [resources]]
            [compojure.handler :refer [api]]
            [net.cgrand.enlive-html :refer [deftemplate]]

             [:database]))))

(def system (prod-system {:github-address "http://github.com/api/v3"
                          :datomic-url "datomic:dev://localhost:4334/mbrainz-1968-1973"}))

#_(defn run [& [port]]
  (defonce ^:private server
           (do
             (if is-dev? (start-figwheel))
             (let [port (Integer. (or port (env :port) 10555))]
               (print "Starting web server on port" port ".\n")
               (run-jetty make-handler {:port port
                                        :join? false}))))
  server)


(defn -main [& [port]]
  (component/start system))
