juxt / bidi

Bidirectional URI routing
MIT License
993 stars 90 forks source link

filesmaybe missing #209

Open awb99 opened 3 years ago

awb99 commented 3 years ago

filesmaybe is useful as well. I modelled it after resourcesMaybe

(defrecord FilesMaybe [options]
  bidi/Matched
  (resolve-handler [this m]
    (let [reminder (url-decode (:remainder m))
          filename (str (:dir options) reminder)]
      ;(warn "file-maybe: " filename)
      (when (.exists (io/file filename))
        ;(warn "file found: " filename)
        (assoc (dissoc m :remainder)
               :handler (->
                         (fn [req] (file-response reminder
                                                  {:root (:dir options)}))
                         (wrap-content-type options)
                         (wrap-not-modified))))))
  (unresolve-handler [this m]
    (when (= this (:handler m)) "")))