path parameter
This commit is contained in:
+28
-9
@@ -1,36 +1,55 @@
|
|||||||
(in-package :web-apps)
|
(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))
|
(defun products (&optional (n 5))
|
||||||
(loop for i from 0 below n
|
(loop for i from 0 below n
|
||||||
collect (list i
|
collect (get-product i)))
|
||||||
(format nil "Product nb ~a" i)
|
|
||||||
9.99)))
|
|
||||||
|
|
||||||
(defvar *server* nil
|
(defvar *server* nil
|
||||||
"Server instance (Hunchentoot acceptor).")
|
"Server instance (Hunchentoot acceptor).")
|
||||||
|
|
||||||
(defparameter *port* 8899 "The application port.")
|
(defparameter *port* 8899 "The application port.")
|
||||||
|
|
||||||
|
(defun render (template &rest args)
|
||||||
|
(apply
|
||||||
|
#'djula:render-template*
|
||||||
|
(djula:compile-string template)
|
||||||
|
nil
|
||||||
|
args))
|
||||||
|
|
||||||
(defparameter *template-root* "
|
(defparameter *template-root* "
|
||||||
<title> Lisp web app </title>
|
<title> Lisp web app </title>
|
||||||
<body>
|
<body>
|
||||||
<ul>
|
<ul>
|
||||||
{% for product in products %}
|
{% for product in products %}
|
||||||
<li> {{ product.1 }} - {{ product.2 }} </li>
|
<li>
|
||||||
|
<a href=\"/product/{{ product.0 }}\">{{ product.1 }} - {{ product.2 }}</a>
|
||||||
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
")
|
")
|
||||||
|
|
||||||
(defun render-products ()
|
(defparameter *template-product* "
|
||||||
(djula:render-template*
|
<body>
|
||||||
(djula:compile-string *template-root*)
|
{{ product }}
|
||||||
nil
|
</body>
|
||||||
:products (products)))
|
")
|
||||||
|
|
||||||
|
(easy-routes:defroute root ("/") ()
|
||||||
|
(render *template-root* :products (products)))
|
||||||
|
|
||||||
|
(easy-routes:defroute product-route ("/product/:n") (&path (n 'integer))
|
||||||
|
(render *template-product* :product (get-product n)))
|
||||||
|
|
||||||
(defroute root ("/" :method :get) ()
|
(defroute root ("/" :method :get) ()
|
||||||
(render-products))
|
(render-products))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defun start-server (&key (port *port*))
|
(defun start-server (&key (port *port*))
|
||||||
(format t "~&Starting the web server on port ~a~&" port)
|
(format t "~&Starting the web server on port ~a~&" port)
|
||||||
(force-output)
|
(force-output)
|
||||||
|
|||||||
Reference in New Issue
Block a user