环形/Compojure如何在子文件夹中提供 index.html?

3

我的 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个回答

4

对于 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接