metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.43k stars 257 forks source link

Add context to support servlet based apps better #287

Open mitchelkuijpers opened 5 years ago

mitchelkuijpers commented 5 years ago

We are currently using reitit with ring-servlet. And our servlet is mounted under a context which we get in the ring request map it looks like this:

:servlet-context-path "/servlets"
:uri "/servlets/ring/api/company/1"

Now we would expect the http reitit router strips the "/servlets" part while routing. And when we do reverse based routing we would expect reitit to add the /servlets part. Maybe we can add a context option to reitit. One thing to note is that the option should probably not be static because you can sometimes access servlets from multiple context paths.

mitchelkuijpers commented 5 years ago

Btw we currently fix the routing by doing this:

(app (update request :uri #(str/replace-first % (:servlet-context-path request "") "")))
ouvanous commented 5 years ago

Hi, I had the same problem with Tomcat and with the help of @ikitommi i made this :

(defn handler [request]
  {:status 200, :body "ok"})

(defn wrap-servlet-adapt-uri [handler]
  (fn [request]
    ; remove from the :uri the :servlet-content-path to adapt to reitit routes
    (handler (update request :uri #(cstr/replace-first % (:servlet-context-path request "") "")))))

(def router
  (ring/router
   [["/" {:get handler}]
    ["/ping" {:get handler}]]))

(def app (ring/ring-handler 
           router
           nil
           ; apply middleware before routing
           {:middleware [wrap-servlet-adapt-uri]}))

And it now work. Thanks