Skip to content

Instantly share code, notes, and snippets.

@micha
Created November 21, 2014 15:28
Show Gist options
  • Select an option

  • Save micha/b355082eee0379cb0bb9 to your computer and use it in GitHub Desktop.

Select an option

Save micha/b355082eee0379cb0bb9 to your computer and use it in GitHub Desktop.
(ns pandeiro.http
{:boot/export-tasks true}
(:require
[boot.pod :as pod]
[boot.util :as util]
[boot.core :as core]
[boot.task.built-in :as task]))
(core/deftask serve
"Start a web server on localhost and serve a directory.
If no directory is specified the current one is used. Listens on
port 3000 by default."
[d dir PATH str "The directory to serve."
p port PORT int "The port to listen on."
n no-block NO_BLOCK bool "Non-blocking (to compose serve with other tasks)"]
(let [worker (pod/make-pod
{:dependencies '[[ring/ring-jetty-adapter "1.3.1"]
[compojure "1.2.1"]]})
dir (or dir ".")
port (or port 3000)
no-block (or no-block false)]
(core/cleanup
(util/info "<< stopping Jetty... >>")
(pod/eval-in worker (.stop server)))
(comp
(core/with-pre-wrap
(pod/eval-in worker
(require '[ring.adapter.jetty :refer [run-jetty]]
'[compojure.route :refer [files]])
(def server (run-jetty (files "/" {:root ~dir}) {:port ~port :join? false})))
(util/info "<< started web server on http://localhost:%d (serving: %s) >>\n" port dir))
(if no-block identity (task/wait)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment