Ring / Compojure在子文件夹中提供index.html?

时间:2014-08-17 20:20:23

标签: clojure compojure ring

我的ressources/public目录包含许多子文件夹,每个子文件夹都包含一个index.html。如何设置路线以便他们观察任何GET" ... /"递归路径并返回index.html而不在URL中显示文件?

一种方法是明确设置每条路线,但我希望不需要定义每条路径。

(GET  "/" [] (resource-response "index.html" {:root "public"}))
(GET  "/foo" [] (resource-response "foo/index.html" {:root "public"}))
...

2 个答案:

答案 0 :(得分:3)

ring wrap-resources中间件的一个小修改就可以解决问题:

(defn wrap-serve-index-file
  [handler root-path]
  (fn [request]
    (if-not (= :get (:request-method request))
      (handler request)
      (let [path (.substring (codec/url-decode (:uri request)) 1)
            final-path (if (= \/ (or (last path) \/))
                           (str path "index.html")
                           path)]
        (or (response/resource-response path {:root root-path})
            (handler request))))))

答案 1 :(得分:1)

你可以通过像这样提供一个解构向量来轻松地做到这一点:

(GET "/:subpath" [subpath] (resource-response (str subpath "/index.html") {:root "public"}))

请点击此处了解更多详情: https://github.com/weavejester/compojure/wiki/Destructuring-Syntax