swagger-ui path work, wip

This commit is contained in:
Tommi Reiman 2018-06-04 11:33:51 +03:00
parent 7a544cd52d
commit fa078470c5
2 changed files with 17 additions and 15 deletions

View file

@ -96,24 +96,26 @@
(fn (fn
([request] (handler request)) ([request] (handler request))
([request respond _] (respond (handler request))))) ([request respond _] (respond (handler request)))))
trim-path (fn [& paths] (str/replace (apply str paths) "//" "/"))
resource-response (fn [path] resource-response (fn [path]
(if-let [response (or (paths path) (response/resource-response path options))] (if-let [response (or (paths (trim-path "/" path)) (response/resource-response path options))]
(response/content-type response (mime-type/ext-mime-type path)))) (response/content-type response (mime-type/ext-mime-type path))))
path-or-index-response (fn [path] path-or-index-response (fn [path uri]
(or (resource-response path) (or (resource-response path)
(let [separator (if-not (str/ends-with? path "/") "/")] (loop [[file & files] index-files]
(loop [[file & files] index-files] (if file
(if file (let [path (trim-path path file)]
(or (resource-response (str path separator file)) (if (resource-response path)
(recur files))))))) {:status 302 :headers {"Location" (trim-path uri "/" path)}}
(recur files)))))))
handler (if path handler (if path
(fn [request] (fn [request]
(let [uri (:uri request)] (let [uri (:uri request)]
(if-let [path (if (>= (count uri) path-size) (subs uri path-size))] (if-let [path (if (>= (count uri) path-size) (subs uri path-size))]
(path-or-index-response path)))) (path-or-index-response path uri))))
(fn [request] (fn [request]
(let [path (-> request :path-params parameter)] (let [path (-> request :path-params parameter)]
(or (path-or-index-response path) {:status 404}))))] (or (path-or-index-response path path) {:status 404}))))]
(create handler))))) (create handler)))))
(defn ring-handler (defn ring-handler

View file

@ -42,11 +42,11 @@
(update $ :root (fnil identity "swagger-ui")) (update $ :root (fnil identity "swagger-ui"))
(update $ :url (fnil identity "/swagger.json")) (update $ :url (fnil identity "/swagger.json"))
(update $ :config #(->> % (map mixed-case-key) (into {}))) (update $ :config #(->> % (map mixed-case-key) (into {})))
(assoc $ :paths {"conf.js" {:headers {"Content-Type" "application/javascript"} (assoc $ :paths {"/conf.js" {:headers {"Content-Type" "application/javascript"}
:status 200 :status 200
:body (conf-js $)} :body (conf-js $)}
"config.json" {:headers {"Content-Type" "application/json"} "/config.json" {:headers {"Content-Type" "application/json"}
:status 200 :status 200
:body (config-json $)}}))] :body (config-json $)}}))]
(ring/routes (ring/routes
(ring/create-resource-handler options)))))) (ring/create-resource-handler options))))))