metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.11k stars 149 forks source link

Compile to non-linear routing strategies #467

Open frenchy64 opened 3 months ago

frenchy64 commented 3 months ago

These will probably have to be different than reitit's since route conflicts are idiomatic. We'll probably want more types of routing between trie and linear.

I think routing based on HTTP verb is a good start;

e.g.,

(context "/foo" []
  (GET "/bar" [] (ok 1))
  (POST "/bar" [] (ok 1))
  (PUT "/bar" [] (ok 1))
  (PATCH "/bar" [] (ok 1)))
=>
(context "/foo" req
  (case (get-http-verb req)
    :GET (GET "/bar" [] (ok 1))
    :POST (POST "/bar" [] (ok 1))
    :PUT (PUT "/bar" [] (ok 1))
    :PATCH (PATCH "/bar" [] (ok 1))))

This could probably be achieved at runtime by reifying some of the structure of a route macro at runtime.

Maybe a static context can communicate to its endpoints to return a data representation of themselves instead of expanding to compojure and the static context can compile a more efficient router.

This could be accomplished more dynamically by having context bind a dynamic variable and expanding endpoints to code that checks for this variable to decide whether to return a data representation or a compojure route.

This has the advantage over static transformation under a context of working even if the endpoint is not directly under the context (say, separated by a function call).