|
| 1 | +--- |
| 2 | +title: "Clojure Web Service from scratch" |
| 3 | +date: |
| 4 | + created: 2020-12-31 |
| 5 | + updated: 2025-05-30 |
| 6 | +authors: |
| 7 | + - practicalli |
| 8 | +categories: |
| 9 | + - clojure |
| 10 | +tags: |
| 11 | + - clojure |
| 12 | + - api |
| 13 | +draft: true |
| 14 | +--- |
| 15 | + |
| 16 | +An API web service in Clojure can be easily created from Scratch using the REPL and a few Clojure tools. |
| 17 | + |
| 18 | +Only requirements are a Java VM, Clojure CLI and your favourite editor (with Clojure support ideally). |
| 19 | + |
| 20 | +> NOTE: for a working server, clone xxx or generate a project using the [Practicalli Project Templates]() |
| 21 | +
|
| 22 | + |
| 23 | +## Create a simple Clojure project |
| 24 | + |
| 25 | +> NOTE: new projects should start simply to avoid creating unnecessary abstractions of levels of indirection |
| 26 | +
|
| 27 | +Create a project configuration file containing an empty map and the `src` and `test` directories. |
| 28 | + |
| 29 | +```shell |
| 30 | +echo '{}' > deps.edn && \ |
| 31 | +mkdir src test |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +> The install of Clojure CLI includes the latest stable version of Clojure and includes the `src` directory on the JVM classpath, so any Clojure files under `src` can be loaded into the REPL. |
| 36 | +
|
| 37 | +Start a REPL, ideally with an nREPL server so we can connect an editor to it. |
| 38 | + |
| 39 | +```shell |
| 40 | +clojure -M:repl/rebel |
| 41 | +``` |
| 42 | + |
| 43 | +Test the REPL is workng by typing a Clojure expression, my favourite test is |
| 44 | + |
| 45 | +```clojure |
| 46 | +(map inc [1 2 3 ]) |
| 47 | +``` |
| 48 | + |
| 49 | +This should return a sequence of `(2 3 4)`. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +## Web Server |
| 54 | + |
| 55 | +Add an embedded web server to run the API, using the Ring specification for processing HTTP requests and responses as Clojure hash-maps. |
| 56 | + |
| 57 | +Reitit libraries will be used to run the web server, manage routing, middleware, requests and responses. |
| 58 | + |
| 59 | +Use the Clojure CLI to find the current version of a library (in a separate terminal window to the REPL) |
| 60 | + |
| 61 | +```shell |
| 62 | +clojure -X:deps find-versions :lib metosin/reitit |
| 63 | +``` |
| 64 | + |
| 65 | +The latest version is `0.9.1` |
| 66 | + |
| 67 | +```shell-output |
| 68 | +❯ clojure -X:deps find-versions :lib metosin/reitit |
| 69 | +
|
| 70 | +Downloading: metosin/reitit/maven-metadata.xml from clojars |
| 71 | +{:mvn/version "0.7.0-alpha8"} |
| 72 | +{:mvn/version "0.7.0"} |
| 73 | +{:mvn/version "0.7.1"} |
| 74 | +{:mvn/version "0.7.2"} |
| 75 | +{:mvn/version "0.8.0-alpha1"} |
| 76 | +{:mvn/version "0.8.0"} |
| 77 | +{:mvn/version "0.9.0"} |
| 78 | +{:mvn/version "0.9.1"} |
| 79 | +❯ |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +Add the library to the REPL using the add-libs library |
| 84 | + |
| 85 | +```clojure |
| 86 | +;; add-libs function call |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +I am happy the library is working, so I'll add the library to the `deps.edn` project configuration |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +## Request routing |
| 95 | + |
| 96 | +TODO: add reitit-ring router |
| 97 | + |
| 98 | + |
| 99 | +Define routes |
| 100 | + |
| 101 | +Define default route |
| 102 | + |
| 103 | +Add Swagger docs |
| 104 | + |
| 105 | +Test API with swagger UI |
| 106 | + |
| 107 | +Test API via REPL. |
0 commit comments