Simple web server
Before dwelving into web development, we might want to do something simple: serve some files we have on disk.
Serve local files
If you followed the tutorial you know that we can create and start a webserver like this:
(defvar *acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4242))
(hunchentoot:start *acceptor*)
We create an instance of easy-acceptor
on port 4242 and we start
it. We can now access http://127.0.0.1:4242/. You should get a welcome
screen with a link to the documentation and logs to the console.
You can also use Roswell’s http.server from the command line:
$ ros install roswell/http.server
$ ros -s http.server
Hunchentoot server is going to start.
Listening on 127.0.0.1:5000.
By default, Hunchentoot serves the files from the www/
directory in
its source tree. Thus, if you go to the source of
easy-acceptor
(M-.
in Slime), which is probably
~/quicklisp/dists/quicklisp/software/hunchentoot-v1.2.38/
, you’ll
find the root/
directory. It contains:
- an
errors/
directory, with the error templates404.html
and500.html
, - an
img/
directory, - an
index.html
file.
To serve another directory, we give the option document-root
to
easy-acceptor
. We can also set the slot with its accessor:
(setf (hunchentoot:acceptor-document-root *acceptor*) #p"path/to/www")
Let’s create our index.html
first. Put this in a new
www/index.html
at the current directory (of the lisp repl):
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>Hello local server!</h1>
<p>
We just served our own files.
</p>
</body>
</html>
Let’s start a new acceptor on a new port:
(defvar *my-acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4444
:document-root #p"www/"))
(hunchentoot:start *my-acceptor*)
go to http://127.0.0.1:4444/ and see the difference.
Note that we just created another web application on a different port on the same lisp image. This is already pretty cool.
Access your server from the internet
With Hunchentoot we have nothing to do, we can see the server from the internet right away.
If you evaluate this on your VPS:
(hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242))
You can see it right away on your server’s IP.
You can use the :address
parameter of Hunchentoot’s easy-acceptor to
bind it and restrict it to 127.0.0.1 (or any address) if you wish.
Stop it with (hunchentoot:stop *)
.
Now on the next section, we’ll create some routes to build a dynamic website.