Reputation: 1819
My ressources/public
directory contain many subfolders and each one contain an index.html. How do I set the routes so that they watch for any GET ".../" path recursively and return the index.html without showing the file in the URL?
One way to do it is to explicitly set each route like below but I'm hoping to not need to define each path.
(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/foo" [] (resource-response "foo/index.html" {:root "public"}))
...
Upvotes: 3
Views: 559
Reputation: 1455
You can do this easily by providing a destructuring vector to compojure like so:
(GET "/:subpath" [subpath] (resource-response (str subpath "/index.html") {:root "public"}))
See here for more details: https://github.com/weavejester/compojure/wiki/Destructuring-Syntax
Upvotes: 1
Reputation: 8593
A small modification to ring wrap-resources middleware will do the trick:
(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))))))
Upvotes: 4