An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

lein-try rocks - instant HTTP server!

October 17, 2013 ·

The lein-try plugin is one of the more stunningly useful "little pieces" of software that you can have available if you're developing in Clojure. Simply add the plugin to your user profile (in ~/.lein/profiles.clj) and then lein try some-dependency will fire up a REPL with the specified dependency on the classpath.

Today, I needed a simple HTTP server running for a short while, serving up static assets from a specific folder tree. Instead of messing with my local Apache setup, I used lein-try to fire up a temporary HTTP server:

> lein try ring/ring-core 1.2.0 ring/ring-jetty-adapter 1.2.0
...
user=> (use 'ring.adapter.jetty)
nil
user=> (use 'ring.middleware.resource)
nil
user=> (def app (wrap-resource (constantly {:headers {"Location" "index.html"} :status 301}) "."))
#'user/app
user=> (run-jetty app {:port 3333})
2013-10-17 18:03:14.792:INFO:oejs.Server:jetty-7.6.8.v20121106
2013-10-17 18:03:14.819:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3333

That starts a Jetty instance on port 3333 with a very simple Ring-based web application:

  • If static assets are requested, they are served up (by wrap-resource) relative to the current folder ("."),
  • Else you are redirected to index.html.

Tags: clojure

5 responses

  • 1 ZC // Oct 18, 2013 at 5:19 AM

    Why not just:

    $ cd /home/somedir
    $ python -m SimpleHTTPServer

    Serving HTTP on 0.0.0.0 port 8000 ...
  • 2 Nikita Beloglazov // Oct 18, 2013 at 5:02 PM

    lein-try is nice. But if you on linux it most certainly has python. It has VERY simple builtin HTTP server http://m.linuxjournal.com/content/tech-tip-really-simple-http-server-python
  • 3 Sean Corfield // Oct 19, 2013 at 11:43 PM

    You're missing the point. I work in Clojure all day, every day, and I work on machines that don't have Python on them (shock! horror!).
  • 4 Bill Allen // Feb 5, 2014 at 8:00 AM

    lein-try is great for what it does, but for a simple web server lein-simpleton might be a better solution.
  • 5 Sean Corfield // Feb 7, 2014 at 8:27 PM

    Nice, Bill, I didn't know about that!