   "This wraps a handler such that requested for /healthcheck"
   [handler]
   (fn [{:as request :keys [uri]}]
-    (log/debug "uri:" uri)
     (if (= uri "/healthcheck")
       (healthcheck-handler request)
       (handler request))))

  (:require
    ;; XXX Deprecate; use component config directly
    [clojure.core.async :as async :refer [chan >!! <!! >! <! go]]
    [clojure.tools.logging :as log]

    [ring.component.jetty :as jetty]
    [com.stuartsierra.component :as component]

  [config opts app jetty-server]
  component/Lifecycle
  (start [component]
    (log/info ">> Starting server component with config:" config)
    (let [wrapped-handler (wrap-handler (:handler app))
          jetty-server (jetty/jetty-server (merge {:app (merge app {:handler wrapped-handler})}
                                                  (:server config)
                                                  opts))]
      (component/start jetty-server)
      (assoc component :jetty-server jetty-server)))
  (stop [component]
    (log/info "<< Stopping server component")
    (component/stop jetty-server)
    component))


(defn create-server
  ;; Default handler just return the healthcheck response always
  ([opts]
   (let [opts (merge {:app {:handler healthcheck-handler}}
                     opts)]
     (map->Server opts)))
  ([]
   (create-server {})))
