Add option to allow serving index files without redirect

Signed-off-by: Loukas Agorgianitis <loukas@agorgianitis.com>
This commit is contained in:
Loukas Agorgianitis 2025-03-21 11:19:35 +02:00
parent 58195eed68
commit f50feff63c
No known key found for this signature in database
GPG key ID: DDC6FA7D5BB332E6
3 changed files with 41 additions and 8 deletions

View file

@ -61,6 +61,7 @@ This way, they are only served if none of the actual routes have matched.
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)

View file

@ -227,18 +227,20 @@
;; TODO: ring.middleware.head/wrap-head
;; TODO: handle etags
(defn -create-file-or-resource-handler
[response-fn {:keys [parameter root path loader allow-symlinks? index-files paths not-found-handler]
[response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? paths not-found-handler]
:or {parameter (keyword "")
root "public"
index-files ["index.html"]
paths (constantly nil)
not-found-handler (if path
(constantly nil)
(constantly {:status 404, :body "", :headers {}}))}}]
index-redirect? true
paths (constantly nil)}}]
(let [options {:root root
:loader loader
:index-files? false
:allow-symlinks? allow-symlinks?}
not-found-handler (or not-found-handler
(if path
(constantly nil)
(constantly {:status 404, :body "", :headers {}})))
path-size (count path)
create (fn [handler]
(fn
@ -254,8 +256,10 @@
(or (response path)
(loop [[file & files] index-files]
(if file
(if (response (join-paths path file))
(response/redirect (join-paths uri file))
(if-let [resp (response (join-paths path file))]
(if index-redirect?
(response/redirect (join-paths uri file))
resp)
(recur files))))))
handler (if path
(fn [request]
@ -281,6 +285,7 @@
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
([]
(create-resource-handler nil))
@ -298,6 +303,7 @@
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
([]
(create-file-handler nil))

View file

@ -671,7 +671,33 @@
(app (request "/hello.xml") respond raise)
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
(is (get-in @result [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result))))))))))))))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result)))))))))
(testing "with index-redirect"
(let [app (ring/ring-handler
(ring/router
["/*" (create {:index-redirect? true})])
(ring/create-default-handler))]
(testing "index-files"
(let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response)))
(let [response (app (request "/docs/"))]
(is (= (redirect "/docs/index.html") response))))))
(testing "without index-redirect"
(let [app (ring/ring-handler
(ring/router
["/*" (create {:index-redirect? false})])
(ring/create-default-handler))]
(testing "index-files"
(let [response (app (request "/docs"))]
(is (= 200 (:status response)))
(is (= "<h1>hello</h1>\n" (slurp (:body response)))))
(let [response (app (request "/docs/"))]
(is (= 200 (:status response)))
(is (= "<h1>hello</h1>\n" (slurp (:body response)))))))))))))
#?(:clj
(deftest file-resource-handler-not-found-test