             [k2nr.docker.container :as container]
             [dokkaa-builder.apps-router :as router]
             [dokkaa-builder.config :as config]
-            [clojurewerkz.urly.core :as urly]))
+            [clojurewerkz.urly.core :as urly]
+            [dokkaa-builder.redis :refer [wcar*]]
+            [taoensso.carmine :as redis]))
 
 (declare delete-instances)
 
-(def apps (atom {}))
+(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 (generate-host-port (.host cli))}]))
+      :port (router/generate-host-port (.host cli))}]))
 
 (defn- default-frontend-url [app-name]
   (str app-name "." (config/domain-name)))

        url (str "http://" (urly/host-of host) ":" port)]
    url))

(defn app->id [app-name user]
  (get-in @apps [(keyword app-name) :instances 0 :id]))

(defn create [app-name user image & {:keys [tag command port]}]
  (let [backends (pick-backends)
        front-url (default-frontend-url app-name)
        old-isntances (get-in @apps [(keyword app-name) :instances])
        new-instances (vec (for [{cli :client host-port :port} backends]
                             (let [id (docker/run cli image
                                        :tag tag

                               {:id id, :host upstream})))]
    (when old-isntances (router/delete-domain front-url))
    (apply router/add-domain front-url (map :host 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))

