Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ring/Compojure serving index.html in subfolders?

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"}))
...
like image 353
Paul Lam Avatar asked May 15 '26 23:05

Paul Lam


1 Answers

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))))))
like image 116
DanLebrero Avatar answered May 19 '26 02:05

DanLebrero