Simple boot setup to run a clojure app
I’ve been meaning to try out boot for quite a while. But never quite got there due to the projects I’ve been working on. This week the moment arrived, as I was starting a new project without any immediate external pressure.
The project will eventually combine both rich client and (clojure) backend. But my starting point was getting a ring app running with boot. Due mainly, I guess, to boot’s origins, there’s plenty online about using boot to build frontend (ClojureScript) projects, but relatively little for web projects. For some reason - stupidity, not doing enough reading up front, or some combination of the two - getting this working took me longer than it should have. So here’s the essence for anyone else who may be in a similar place.
For context, here’s my versions:
Here’s the (somehwat minimal) app itself:
And here’s the build.boot file:
With those two pieces (and boot being installed, of course), you should be able to run
boot run and visit http://localhost:3200.
It really is very simple.
Actually, it turns out to be too simple. The application block when Jetty starts, and this in turn blocks the task. The end result is that while the application runs fine, the task itself is not composable.
Take 2: a composable approach
To make the task composable, we’re going to introduce two changes.
One change is to our application itself - we’ll allow passing in a parameter to the main function to specify whether the application should block or not (handily, jetty provides a
join? configuration flag which set to false prevents waiting for the server thread to exit).
The other change is in our task - we’ll start the application in a Pod and require our application only in that context.
For good measure we’ll also allow calling the run task in blocking or non-blocking mode. And also make the server port confgurable.
First of all, here’s the revised application code:
And here is the new build.boot:
This should now be composable with other tasks such as cljs/asset compilation. Still fairly basic, but moving in the right direction.
Incidentally, if serving a ring application is your goal, you may well want to consider boot-http.blog comments powered by Disqus