66 lines
1.4 KiB
Common Lisp
66 lines
1.4 KiB
Common Lisp
(in-package :web-apps)
|
|
|
|
(defun get-product (n)
|
|
;; Query the DB.
|
|
(list n (format nil "Product nb ~a" n) 9.99))
|
|
|
|
(defun products (&optional (n 5))
|
|
(loop for i from 0 below n
|
|
collect (get-product i)))
|
|
|
|
(defvar *server* nil
|
|
"Server instance (Hunchentoot acceptor).")
|
|
|
|
(defparameter *port* 8899 "The application port.")
|
|
|
|
(defun render (template &rest args)
|
|
(apply
|
|
#'djula:render-template*
|
|
(djula:compile-string template)
|
|
nil
|
|
args))
|
|
|
|
(defparameter *template-root* "
|
|
<title> Lisp web app </title>
|
|
<body>
|
|
<ul>
|
|
{% for product in products %}
|
|
<li>
|
|
<a href=\"/product/{{ product.0 }}\">{{ product.1 }} - {{ product.2 }}</a>
|
|
</li>
|
|
{% endfor %}
|
|
</ul>
|
|
</body>
|
|
")
|
|
|
|
(defparameter *template-product* "
|
|
<body>
|
|
{{ product }}
|
|
{% if debug %}
|
|
debug info!
|
|
{% endif %}
|
|
</body>
|
|
")
|
|
|
|
(easy-routes:defroute root ("/") ()
|
|
(render *template-root* :products (products)))
|
|
|
|
(easy-routes:defroute product-route ("/product/:n")
|
|
(&get debug &path (n 'integer))
|
|
(render *template-product* :product (get-product n) :debug debug))
|
|
|
|
(defroute root ("/" :method :get) ()
|
|
(render-products))
|
|
|
|
|
|
|
|
(defun start-server (&key (port *port*))
|
|
(format t "~&Starting the web server on port ~a~&" port)
|
|
(force-output)
|
|
(setf *server* (make-instance 'easy-routes:easy-routes-acceptor
|
|
:port port))
|
|
(hunchentoot:start *server*))
|
|
|
|
(defun stop-server (&optional (server *server*))
|
|
(hunchentoot:stop server))
|