 ;; if/when I get more content, I dump that onto in-chan as well,
 ;; and I dump a :closed token onto in-chan when I get there.
 ;;
-;; 
-
-(defn routes [mappings]
-
-  ;; (loop [prefixes (keys mappings)]
-  ;;   (let [prefix (first prefixes)]
-  ;;     (if (.startsWith path prefix)
-  ;;       (let [next (mappings prefix)]
-  ;;         (if (instance? java.util.Map next)
-  ;;           (invoke-view (.substring path (count prefix)) next)
-  ;;           (next))))))
-  )
-
-(defn bing [in-port out-port]
-  )
-
-(run (routes {"/foo" {"/bing" bing}}))
+;;
+
+(defn route [mappings]
+  "TODO: add regex support, too, cuz we should."
+  (let [route** (fn route* [mappings full-path path in-chan out-chan]
+                  (loop [prefixes (keys mappings)]
+                    (let [prefix (first prefixes)]
+                      (println "prefix = " prefix)
+                      (println "path is " path)
+                      (if (.startsWith path prefix)
+                        (let [next (mappings prefix)]
+                          (if (instance? java.util.Map next)
+                            (route* next full-path (.substring path (count prefix)) in-chan out-chan)
+                            (do
+                              (println "invoking" next)
+                              (next path in-chan out-chan))
+                            ))
+                        (recur (rest prefixes))
+                        )
+                      )))]
+    (fn [command path in-chan out-chan]
+      (println "command = " command)
+      (route** (mappings command) path path in-chan out-chan))))
+
+(defn home [path in-port out-port]
+  (println "HOME")
+  (go (>! out-port 200)
+      (>! out-port :headers)
+      (>! out-port {:content-type "text"})
+      (>! out-port :body)
+      (>! out-port (str path " INDEX\n"))
+      (>! out-port :end)))
+
+(defn bing [path in-port out-port]
+  (println "BING")
+  (go (>! out-port 200)
+      (>! out-port :headers)
+      (>! out-port {:content-type "text"})
+      (>! out-port :body)
+      (>! out-port (str path " BING\n"))
+      (>! out-port :end)))
+
+
+(run (route {:get
+             {"/foo" {"/bing" bing}
+              "/" home}}))

         (>!! in-chan :content)
         (>!! in-chan body))

       (route command path in-chan out-chan)
       )
     )
   (parse-request request-string)))

(defn read-socket [selected-key]
  (let [socket-channel (.channel selected-key)]
    (.clear *buffer*)
    (.read socket-channel *buffer*)

            SelectionKey/OP_ACCEPT
            (accept-connection server-socket selector)
            SelectionKey/OP_READ
            (read-socket k rout)))
        (.clear selected-keys)))))


(defn run [router & {:keys [ip port]
                     :or {ip "0.0.0.0" port 8080}}]
  (apply react (conj (setup port) router)))


;; what I want to do:
