              [:database]))))
 
 (def system (prod-system {:github-address "http://github.com/api/v3"
-                         :datomic-url "datomic:dev://localhost:4334/mbrainz-1968-1973"}))
+                          :datomic-url "datomic:dev://localhost:4334/mbrainz-1968-1973"}))
 
-(defn run [& [port]]
+#_(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 http-handler {:port port
+               (run-jetty make-handler {:port port
                                         :join? false}))))
   server)
 
-(defn sum [req]
-  {:status 200
-   :body (pr-str (find-countries (:database system)))
-   :headers {"Content-Type" "application/edn"}})
-
-
-
-(def http-handler [database]
-  (if is-dev?
-    (reload/wrap-reload (api #'routes))
-    (api (routes
-           (resources "/")
-           (resources "/react" {:root "react"})
-           (GET "/sum" req sum database)
-           (GET "/*" req (page))))))
-
-
-
 
 (defn -main [& [port]]
-  (run port))
+  (component/start system))

(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]]

         :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"}})

(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 :jetty (run-jetty (make-handler database) {:port port :join? false})))))
  (stop [this]
    (.stop (:jetty this))))

(defn new-database [uri]
  (Database. uri nil))
