简体   繁体   English

使用leiningen访问clojure中项目目录外的JSON文件

[英]Access to JSON files outside project directory in clojure using leiningen

I'm building a small web app using clojure with leiningen. 我正在使用与leiningen的clojure构建一个小型Web应用程序。 I have certain json files that I need to access that I also update nightly using a batch process running on my server. 我有一些我需要访问的json文件,我每晚都使用在我的服务器上运行的批处理进行更新。 I am using leiningen locally, but want to deploy an uberjar to the server. 我在本地使用leiningen,但是想在服务器上部署一个uberjar。 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. 在那里我可以更新jar文件中压缩的json文件,或者访问uberjar之外的json文件。 Right now I am trying to do the latter using ring.util.response/resource-response in a compojure route: 现在我试图在compojure路由中使用ring.util.response / resource-response来执行后者:

      (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. 当我的应用尝试访问文件时,我收到404错误。 Does anyone know of a possible solution? 有谁知道可能的解决方案?

The JVM does not expand the ~ in paths, use a call to System/getenv to get the home directory and build the path. JVM不会扩展~ in路径,使用对System/getenv的调用来获取主目录并构建路径。

{: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. Tomcat通常以其自己的用户身份运行,因此请确保将其放入正确的主目录或完全拼出路径。 It may also be necessary to configure tomcat to have access to that dir. 可能还需要配置tomcat以访问该目录。

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. resp/resource-response用于提供与jar一起打包的资源,因此它不适合提供需要与应用程序分开更新的文件。

For your case I think Ring's resp/file-response is more suitable. 对于你的情况,我认为Ring的resp/file-response更合适。 It allows serving files from a specified location in the filesystem, which allows keeping the json files separated from the app. 它允许从文件系统中的指定位置提供文件,这允许将json文件与应用程序分开。

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. 正如Sean建议的那样,确切的文件夹名称可能来自配置或系统环境。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM