joaotavora / snooze

Common Lisp RESTful web development
207 stars 23 forks source link

struggling with redirect condition #22

Closed davd33 closed 4 years ago

davd33 commented 4 years ago

Hello, first of all, thank you for this snooze lib :)

Then, I am struggling with http redirection. I have done as it is shown in the demo of the repo, but the method doesn't get called on a redirect condition signal.

Could please help me? Here si my code: https://github.com/davd33/colombia-tribal/pull/2/files#diff-c4a1309ae0a4d2355efe07e610116efb

Thank you

joaotavora commented 4 years ago

Hi, I don't have time to go through all that code, sorry.

But I can offer this suspicion: that you might have forgotten to define a primary method on snooze:explain-condition. By default. this particular generic function comes with none. So here's how to implement redirections using conditions (you don't have to implement them with conditions, but it's indeed reasonable to do so).

In this example, I'll assume you're using hunchentoot, and modify the headers using its API, because Snooze doesn't have (yet?) a server-independent way to affect outgoing headers:

So:

(ql:quickload :snooze)
(ql:quickload :clack)

(in-package :cl-user)

(define-condition redirect (snooze:http-condition)
  ((location :initarg :location :accessor location))
  (:default-initargs :status-code 303))

(snooze:defroute take-me-to-google (:get :*/*)
  (signal 'redirect :location "http://google.com"))

(defmethod snooze:explain-condition ((c redirect) any-resource any-content-type)
  (declare (ignore any-resource any-content-type))
  (setf (hunchentoot:header-out :location) (location c))
  (format nil "See here: ~a" (location c)))

(clack:clackup (snooze:make-clack-app) :port 9003)

Now go to http://localhost:9003/take-me-to-google

One thing that can help you debug is to do: (setq snooze:*catch-errors* :verbose) which will print lots of backtraces in your browser. Unfortunately, those backtraces are a little hard to decipher. I have to work on it.

davd33 commented 4 years ago

Hi @joaotavora, thank you it actually worked! :+1: