Permalink
Browse files

wrap all index paths

  • Loading branch information...
1 parent 0e35f12 commit c4ce71b6c634036c5e2bbeb0c59f36a362ff33b3 @rn-superg rn-superg committed Apr 26, 2012
Showing with 13 additions and 17 deletions.
  1. +13 −17 src/ring/middleware/index_file.clj
@@ -7,22 +7,18 @@
(str path-with-slash "?" (:query-string request))
path-with-slash))
-(defn wrap-index-path [handler path & opts]
- (let [path-with-slash (str path "/")
- opts (merge {:root "resources/public"
- :index "index.html"}
- (apply hash-map opts))]
- (fn [request]
- (let [uri (:uri request)
- index-path (str (:root opts) path-with-slash (:index opts))
- get-method? (= :get (:request-method request))]
- (cond
- (and get-method? (= path-with-slash uri))
- (ring.util.response/file-response index-path)
+(defn wrap-index-paths [handler doc-root]
+ (fn [request]
+ (let [get-method? (= :get (:request-method request))
+ uri (:uri request)
+ fpath (str doc-root uri "/index.html")
+ exists? (.exists (java.io.File. fpath))]
+ (cond
+ (and exists? (not (.endsWith uri "/")))
+ (ring.util.response/redirect (redirect-uri request (str uri "/")))
- ;; if the uri does not end in a slash, redirect so it does...
- (and get-method? (= path uri))
- (ring.util.response/redirect (redirect-uri request path-with-slash))
+ exists?
+ (ring.util.response/file-response fpath)
- :else
- (handler request))))))
+ :no-index-file-found
+ (handler request)))))

0 comments on commit c4ce71b

Please sign in to comment.