ggenikus / comcomp

'Comcomp' helps integrate compojure routes with Stuart Sierra's 'Component' library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using defroutes with a gensym is dangerous

Kungi opened this issue · comments

First thanks for solving my problem with comcomp.

While using it I found the following problem. You are using a gensym in conjunction with defroutes
in the defroutes-with-deps macro.

This generates the same gensym every time you call it and therefore overwrites the same var every
time.

Have a look at the following example:

(defmacro not-so-safe [value] `(def name# ~value))

user=> (not-so-safe 5)
#'user/name__2908__auto__
user=> (not-so-safe 6)
#'user/name__2908__auto__

By keeping the routes in the record you can solve this problem. You can write your macro like this:

(defmacro defroutes-with-deps[rec-name routes-name deps & routes]
  `(defrecord ~rec-name [~@deps]
     component/Lifecycle
     (start [this#]
       (let [keys# (map keyword '~deps)
             dep-map# (zipmap keys# ~deps)

             routes# (compojure/routes ~@(with-dependency routes deps))]

         (assoc this# :routes (make-handler routes# dep-map#))))
     (stop [this#] this#)

     IRoutesDescriber
     (get-routes [this#] (:routes this#))))

And last but not least, you got a typo in IRotesDescriber.

Hi there,
I have used the code from ggenikus and the fix from Kungu. I have added the working example with http-kit too. You can find it here.