Skip to content

Commit

Permalink
refactor and add doc string
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Kiva-Meyer committed Jun 12, 2014
1 parent 25677a4 commit c2ca944
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/omelette/serve.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@
[compojure.core :as compojure]
[compojure.handler :as handler]
[org.httpkit.server :as http-kit]
[ring.middleware.anti-forgery :as ring-anti-forgery]))
[ring.middleware.anti-forgery :refer [wrap-anti-forgery]]))

(defrecord Server [port]
component/Lifecycle
(start
[component]
(if (:server component)
(if (:stop! component)
component
(let [server
(-> component
:router
:ring-routes
(ring-anti-forgery/wrap-anti-forgery {:read-token #(-> % :params :csrf-token)})
(wrap-anti-forgery {:read-token (comp :csrf-token :params)})
handler/site
(http-kit/run-server {:port (or port 0)}))
port
(-> server meta :local-port)]
(println "Web server running on port " port)
(assoc component :server server :port port))))
(assoc component :stop! server :port port))))
(stop
[component]
(when-let [server (:server component)]
(server :timeout 250))
(dissoc component :server :router)))
(when-let [stop! (:stop! component)]
(stop! :timeout 250))
(dissoc component :stop! :router)))

(defn server []
(map->Server {}))
(defn server
"Takes a port number.
Returns an http-kit server component.
Requires `(get-in this [:router :ring-routes])` to be a routes."
[port]
(map->Server {:port port}))

0 comments on commit c2ca944

Please sign in to comment.