Fix bug in constructCommentControls.
[lw2-viewer.git] / src / routes.lisp
blobba19524a09746982171c95127d4c75d574171aa9
1 (uiop:define-package #:lw2.routes
2 (:use #:cl #:lw2.utils)
3 (:export #:route #:route-name #:standard-route #:execute-route
4 #:function-route #:no-match
5 #:regex-route))
7 (in-package #:lw2.routes)
9 (defclass route ()
10 ((name :initarg :name :accessor route-name :type symbol)
11 (handler :initarg :handler :type function)))
13 (defclass standard-route (route)
14 ((uri :initarg :uri :type string)))
16 (defmethod execute-route ((r standard-route) request-uri)
17 (with-slots (uri handler) r
18 (if (string= uri request-uri)
19 (progn
20 (funcall handler)
22 nil)))
24 (defclass function-route (route)
25 ((function :initarg :function :type function)))
27 (define-condition no-match () ())
29 (defmethod execute-route ((r function-route) request-uri)
30 (with-slots (function handler) r
31 (handler-case
32 (progn
33 (multiple-value-call handler (funcall function request-uri))
35 (no-match () nil))))
37 (defclass regex-route (function-route) ())
39 (defmethod initialize-instance :around ((r regex-route) &rest args &key regex &allow-other-keys)
40 (let* ((scanner (ppcre:create-scanner regex))
41 (function
42 (lambda (request-uri)
43 (multiple-value-bind (match? strings)
44 (ppcre:scan-to-strings scanner request-uri)
45 (if match?
46 (values-list (coerce strings 'list))
47 (signal (load-time-value (make-condition 'no-match))))))))
48 (apply #'call-next-method
50 :function function
51 (map-plist
52 (lambda (k v) (unless (eq k :regex) (list k v)))
53 args))))