Access to JSON files outside project directory in clojure using leiningen

484 views Asked by At

I'm building a small web app using clojure with leiningen. I have certain json files that I need to access that I also update nightly using a batch process running on my server. I am using leiningen locally, but want to deploy an uberjar to the server. in there a way for me to either update json files compressed inside the jar file, or access json files that are outside the uberjar. Right now I am trying to do the latter using ring.util.response/resource-response in a compojure route:

      (GET "/json/:filename" [filename] 
        (resp/resource-response 
          (str filename ".json") 
          {:root "~/internal_dashboard/app/json/"}))

When my app attempts to access the files I get a 404 error. Does anyone know of a possible solution?

3

There are 3 answers

0
Arthur Ulfeldt On

The JVM does not expand the ~ in paths, use a call to System/getenv to get the home directory and build the path.

{:root (str (System/getenv "HOME") "/internal_dashboard/app/json/")}

Tomcat often runs as it's own user so make sure you either put it in the correct home dir or spell out the path completely. It may also be necessary to configure tomcat to have access to that dir.

0
mlni On

The resp/resource-response is meant for serving resources packaged together with the jar, so it's not really suited for serving files that you need to update separately from the application.

For your case I think Ring's resp/file-response is more suitable. It allows serving files from a specified location in the filesystem, which allows keeping the json files separated from the app.

Something like this:

(GET "/json/:filename" [filename] (resp/file-response
                                    (str filename ".json")
                                    {:root "/some/folder/"}))

The exact folder name should probably come from configuration or the system environment, as Sean suggested.

0
Dapeng On
(route/files "/upload/" {:root "/path_to_your_folder/"})

http://weavejester.github.io/compojure/compojure.route.html#var-files