Skip to content

Commit 152cf85

Browse files
authored
Update 2019-09-16-set-up-clojure-api.md
1 parent 7d22464 commit 152cf85

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

_posts/2019-09-16-set-up-clojure-api.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ We are going to use the [Ring](https://github.com/ring-clojure/ring) library, Ri
7474

7575
Inside your *project.clj* edit the **:dependencies** key to the following:
7676

77-
```
77+
```clojure
7878
:dependencies [[org.clojure/clojure "1.10.0"]
7979
[ring "1.7.1"]]
8080
```
8181

8282
You can always download the dependencies you add to your project opening the terminal and running the command:
8383

84-
```
84+
```bash
8585
lein deps
8686
```
8787

@@ -128,15 +128,15 @@ Finally, we are defining a ***main*** function, which will be executed when we r
128128
129129
Alright. Before we finally turn the server on, we need to specify to Leinigen (which we will use to run our code) that we do have a main function that should run by default. <br/>
130130
Open the ***project.clj*** file again, and add this right before the enclosing parenthesis:
131-
```
131+
```clojure
132132
:main app.core
133133
```
134134
This is telling leiningen as i said, that there is a main function, on the namespace app.core<br/>
135135

136136
Remember, if you get stucked, check how the code is written in the [Github repo](https://github.com/matthewlisp/clj-api-template) of this guide.<br/>
137137

138138
Let's try to run the server now, open the terminal inside your project folder and run the command:
139-
```
139+
```bash
140140
lein run
141141
```
142142
If everything went smooth, open up your browser and go to: [localhost:3000](localhost:3000)<br/>
@@ -147,15 +147,15 @@ Ok, before we proceed, it's important to know how the HTTP requests are represen
147147

148148
The easiest way to do this is by looking at the request, to do that, we're going to use a clojure function that prints data to the terminal in a more readable way, let's update our namespace require definitions:
149149

150-
```
150+
```clojure
151151
(ns app.core
152152
(:require [ring.adapter.jetty :refer [run-jetty]]
153153
[clojure.pprint :refer [pprint]]))
154154
```
155155

156156
And now, let's update our handler function, because this is the function that receives our HTTP request:
157157

158-
```
158+
```clojure
159159
(defn handler [request]
160160
(clojure.pprint/pprint request) ; Prints the request on the console
161161
{:status 200
@@ -167,7 +167,7 @@ Remember that what our functions returns is the last expression, and that means
167167

168168
Now, while the server is running, you can look at the terminal window and see how the HTTP request is represented, let's take a look:
169169

170-
```
170+
```clojure
171171
{:ssl-client-cert nil,
172172
:protocol "HTTP/1.1",
173173
:remote-addr "127.0.0.1",
@@ -203,7 +203,7 @@ We have a very known routing library in clojure, it's specially made for Ring. T
203203

204204
To start using it, let's update our ***project.clj*** file again, at the ***:dependencies*** key of the map, add:
205205

206-
```
206+
```clojure
207207
[compojure "1.6.1"]
208208
```
209209

@@ -213,7 +213,7 @@ I recommend that later you take a deeper look at it's documentation.
213213

214214
Before we start creating routes, we have to update again our namespace require definitions, take a look:
215215

216-
```
216+
```clojure
217217
(ns app.core
218218
(:require [ring.adapter.jetty :refer [run-jetty]]
219219
[clojure.pprint :refer [pprint]]
@@ -223,7 +223,7 @@ Before we start creating routes, we have to update again our namespace require d
223223

224224
What is happening here? I've added two functions from compojure.core and one from compojure.route, we're going to see them in action now as i create our http routes:
225225

226-
```
226+
```clojure
227227
(def my-routes
228228
(routes
229229
(GET "/endpoint-a" [] "<h1>Hello endpoint A</h1>")
@@ -281,7 +281,7 @@ Well, this says pretty much everything, and why we need this function? It's beca
281281

282282
Alright, the last thing we have to do is replace the handler used by ***run-jetty*** in our ***main*** function, simply because ***my-routes*** returns a handler and this handler takes care now of routing and that's what we wanted in this section, in the end our code is looking like this now:
283283

284-
```
284+
```clojure
285285
(ns app.core
286286
(:require [ring.adapter.jetty :refer [run-jetty]]
287287
[clojure.pprint :refer [pprint]]
@@ -315,13 +315,13 @@ ForJSON the two critical middlewares comes from the [Ring-JSON](https://github.c
315315

316316
But as always, before we use it, we have to update our ***project.clj*** again by importing this lib in our project, open the file and at the ***:dependencies*** key of the map, add:
317317

318-
```
318+
```clojure
319319
[ring/ring-json "0.5.0"]
320320
```
321321

322322
Cool. Because this iteration on our code makes many modifications, i'll paste it here and we are going to breakdown the changes, here we go:
323323

324-
```
324+
```clojure
325325
(ns app.core
326326
(:require [ring.adapter.jetty :refer [run-jetty]]
327327
[clojure.pprint :refer [pprint]]
@@ -355,7 +355,7 @@ Before we discuss the changes in our routes, let's see this new definition that
355355

356356
What is this doing? this is **wrapping middlewares** to our ***my-routes*** handler. And i'm using the threading macro ***->*** because otherwise this code would start being ugly to read, as we possibly will need more middlewares in the future, and things would start being like this:
357357

358-
```
358+
```clojure
359359
(wrap-blablabla (wrap-json-bdoy (wrap-json-response my-routes)))
360360
```
361361

@@ -378,13 +378,13 @@ The rest is pretty much self explanatory, i've used the ***response*** function
378378
* Open the terminal on the project folder and do: lein run
379379
* Open another terminal window and usgin the curl tool, we are going to check the debug endpoint:
380380

381-
```
381+
```bash
382382
$ curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/debug
383383
```
384384

385385
The output:<br/>
386386

387-
```
387+
```bash
388388
{:ssl-client-cert nil,
389389
:protocol "HTTP/1.1",
390390
:remote-addr "127.0.0.1",
@@ -414,11 +414,11 @@ As you can see, now our ***:body*** key has a value of our JSON encoded request,
414414

415415
Let's also test one of our endpoints, the */endpoint-a*:
416416

417-
```
417+
```bash
418418
$ curl http://localhost:3000/endpoint-a
419419
```
420420
The output:<br/>
421-
```
421+
```bash
422422
{"foo":"bar"}
423423
```
424424

0 commit comments

Comments
 (0)