4

我正在使用 clojure 和 leiningen 构建一个小型 Web 应用程序。我有某些需要访问的 json 文件,我还使用在我的服务器上运行的批处理进程每晚更新这些文件。我在本地使用 leiningen,但想将 uberjar 部署到服务器。有一种方法可以让我更新压缩在 jar 文件中的 json 文件,或者访问 uberjar 之外的 json 文件。现在我正在尝试在组合路由中使用 ring.util.response/resource-response 来做后者:

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

当我的应用程序尝试访问文件时,我收到 404 错误。有谁知道可能的解决方案?

4

3 回答 3

3

JVM 不会扩展~in 路径,使用调用来System/getenv获取主目录并构建路径。

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

Tomcat 通常以自己的用户身份运行,因此请确保将其放在正确的主目录中或完整地拼出路径。可能还需要配置 tomcat 以访问该目录。

于 2013-12-12T01:02:00.837 回答
0
(route/files "/upload/" {:root "/path_to_your_folder/"})

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

于 2014-08-22T07:40:24.423 回答
0

resp/resource-response用于提供与 jar 一起打包的资源,因此它并不真正适合提供需要与应用程序分开更新的文件。

对于您的情况,我认为 Ring'sresp/file-response更合适。它允许从文件系统中的指定位置提供文件,这允许将 json 文件与应用程序分开。

像这样的东西:

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

正如 Sean 所建议的,确切的文件夹名称可能应该来自配置或系统环境。

于 2013-12-30T17:25:30.733 回答