 (defn http-handler
   [req]
   (if-let [swagger (::swagger @shim-socket)]
-    (let [app (make-app swagger)]
-      (app req))
-    {:status 404 :body "I'm swagger-less"}
+    (let [app (make-app swagger)
+          resp (app req)]
+      resp
+      )
+    {:status 404 :headers {"Content-Type" "text/plain"} :body "No Swagger Defined. Unable to route request\n"}
     ))
 
 (defonce end-server (atom nil))

            [io.sarnowski.swagger1st.core :as s1st]
            [io.sarnowski.swagger1st.util.security :as s1stsec]
            [io.sarnowski.swagger1st.context :as s1ctx])
  (:import (java.net ServerSocket Socket)
           (java.util Base64 UUID)
           (java.io BufferedWriter OutputStreamWriter BufferedReader InputStreamReader OutputStream InputStream ByteArrayInputStream)))


(set! *warn-on-reflection* true)

(defn- listen-to-input
  "Listen to the incoming socket"
  [^InputStream input]
  (let [in (BufferedReader. (InputStreamReader. input "UTF-8"))
        continue (atom true)]
    (try
      (while @continue
        (let [line (.readLine in)]
          (if line
            (let [bytes (.decode (Base64/getDecoder) line)
                  map (json/parse-stream (BufferedReader. (InputStreamReader. (ByteArrayInputStream. bytes) "UTF-8")))
                  map (fu/keywordize-keys map)
                  cmd (:cmd map)]
              (cond
                (= "setSwagger" cmd)
                (set-swagger (:swagger map))

                (= "hello" cmd)
                (clojure.tools.logging/log :info "We got hello from the client")

                (= "reply" cmd)
                (do-reply map)

                :else nil
                )
              )
            (reset! continue false)
            )))
      (finally
        (shutdown-socket)
        )
      )))

(defn- listen-to-socket
  "Accept socket connections"
  [^ServerSocket server-socket]
  (shutdown)
  (reset! shim-socket {::server-socket server-socket
                       ::socket        nil
                       ::input         nil
                       ::requests      {}
                       ::output        nil})
  (try
    (while (not (.isClosed server-socket))
      (let [socket (.accept server-socket)
            input (.getInputStream socket)
            output (.getOutputStream socket)
            thread (Thread. (fn [] (listen-to-input input)) "Input Listener")]
        (swap! shim-socket #(merge % {::socket socket
                                      ::input  input
                                      ::output output}))
        (.start thread)
        )

      )
    (catch SocketException se nil)                          ;; don't worry about closed socket closing
    (catch Exception e (clojure.tools.logging/log :error e "Closed server socket"))
    (finally
      (shutdown)))
  )

(defn exec-app
  [op-i req]
  (let [req2 (dissoc req :swagger)
        req2 (assoc req2 :body (get-in req [:parameters :body]))]
    (let [uuid (.toString (UUID/randomUUID))
          p (promise)]
      (swap! shim-socket assoc-in [::requests uuid] p)
      (send-message {:cmd     "invoke"
                     :headers (:headers req)
                     :class   op-i
                     :replyTo uuid
                     :body    (if (:body req2) (json/generate-string (:body req2)) nil)})
      (let [answer (deref p 10000 ::failed)]
        (if (= ::failed answer)
          {:status 500 :headers {"Content-Type" "text/plain"} :body "Didn't get the answer"}
          (let [response (:response answer)
                response (update response :headers fu/stringify-keys)
                response (if (and (:body response) (:decodeBody answer))
                           (assoc response
                             :body
                             (ByteArrayInputStream. (.decode (Base64/getDecoder) ^String (:body response))))
                           response)
                ]
            response
            ))))))

(defn resolve-app
  [req]
