Compojure routing of www and api content

159 views
Skip to first unread message

Jonathon McKitrick

unread,
Aug 7, 2014, 8:17:15 AM8/7/14
to clo...@googlegroups.com
I'm serving up some html and js content, and using handler/site for that.  I have a separate handler/api group of routes under the "/api" context.

If I include the api routes before the site routes, the site works fine.  If the www routes come first, the api calls fail, probably because the (route/resources "/") at the end of the site routes catches that call and returns null.

OTOH, if I try to use friend with the api routes, it breaks the friend wrapping of the www calls.

(defroutes api-routes
  (context "/api" []
            .....
           (route/not-found "ERROR")))

(defroutes www-routes
  (GET "/admin" req (friend/authorize #{::admin} "Admin only"))
  (GET "/authorized" req (friend/authorize #{::user} "Users only"))
  (GET "/home" [] (response/file-response "home.html" {:root "resources/public"}))
  (GET "/login" [] (response/file-response "login.html" {:root "resources/public"}))
  (friend/logout (ANY "/logout" req (response/redirect "/")))
  (GET "/" [] (response/redirect "index.html"))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (routes
   (-> www-routes
       (friend/authenticate {;:allow-anon? true
                             ;;:login-uri "/login.html"
                             ;:default-landing-uri "/"
                             ;:redirect-on-auth? "/home"
                             ;:unauthorized-handler #(response/status (response/response "NO") 401)
                             ;:login-failure-handler #(response/response "OOPS")
                             :credential-fn (partial creds/bcrypt-credential-fn users)
                             :workflows [(workflows/interactive-form)]})
       ;;(wrap-resource "public")
       ;wrap-content-type
       ;wrap-not-modified
       ;;wrap-reload
       handler/site)
   (-> api-routes
       handler/api
       ;;wrap-reload
       wrap-restful-format)))

James Reeves

unread,
Aug 7, 2014, 2:55:33 PM8/7/14
to clo...@googlegroups.com
Compojure routes are checked in order until one matches. You've set up your www-routes to match all possible routes, as you have a "catch-all" not-found route at the bottom.

- James


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathon McKitrick

unread,
Aug 7, 2014, 3:14:11 PM8/7/14
to clo...@googlegroups.com
Right, I tried removing that as well, and Friend still fails, and the api routes fail as well.


--
Jonathon McKitrick


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/A-qRAftd6XY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Jonathon McKitrick

unread,
Aug 7, 2014, 3:27:01 PM8/7/14
to clo...@googlegroups.com
Let me clarify.  I removed the 'not-found' route and the api calls all return an empty response.


--
Jonathon McKitrick

Gary Verhaegen

unread,
Aug 8, 2014, 11:22:07 AM8/8/14
to clo...@googlegroups.com
Maybe post your (minimal) complete code on a github project or something, to make it easier for us to reproduce and diagnose?
Reply all
Reply to author
Forward
0 new messages