Search code examples
clojureringcompojure

Compojure with configurable base route


I am working with Clojure, Ring and Compojure for some time now, but I am still fairly new.

What I need to know is how to make a configurable base route, for example:

/:base-route

/:base-route/user

/:base-route/settings

/:base-route can be different (api, company, stuff...) and will depend on an internal check. What I can't do is create a universal check for all routes to see if /:base-route has the appropriate value. For example /:base-route is configured to be /api, but the user tries /company/user -> the response must be 404.


Solution

  • Ok, so the way to do it is with a regular expression and a context:

    (defroutes routes
      (context ["/:base-route" :base-route (re-pattern base-route)] [base-route]
                 (GET  "/user" [] (str "base: " base-route " user"))
                 (GET  "/settings" [] (str "base: " base-route " settings"))))