Coder Social home page Coder Social logo

Comments (11)

ikitommi avatar ikitommi commented on August 29, 2024

ugh. we'll check this out.

from compojure-api.

Deraen avatar Deraen commented on August 29, 2024

Ah yes. Middleware is executed before the handler checks if the route is correct.

  1. Compojure executes GET* "/first" handler, it returns nil
  2. Compojure executes GET* "/second" handler, it should return nil (but instead the middleware is executed first)
  3. Should executes GET "/third" handler...

I merged the tests and I'll have a fix shortly.

from compojure-api.

Deraen avatar Deraen commented on August 29, 2024

... And the fix is probably to remove :middlewares because it's impossible to implement this properly.

To apply middlewares after route is checked but before the handler is called would require changes in compojure.core generated code: https://github.com/weavejester/compojure/blob/1.1.8/src/compojure/core.clj#L99.

from compojure-api.

phadej avatar phadej commented on August 29, 2024

Couldn't middlewares be applied on the route body:

(GET* "/foo" []
  :middlewares [mw]
  (ok "foo"))

;;; =>
(GET "/foo" request
  (mw request))

or do I understand something wrong? (I though that middlewares is desugared into something like that)

from compojure-api.

Deraen avatar Deraen commented on August 29, 2024

Middlewares don't take a request as param but a handler:

(defn mw [handler]
  (fn [req]
    (let [req (update-in req [:x] ...)
          res (handler req)]
      res)))

(GET* "/foo" req
  :body-params [x :- String]
  :middlewares [mw]
  (ok x)

=>
(GET "/foo" req
  ;; mock-handler
  ((mw
    (fn [_] (ok x)))
   req)   

And even while it's possible to create "a mock handler": (fn [_] ~@body) the edits to request won't be seen by coercers etc.

But if we forget the middleware semantics and create e.g. "routewares" we could implement what you are suggesting:

;; Can't edit the req
;; can call the route body conditionally
(defn rw [req next]
  (if (... req)
    (ok {:value "bar"})
    (next)))

(GET* "/foo" []
  :routewares [rw]
  (ok "foo"))

from compojure-api.

phadej avatar phadej commented on August 29, 2024

routewares are fine to me.

I found the bug while adding authentication middleware for just few selected routes (e.g. content modifying ones). Not sure what is the right way to do that kind of functionality.

EDIT though I want to do authentication check before parsing the request...

from compojure-api.

Deraen avatar Deraen commented on August 29, 2024

I think I found a fix :)

from compojure-api.

yogsototh avatar yogsototh commented on August 29, 2024

I tried with the latest version it doesn't seem to work.

(defroute* my-routes
  (GET* "/route1" []
    :middle wares [admin-only]
    ... )

  (GET* "/route2" []
    :middle wares [user-only]
    ... ))

route2 isn't accessible for users :-(

from compojure-api.

Deraen avatar Deraen commented on August 29, 2024

I haven't yet merged the fix to master branch.

from compojure-api.

yogsototh avatar yogsototh commented on August 29, 2024

Ah thanks! I'll try with the right version.

from compojure-api.

yogsototh avatar yogsototh commented on August 29, 2024

It works perfectly, thanks!

from compojure-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.