This episode is for subscribers only. Sign Up or Log in to watch it.

31. Component in Practice

Published 01 June 17

Learn how to incorporate Component in a real world project. This episode is a bit of grab bag. It shows how to set up a -main function and how to use it, how to use reloaded.repl and tools.namespace for a better REPL workflow. You get introduced to the Suspendable protocol that complements Component’s Lifecycle protocol. Finally it shows how to streamline your configuration with Aero.

Show notes

In episode 26 you learned the basics of Stuart Sierra’s Component. It’s a small and simple library, but you could fill a book about its practical implications.

That’s because Component provides a language for speaking about systems. We’ve gone over the vocabulary, now it’s time to learn how to speak it for real.

In this episode I’ll show you how to use component in practice, to speed up your development workflow, improve your tests, and manage your application’s lifecycle in production.

Setting up the main system

Let’s start by looking at a typical way to structure your app with Component. The Chestnut application template establishes some conventions which form a good starting point.

Generate a new application with lein new chestnut, followed by the name of your app, and flags for the features you want to include. The Chestnut README has a complete list. I’m tend to use Ring’s site middleware instead of the default API middleware, mainly because it provides session support. I’ll use the http-kit server instead of the default Jetty, and I’m adding the Contributor covenant Code of Conduct to the project.

$ lein new chestnut ep31 +site-middleware +http-kit +code-of-conduct
Retrieving chestnut/lein-template/0.15.0-SNAPSHOT/lein-template-0.15.0-20170517.134032-2.pom from clojars
Generating fresh Chestnut project.
README.md contains instructions to get you started.

This episodes builds upon episode 26, so make sure to check that one first.