Hunchentoot Redirects List

The structure of the URI of my website has changed a lot lately, and I need to redirect all the old pages to their corresponding new pages. I have a dotted list of pairs of all old and new URIs. I'm currently trying to define simple handlers for each of the loops:

(let ((redirects '(("/old/uri/example-1" . "/new/uri/example-1"))))
  (dolist (redirect redirects)
    (hunchentoot:define-easy-handler (???? :uri (first redirect)) ()
       (redirect (rest redirect)))
    ))

Maybe there is a better way. Assuming the define-easy-handler is valid, a function symbol is required for each simple handler. I tried the following to no avail:

  • Location (gensym) where it expects a function symbol
  • Using lists rather than dotted lists and calls (redirection first) where it expects a character
  • Placing a quasi-quarter around the entire object and under-pumping around (first redirect)

What would be a good way to accomplish this?

+1
3

: DEFINE-EASY-HANDLER - .

:

  • -

  • ,

(defredirects (a . a1) (b . b1) (c . c1)))

(progn
  (hunchentoot:define-easy-handler (f-a ... a) () (... a1))
  (hunchentoot:define-easy-handler (f-b ... b) () (... b1))
  (hunchentoot:define-easy-handler (f-c ... c) () (... c1)))
  • , , eval ( compile funcall, ) .
+4

, , . , around HUNCHENTOOT:ACCEPTOR-DISPATCH-REQUEST HUNCHENTOOT:EASY-HANDLER.

:

(defparameter *acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4242))

(hunchentoot:define-easy-handler (foo :uri "/foo") ()
  (format nil "<html><body><h1>Test</h1><p>foo</p></body></html>"))

(hunchentoot:start *acceptor*)

/bar /quux /foo:

;; A simple helper to create prefix dispatchers.
(defun make-redirect-list (redirects)
  (mapcar (lambda (redirect)
            (destructuring-bind (from . to) redirect
              (hunchentoot:create-prefix-dispatcher from
                                                    (lambda ()
                                                      (hunchentoot:redirect to)))))
          redirects))

(defparameter *redirects* (make-redirect-list
                           '(("/bar" . "/foo")
                             ("/quux" . "/foo"))))

(defmethod hunchentoot:acceptor-dispatch-request :around
    ((acceptor hunchentoot:easy-acceptor) request)
  (dolist (redirect *redirects*)
    ;; Match the request against the prefix dispatchers in *REDIRECTS*...
    (let ((handler (funcall redirect request)))
      (when handler
        ;; and call the corresponding handler if a match is found.
        (return-from hunchentoot:acceptor-dispatch-request
          (funcall handler)))))
    ;; Unless a handler was found, call next method to 
    ;; handle the request normally.
  (call-next-method))

: , . , , , /etc. , .

+1

This solution works. I definitely appreciate feedback on whether this is really better.

(defun add-redirect (name from to)
  (eval `(hunchentoot:define-easy-handler (,name :uri ,from) ()
    (redirect ,to))))

(defun add-redirects (redirects)
  (dolist (redirect redirects)
    (add-redirect (first redirect) (second redirect) (third redirect))
    ))

(add-redirects
 '(
   (redirect-1 "/redirect-1/" "/destination-1/")
   (redirect-2 "/redirect-2/" "/destination-2/")
   (redirect-3 "/redirect-3/" "/destination-3/")
   ))
0
source

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


All Articles