How to connect a web application to Hunchentoot

I am writing a web application that will require a hunchentoot web server. I have almost no knowledge of hunchentoot or any web server, and I wonder how my application, written in Common Lisp, will serve pages for the web client. I saw some great examples (e.g. Hunchentoot Primer , Lisp for the Internet) esp. the one listed on the Hunchentoot page. Do you know where I can find more such examples? Thanks.

+4
source share
2 answers

I am wondering how my application, written in Common Lisp, will serve pages for the web client.

Hunchentoot serves everything that is in *dispatch-table* , which is just a list of dispatch handlers.

The simplest task is to maintain a static file. One typical example is a CSS file:

 (push (create-static-file-dispatcher-and-handler "/example.css" "example.css") *dispatch-table*) 

For a web application, you most likely want to dynamically create a web page. You do this by defining a function that returns the page as a string (for example, with CL-WHO), then creating a handler for this function:

 (defun foo () (with-html-output-to-string ; ... )) (push (create-prefix-dispatcher "/foo.html" 'foo) *dispatch-table*) 

You can eliminate many patterns through macros, by the way:

  (defmacro standard-page ((title) & body body)
   `(with-html-output-to-string (* standard-output * nil: prologue t: indent t)
      (: html: xmlns "http://www.w3.org/1999/xhtml"
           : xml \: lang "de"
           : lang "de"
           (: head 
            (: meta: http-equiv "Content-Type" 
                       : content "text / html; charset = utf-8")
            (: title, title)
                (: link: type "text / css" 
               : rel "stylesheet"
               : href "/example.css"))
               (: body
                , @ body))))

 (defmacro defpage (name (title) & body body)
   `(progn
      (defmethod, name ()
        (standard-page (, title)
          , @ body))
      (push (create-prefix-dispatcher, (format nil "/~(~a~).html" name) ', name)
            * dispatch-table *)))

The examples you provided should be sufficient to get started, and if you encounter any problems, read the manual and ask specific questions.

+8
source

define-easy-handler registers a handler that you define automatically in a global variable that is checked when an HTTP request arrives (the variable is called *easy-handler-alist* ). Thus, this is taken care of automatically. Do you want to use a handler of a different form than the one indicated in the tutorial?

I think there is an example of using Hunchentoot in the Elephant distribution ( Elephant is a permanent database of objects for Common Lisp.)

+2
source

Source: https://habr.com/ru/post/1286523/


All Articles