                                {:id id, :host upstream})))]
     (when old-isntances (router/delete-domain front-url))
     (apply router/add-domain front-url (map :host new-instances))
-    (swap! apps assoc (keyword app-name) {:image      image
-                                          :tag        tag
-                                          :user-id    (:id user)
-                                          :front-urls [front-url]
-                                          :instances  new-instances})
+    (set-app app-name {:image      image
+                       :tag        tag
+                       :user-id    (:id user)
+                       :front-urls [front-url]
+                       :instances  new-instances})
     (when old-isntances (delete-instances old-isntances))
     nil))
 

  )

(defn delete [app-name user]
  (let [instances (get-in @apps [(keyword app-name) :instances])]
    (delete-instances instances)
    (swap! apps dissoc (keyword app-name))
    (router/delete-domain (default-frontend-url app-name))
    nil))


            [k2nr.docker.container :as container]
            [dokkaa-builder.apps-router :as router]
            [dokkaa-builder.config :as config]
            [clojurewerkz.urly.core :as urly]
            [dokkaa-builder.redis :refer [wcar*]]
            [taoensso.carmine :as redis]))

(declare delete-instances)

(defn app [user app-name]
  (wcar* (redis/get (str "apps:" app-name))))

(defn set-app [app-name app]
  (wcar* (redis/set (str "apps:" app-name) app)))

(defn delete-app [app-name]
  (wcar* (redis/del (str "apps:" app-name))))

(defn- pick-backends
  "Randomly choose docker backend"
  []
  (let [cli (docker/make-client "127.0.0.1:4243")]
    [{:client cli
      :port (router/generate-host-port (.host cli))}]))

(defn- default-frontend-url [app-name]
  (str app-name "." (config/domain-name)))
