You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2019-09-16-set-up-clojure-api.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,14 +74,14 @@ We are going to use the [Ring](https://github.com/ring-clojure/ring) library, Ri
74
74
75
75
Inside your *project.clj* edit the **:dependencies** key to the following:
76
76
77
-
```
77
+
```clojure
78
78
:dependencies [[org.clojure/clojure "1.10.0"]
79
79
[ring "1.7.1"]]
80
80
```
81
81
82
82
You can always download the dependencies you add to your project opening the terminal and running the command:
83
83
84
-
```
84
+
```bash
85
85
lein deps
86
86
```
87
87
@@ -128,15 +128,15 @@ Finally, we are defining a ***main*** function, which will be executed when we r
128
128
129
129
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/>
130
130
Open the ***project.clj*** file again, and add this right before the enclosing parenthesis:
131
-
```
131
+
```clojure
132
132
:main app.core
133
133
```
134
134
This is telling leiningen as i said, that there is a main function, on the namespace app.core<br/>
135
135
136
136
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/>
137
137
138
138
Let's try to run the server now, open the terminal inside your project folder and run the command:
139
-
```
139
+
```bash
140
140
lein run
141
141
```
142
142
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
147
147
148
148
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:
149
149
150
-
```
150
+
```clojure
151
151
(nsapp.core
152
152
(:require [ring.adapter.jetty :refer [run-jetty]]
153
153
[clojure.pprint :refer [pprint]]))
154
154
```
155
155
156
156
And now, let's update our handler function, because this is the function that receives our HTTP request:
157
157
158
-
```
158
+
```clojure
159
159
(defnhandler [request]
160
160
(clojure.pprint/pprint request) ; Prints the request on the console
161
161
{:status200
@@ -167,7 +167,7 @@ Remember that what our functions returns is the last expression, and that means
167
167
168
168
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:
169
169
170
-
```
170
+
```clojure
171
171
{:ssl-client-cert nil,
172
172
:protocol"HTTP/1.1",
173
173
: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
203
203
204
204
To start using it, let's update our ***project.clj*** file again, at the ***:dependencies*** key of the map, add:
205
205
206
-
```
206
+
```clojure
207
207
[compojure "1.6.1"]
208
208
```
209
209
@@ -213,7 +213,7 @@ I recommend that later you take a deeper look at it's documentation.
213
213
214
214
Before we start creating routes, we have to update again our namespace require definitions, take a look:
215
215
216
-
```
216
+
```clojure
217
217
(nsapp.core
218
218
(:require [ring.adapter.jetty :refer [run-jetty]]
219
219
[clojure.pprint :refer [pprint]]
@@ -223,7 +223,7 @@ Before we start creating routes, we have to update again our namespace require d
223
223
224
224
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:
225
225
226
-
```
226
+
```clojure
227
227
(defmy-routes
228
228
(routes
229
229
(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
281
281
282
282
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:
283
283
284
-
```
284
+
```clojure
285
285
(nsapp.core
286
286
(:require [ring.adapter.jetty :refer [run-jetty]]
287
287
[clojure.pprint :refer [pprint]]
@@ -315,13 +315,13 @@ ForJSON the two critical middlewares comes from the [Ring-JSON](https://github.c
315
315
316
316
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:
317
317
318
-
```
318
+
```clojure
319
319
[ring/ring-json "0.5.0"]
320
320
```
321
321
322
322
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:
323
323
324
-
```
324
+
```clojure
325
325
(nsapp.core
326
326
(:require [ring.adapter.jetty :refer [run-jetty]]
327
327
[clojure.pprint :refer [pprint]]
@@ -355,7 +355,7 @@ Before we discuss the changes in our routes, let's see this new definition that
355
355
356
356
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:
0 commit comments