 (ns emender-service.server)
 
 (require '[ring.util.response     :as http-response])
+(require '[clojure.pprint         :as pprint])
 (require '[clojure.data.json      :as json])
 
 (require '[emender-service.html-renderer :as html-renderer])
 (require '[emender-service.results       :as results])
+(require '[emender-service.config        :as config])
 (require '[emender-service.rest-api      :as rest-api])
 
 (defn render-front-page

        "/bootstrap.min.js"    (return-file "bootstrap.min.js"    "application/javascript")
        "/"                    (render-front-page request)))

; todo API call prefix -> config

(defn handler
    "Handler that is called by Ring for all requests received from user(s)."
    [request]
    (println "request URI: " (:uri request))
    (println (:configuration request))
    (let [uri    (:uri request)
          method (:request-method request)]
         (if (.startsWith uri "/v1")
             (api-call-handler request uri method)
             (non-api-call-handler request uri))))


    []
    (.. java.net.InetAddress getLocalHost getHostName))

(defn api-call-handler
    [request uri method]
    (condp = [method (subs uri (count (config/get-api-prefix request)))]
        [:get  "/info"]         (rest-api/info-handler request (get-hostname))
        [:post "/job-started"]  (rest-api/job-started-handler request)
        [:post "/job-finished"] (rest-api/job-finished-handler request)
                                (rest-api/unknown-call-handler uri method)))

