joaotavora / snooze

Common Lisp RESTful web development
207 stars 22 forks source link

How to redirect #6

Closed joaotavora closed 6 years ago

joaotavora commented 6 years ago

@vindarel asks in #5:

Another easy question: how would one do a redirection ? Maybe with a funcall or something. Didn't find, skimmed at the readme and api. If that's Clack's job I didn't find it in its doc either (very scarce). A redirect example would be useful.

It's a question of signalling a 303 with an appropriate Location: header value. I setup a helper function called redirect-to or something like that. This one is for hunchentoot but i suppose it's easy to write one for clack or anything else you use...

(defun redirect-to (url &optional
                          (format-control "Redirected")
                          format-args)
  (setf (hunchentoot:header-out :location) url)
  (snooze:http-condition 303
                         (format nil "~?" format-control format-args)))

and then in some route, say authenticate

(snooze:defroute authenticate (:post "*/*")
  (cond ((know-this-guy)
         (setup-some-session-info)
         (redirect-to "/"))
        (t
         (snooze:http-condition 403 "Unauthorized"))))
vindarel commented 6 years ago

Thanks, that works fine. Will you add it in snooze ?

joaotavora commented 6 years ago

Will you add it in snooze ?

No because the particular details of redirection are the web server's responsibility, and snooze is an URL router. This one works for hunchentoot but will possibly fail for clack or wookie or whatever.

Of course I won't mind if a separate project, say snooze-utils is created that addresses this and creates multiple versions of these utils (one for each server) and dispatches appropriately given the web server in use.

vindarel commented 6 years ago

makes sense, but a bit of a shame, since a redirection mechanism is always used alongside routing.

This one works for hunchentoot but will possibly fail for clack or wookie or whatever.

Clack is supposed to be server agnostic, it uses Hunchentoot by default and can be told to use Woo or others. So requireing Clack and building the function on it would be an all-in-one solution. Can't judge more though, will have to try.

ps:  I'll add some information in the wiki.