How to determine the correct path to the file system

I am working on an application that should download some external resources and make them accessible through a directory public/staticin Ring.

But .. I have a problem with saving resources in a static directory in my application, during development I use ring-jetty-adapter; test & production servers are running Tomcat.

I added :web-content "public"leiningen to my project and added the directory publicto the project root directory, then I have a download function using http-agent and duck-streams:

(defn download
  [file-name url]
  (h/http-agent url
                :handler (fn [agnt]
                           (let [fname file-name]
                             (with-open [w (d/writer fname)]
                               (d/copy (h/stream agnt) w))))))

If I download Jetty from REPL and use savepath: "public/my.file", the downloaded file is correctly placed in the directory public. But when I deploy it using a file .warin Tomcat, it searches for the directory publicin the root directory of Tomcat, and not in the path to the application context.

I tried to add a middleware shell to determine the path to the context and from there build the correct save path, but I can not find any way to access HttpServletor a way to determine whether the application is running in the adapter or it is deployed in a specific context.

Here is a wrapper:

(defn wrap-context-info [handler]
  (fn [req]
    (let [resp (handler req)]
      (assoc resp :servlet (:servlet req) :req (:servlet-request  req)))))

:servletand :reqare nil.

+4
source share
1 answer

, , HttpServlet, HttpServletRequest HttpServletResponse :servlet, :servlet-request :servlet-response, .

:servlet-context (.getServletContext servlet) .

, .

+1

Source: https://habr.com/ru/post/1777042/


All Articles