   (when-let [id (:Id (load-container-by-host-and-port host port))]
     (stop-container host id)))
 
-(defn health-check-host [host port]
-  (mark-host-health (str host ":" port)
-   (loop [n 15]
-     (when (pos? n)
-       (if (= 200 (:status @(http/get (str "http://" host ":" port "/") {:timeout 5000})))
-         true
-         (do
-           (Thread/sleep (* (- 15 n) 500))
-           (recur (dec n))))))))
-
-(defn health-check-instances []
-  (doseq [l (vals (:instances @router))]
-    (doseq [i l]
-      (let [[host port] (ssplit i)]
-        (health-check-host host port)))))
+(defn health-check-host [host port]  
+  (loop [n 15]
+    (when (pos? n)
+      (if (= 200 (:status @(http/get (str "http://" host ":" port "/") {:timeout 5000})))
+        true
+        (do
+          (Thread/sleep (* (- 15 n) 500))
+          (recur (dec n)))))))
 
 (defn pull-docker-image [host image]
   (let [[image tag] (ssplit image)]


(defn extract-app [req] (first (ssplit ((:headers req) "host"))))

(defn pipe-instance [req app]  
  (if-let [instances (get-in @router [:instances app])]
    (if (seq instances)
      (if-let [instance (rand-nth (vec (clojure.set/difference (set instances) (:unhealthy @router))))]
        (with-channel req channel
          (http/request
           {:url (str (name (:scheme req)) "://" instance (:uri req)
                      (let [q (:query-string req)] (if (clojure.string/blank? q) "" (str "?" q))))
            :method (:request-method req)
            :headers (:headers req)
                                        ;:form-params (:form-params req)
            :body (:body req)
            :user-agent ((:headers req) "user-agent")}
           #(send! channel {:status (:status %)
                            :body (:body %)
                            :headers (into {} (map (fn [[k v]] (vector (name k) v)) (:headers %)))})))
        {:status 503 :body (str "No backend available for " app)})
      {:status 503 :body (str "No backend available for " app)})
    {:status 404 :body (str "No backend found for " app)}))

(defn pipe [req]
  (if-let [app (extract-app req)]
    (let [envs (load-app-envs app)]
      (if (envs "auth-user")
        (let [auth-req (basic-authentication-request req #(and (= % (envs "auth-user")) (= %2 (envs "auth-password"))))]
          (if (:basic-authentication auth-req)
            (pipe-instance auth-req app)
            (authentication-failure nil nil)))
        (pipe-instance req app)))
    {:status 404 :body "Invalid hostname"}))

(defn app [req]
  (if (= (:controller @env) (extract-app req))
    (controller-app req)
    (pipe req)))

(defn init []
  (get-token)
  (init-routing-table)
  (car/with-new-pubsub-listener (:spec (redis-conf)) {"updates" (fn [_] (init-routing-table))}
    (car/subscribe "updates"))
  (future
    (loop []
      (health-check-instances)
      (Thread/sleep 5000)
      (recur))))


(defn kill-app-instance [app host port]
  (println "Killing instance at" (str host ":" port))
  (stop-container-by-port host port)
  (remove-app-instance app (str host ":" port)))

(defn deploy-app-instance [app host port internal-port image]
  (println "Pulling new tags for" image)
