janet-lang / spork

Various Janet utility modules - the official "Contrib" library.
MIT License
123 stars 35 forks source link

httpf middleware? #201

Open neezer opened 1 week ago

neezer commented 1 week ago

How could I add something like middleware to my httpf app? I'd like to set the session cookie on every request, but I haven't yet had an epiphany on how I might accomplish this... is there a commonly-understood pattern I could use here?

neezer commented 1 week ago

I came up with the following macro, but not sure if there's a better way?

(defmacro route [meta handler]
  ~(defn ,(gensym) ,meta [req data]
    (set-session-cookie req)
    (apply ,handler [req data])))

Used like:

(route
  {:path "/" :doc "homepage"}
  (fn [&]
    @[[:h1 "hello world"]]))
sogaiu commented 1 week ago

Possibly not much of a useful answer here, but FWIW, searching through various repositories, some of the results that turned up included:

All of the contained uses of spork/httpf seemed fairly simple and not along the lines of middleware AFAICT.

So:

is there a commonly-understood pattern I could use here

I suspect an answer to this might be "no". May be you are pioneering (^^;

It could be that the authors of those projects might have some ideas though?