|
1 |
| - |
2 | 1 | +++
|
3 |
| -title = "Part 1: first project" |
| 2 | +title = "Routing" |
4 | 3 | weight = 15
|
5 | 4 | +++
|
6 | 5 |
|
7 |
| -Let's install the libraries we'll use: |
8 |
| - |
9 |
| -~~~lisp |
10 |
| -(ql:quickload '("hunchentoot" "easy-routes" "spinneret" "djula")) |
11 |
| -~~~ |
12 |
| - |
13 |
| -We'll start by creating our first route, we'll serve local files and |
14 |
| -we'll run more than one web app in the same running image. |
15 | 6 |
|
16 | 7 | {{% notice info %}}
|
17 | 8 |
|
18 |
| -You can create a web project with our project generator: [cl-cookieweb](https://github.com/vindarel/cl-cookieweb). |
| 9 | +I prefer the easy-routes library than pure Hunchentoot to define routes, skip to its section if you want. |
19 | 10 |
|
20 | 11 | {{% /notice %}}
|
21 | 12 |
|
22 |
| - |
23 |
| -## Simple webserver |
24 |
| - |
25 |
| -### Serve local files |
26 |
| - |
27 |
| -Create and start a webserver like this: |
28 |
| - |
29 |
| -~~~lisp |
30 |
| -(defvar *acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4242)) |
31 |
| -(hunchentoot:start *acceptor*) |
32 |
| -~~~ |
33 |
| - |
34 |
| -We create an instance of `easy-acceptor` on port 4242 and we start |
35 |
| -it. We can now access [http://127.0.0.1:4242/](http://127.0.0.1:4242/). You should get a welcome |
36 |
| -screen with a link to the documentation and logs to the console. |
37 |
| - |
38 |
| -By default, Hunchentoot serves the files from the `www/` directory in |
39 |
| -its source tree. Thus, if you go to the source of |
40 |
| -`easy-acceptor` (`M-.` in Slime), which is probably |
41 |
| -`~/quicklisp/dists/quicklisp/software/hunchentoot-v1.2.38/`, you'll |
42 |
| -find the `root/` directory. It contains: |
43 |
| - |
44 |
| -- an `errors/` directory, with the error templates `404.html` and `500.html`, |
45 |
| -- an `img/` directory, |
46 |
| -- an `index.html` file. |
47 |
| - |
48 |
| -To serve another directory, we give the option `document-root` to |
49 |
| -`easy-acceptor`. We can also set the slot with its accessor: |
50 |
| - |
51 |
| -~~~lisp |
52 |
| -(setf (hunchentoot:acceptor-document-root *acceptor*) #p"path/to/www") |
53 |
| -~~~ |
54 |
| - |
55 |
| -Let's create our `index.html` first. Put this in a new |
56 |
| -`www/index.html` at the current directory (of the lisp repl): |
57 |
| - |
58 |
| -~~~html |
59 |
| -<html> |
60 |
| - <head> |
61 |
| - <title>Hello!</title> |
62 |
| - </head> |
63 |
| - <body> |
64 |
| - <h1>Hello local server!</h1> |
65 |
| - <p> |
66 |
| - We just served our own files. |
67 |
| - </p> |
68 |
| - </body> |
69 |
| -</html> |
70 |
| -~~~ |
71 |
| - |
72 |
| -Let's start a new acceptor on a new port: |
73 |
| - |
74 |
| -~~~lisp |
75 |
| -(defvar *my-acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4444 |
76 |
| - :document-root #p"www/")) |
77 |
| -(hunchentoot:start *my-acceptor*) |
78 |
| -~~~ |
79 |
| - |
80 |
| -go to [http://127.0.0.1:4444/](http://127.0.0.1:4444/) and see the difference. |
81 |
| - |
82 |
| -Note that we just created another web application on a different port on |
83 |
| -the same lisp image. This is already pretty cool. |
84 |
| - |
85 |
| - |
86 |
| -## Access your server from the internet |
87 |
| - |
88 |
| -With Hunchentoot we have nothing to do, we can see the server from the |
89 |
| -internet right away. |
90 |
| - |
91 |
| -If you evaluate this on your VPS: |
92 |
| - |
93 |
| - (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242)) |
94 |
| - |
95 |
| -You can see it right away on your server's IP. |
96 |
| - |
97 |
| -Stop it with `(hunchentoot:stop *)`. |
98 |
| - |
99 |
| -## Routing |
100 |
| - |
101 |
| -> Note: you can skip to the easy-routes part if you want. |
| 13 | +## Hunchentoot |
102 | 14 |
|
103 | 15 | ### The dispatch table
|
104 | 16 |
|
@@ -182,7 +94,7 @@ It also has a `default-parameter-type` which we'll use in a minute to get url pa
|
182 | 94 | There are also keys to know for the lambda list. Please see the documentation.
|
183 | 95 |
|
184 | 96 |
|
185 |
| -### Easy-routes |
| 97 | +## Easy-routes |
186 | 98 |
|
187 | 99 | [easy-routes](https://github.com/mmontone/easy-routes) is a route
|
188 | 100 | handling extension on top of Hunchentoot. It provides:
|
@@ -310,24 +222,3 @@ or a compound list:
|
310 | 222 | - `'(:hash-table <type>)`
|
311 | 223 |
|
312 | 224 | where `<type>` is a simple type.
|
313 |
| - |
314 |
| - |
315 |
| -<!-- ## Sessions --> |
316 |
| - |
317 |
| -<!-- ## Cookies --> |
318 |
| - |
319 |
| -## Error handling |
320 |
| - |
321 |
| -In all frameworks, we can choose the level of interactivity. The web |
322 |
| -framework can return a 404 page and print output on the repl, it can |
323 |
| -catch errors and invoke the interactive lisp debugger, or it can show |
324 |
| -the lisp backtrace on the html page. |
325 |
| - |
326 |
| -### Hunchentoot |
327 |
| - |
328 |
| -The global variables to set are `*catch-errors-p*`, |
329 |
| -`*show-lisp-errors-p*` and `*show-lisp-backtraces-p*`. |
330 |
| - |
331 |
| -Hunchentoot also defines condition classes. |
332 |
| - |
333 |
| -See the documentation: [https://edicl.github.io/hunchentoot/#conditions](https://edicl.github.io/hunchentoot/#conditions). |
0 commit comments