Fork me on GitHub
#ring-swagger
<
2017-06-01
>
mhautala07:06:35

I have a problem with not-found route using compojure-api and middleware to wrap authentication. The authentication wrapper “eats” all requests and the not-found route is never matched.

(require '[compojure.api.sweet :as api])

(def handler
  (api/routes
    (api/middleware [auth/wrap-api-authentication]
                    protected-api-routes)
    (route/not-found "Not Found")))
This post describes similar problem with plain compojure that I used to solve with wrap-routes function http://jakemccrary.com/blog/2014/12/21/restricting-access-to-certain-routes/ Any ideas?

juhoteperi07:06:41

@mhautala wrap-middleware which workslike wrap-routes is available in 2.0 alpha

mhautala07:06:54

ok, good to know, so this is a know thing with the middleware function?

mhautala07:06:10

Ok, now I noticed that is exactly what it says in the middleware functions documentation

Note that middlewares will be executed even if routes in body
do not match the request uri. Be careful with middlewares that
have side-effects.

ikitommi13:06:48

@mhautala good solution is to apply authentication for a explicit context. e.g.

(context "/admin-routes" []
  :middleware [auth-wrap-api-authentication]
  ...)
then it only applies for those routes. Can be applied to endpoints & api-level too (with 2.0.0).

ikitommi13:06:36

(also, there will be more tools to compose things with 2.0.0.)

mhautala14:06:51

Ok that’s a good solution, I found already that approach for endpoint level but at context or api-level you can avoid repeating code

michaelblume21:06:42

I’m getting a persistent eastwood warn with compojure-api

michaelblume21:06:48

(ns test-eastwood.core
  (:require [compojure.api.sweet :refer :all]
            [schema.core :as schema]))

(def v1-routes
  (routes
    (POST "/foo" []
          :return {:bar schema/Int}
          {:status 200 :body {:bar 35}})))

michaelblume21:06:08

== Linting test-eastwood.core ==
Entering directory `/Users/michael.blume/workspace/test-eastwood'
src/test_eastwood/core.clj:1:1: suspicious-expression: merge called with 1 args.  (merge map) always returns map.  Perhaps there are misplaced parentheses?

michaelblume21:06:07

both with 1.1.10 and 2.0.0-alpha1