I’m still very new to Clojure. When tackling new languages I always try to take inspiration from other projects, in regards to directory structure and general organization. This rings especially true with web application because they’re pretty clear-cut in how they should be organized, in my opinion. They should be organized somewhat loosely around your main data entities. So if you have a data entity named “message”, you’d probably have a directory for models, routes and views containing code for that message entity. That’s the organizational structure that I tend to aim for, anyway.
It’s actually extremely straight-forward to do this in Clojure. It’s very similar to how I do it in Python or Node.js. First, start a new Compojure project, using Leiningen.
Go into the generated
src\super-tidy directory and you will see your
handler.clj file. We’ll register all of our routes in this
handler.clj file. But first, make a directory right next to the
handler.clj file, named
routes. Inside of the
routes directory, create a new Clojure file named
messages.clj, for example. All of the URL endpoints handling message-data-related requests can cleanly be placed in this file.
Lets start with the
messages.clj code. Because Compojure allows nesting of routes, all we need to do in this file is define our routes.
With that, we have a few routes that we can mess with. Next, we need to register these routes with the main Compojure application. So, step out a directory level and add the following code to
Here we call
defroutes and pass in our various defined route handlers. I included a few more to show how multiple handlers can be registered.
Now you should be able to start up the web server and hit these various routes! Good luck!
— Ryan Cole