40e2b5d
website.git ยท Tickets
website.git > main > 40e2b5d > files / src / dev / grahp / website
Normalize URIs into paths
Commit: 40e2b5d9317476e52dada8633bf23837bcb10f9a
Parent: c78783f
3 changed files:
src/dev/grahp/website.clj view file
@@ -0,0 +1,15 @@+(ns dev.grahp.website+ (:require+ [clojure.spec.alpha :as s]+ [dev.grahp.website.path :as path]))++(defmacro spec+ ([k s] `(s/def ~k ~s))+ ([k doc s] `(s/def ~k (vary-meta (s/spec ~s)+ assoc :doc ~doc))))++(defmacro schema+ ([keys] `(s/keys :opt [~@keys])))++(spec ::path path/path?)+(spec ::page (schema [::path]))src/dev/grahp/website/path.clj view file
@@ -0,0 +1,25 @@+(ns dev.grahp.website.path+ (:require+ [clojure.string :as str]+ [dev.grahp.website :as-alias w]))++(defn path?+ "A vector of URL segments. I.e. '/foo/bar.png' is [\"foo\" \"bar.png\"]."+ [x]+ (and (vector? x)+ (every? string? x)+ (not-any? #(str/includes? % "/") x)))++(defn uri->path [uri]+ (if (nil? uri)+ []+ (into [] (remove str/blank?) (str/split uri #"/"))))++(defn path->uri [path]+ (str "/" (str/join "/" path)))++(defn wrap-path+ "Splits uris into a vector of path strings with no slashes at the :path key."+ [app]+ (fn [{:keys [uri] :as request}]+ (app (assoc request ::w/path (uri->path uri)))))src/dev/grahp/website/server.clj view file
@@ -1,5 +1,6 @@ (ns dev.grahp.website.server (:require+ [dev.grahp.website.path :as path] [ring.adapter.jetty :refer [run-jetty]] [ring.middleware.file :refer [wrap-file]] [ring.middleware.resource :refer [wrap-resource]]))@@ -13,8 +14,9 @@ (defn main [{}] (run-jetty- (-> #'handler- (wrap-file "/srv/website/files" {:index-files? false})- (wrap-resource "files"))- {:port 8000- :host "127.0.0.1"}))+ (-> #'handler+ (wrap-file "/srv/website/files" {:index-files? false})+ (wrap-resource "files")+ (path/wrap-path))+ {:port 8000+ :host "127.0.0.1"}))