This is a very small, yet surprisingly complete, example of using Clojure to serve up a RESTful API and to provide a user interface using Thymeleaf and HTMX. It uses a variety of technologies to demonstrate how this is possible. The project offers a full API CRUD (Create, Read, Update and Delete) experience over some Star Trek starships!
This backend is written in Clojure and uses a PostgreSQL database to store a list of starships. The technologies in play include:
The frontend is rendered using:
Additionally:
-
Also includes tests (including transactional rollbacks per test)!
A full list of the technologies that are used can be found in the deps.edn
file.
This service also exposes Prometheus metrics AND a JMX endpoint for your enjoyment!
❗
|
If you want to use the supplied |
There are at least 3 ways to launch this project:
❗
|
You need to have the database and cache services running first (in
a separate terminal). The command to do this, if you have
|
Now, then for the first option:
❗
|
You need to copy the |
Fire up your repl using:
-
clj -M:dev
to load up the dev
alias defined in the deps.edn
then
-
(require 'dev)
-
(dev/go)
to start the system.
❗
|
You need to copy the |
-
just run-local
or
-
bin/build
-
bin/run-local
❗
|
You don’t need to copy any config file, as the docker container is configured
to use |
-
just run-docker
or
-
bin/build
-
bin/imagify
-
bin/run-docker
Once the service is up and running (and listening by default on port 8080),
there are a few examples of interrogating the API to pull back some data. The
scripts can be found in scripts/bb
.
❗
|
You will need Babashka installed. |
To run a script just execute them on the command line, i.e.,
./find-all-starships.clj
If you have jet installed, you can convert the EDN response to JSON like so:
./find-all-starships.clj | jet -o json
Open up a browser and visit http://localhost:8080/
From that page, click on
the login
link. The username and password has been pre-filled in for you.
Find the full unlicense in the UNLICENSE
(and
LICENSE
) file, but here’s a snippet:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
I welcome feedback. I can usually be found hanging out in the #clojure-uk
or
#clojure-europe
channels on Clojurians Slack.
You can also email me if you wish :-)
Live Long and Prosper!