Compojure and Integrant

Here's how to set up Integrant for a Compojure or Compojure-api project...

14 December 2017

Recently I’ve been playing around with some compojure-api stuff for some side projects and alphas and decided somewhat foolhardily to try moving away from Mount as my component management tool. I like Mount; despite some drawbacks of the pattern, modelling databases as singletons clicks with me logically and as I don’t tend to run tests from the repl, I’ve not had the same pain points around testing and reloading that many have had with it.

Anyway, integrating Integrant was a bit of a pain, but here’s how you do it:

NB: it’s worth saying at this stage that choosing Duct as your web toolkit and using integrant-repl will allow you to adopt a reloaded-style workflow more easily than integrating with an existing project.

You can see that this is a pretty different pattern from just dereferencing a var, and we’ll see how you can set that it up to work in that way in development in just a sec.

So, for a simple compojure-api project with just a web stack and database, we might have the following config file:

We then write initializers for Integrant so it can make components out of our connections and depedencies. To componentize our web stack, we simply wrap the def in a closure and make it a defn.

Finally, if you want to manually be able to reference your system and start and stop it in development, you can use something like the below, although checking out integrant-repl is probably the best call.

I’ll hopefully do a post soon on how to prep this for production use.

Update: I’ve added some notes on getting integrant-repl set up for dev here.

Fork me on GitHub