From 9e8abbf26d4422ba133bba7186363bee5b1f38d7 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Wed, 5 Oct 2022 22:29:03 +0300 Subject: 1.3.0 --- src/ruuter/core.cljc | 99 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/ruuter/core.cljc b/src/ruuter/core.cljc index 444788f..6eaa912 100644 --- a/src/ruuter/core.cljc +++ b/src/ruuter/core.cljc @@ -1,57 +1,74 @@ (ns ruuter.core - (:require [clojure.string :as string]) + (:require + [clojure.string :as string]) #?(:clj (:gen-class))) - (defn- path->regex-path "Takes in a raw route `path` and turns it into a regex pattern to match against the request URI." [path] - (if (= "/" path) - "\\/" - (->> (string/split path #"/") - (map #(cond - ; matches anything, and must be present - ; for example `:name` - (and (string/starts-with? % ":") - (not (string/ends-with? % "?"))) - ".*" - ; matches anything, but is optional - ; for example `:name?` - (and (string/starts-with? % ":") - (string/ends-with? % "?")) - "?.*?" - :else - ; what comes around, goes around - %)) - (string/join "\\/")))) + (cond (= "/" path) + "\\/" + + (re-find #"\*" path) + (-> (string/replace path #"\:.*?\*" ".*?") + (string/replace #"/" "\\/")) + :else + (->> (string/split path #"/") + (map #(cond + ; matches anything, and must be present + ; for example `:name` + (and (string/starts-with? % ":") + (not (string/ends-with? % "?"))) + ".*" + ; matches anything, but is optional + ; for example `:name?` + (and (string/starts-with? % ":") + (string/ends-with? % "?")) + "?.*?" + :else + ; what comes around, goes around + %)) + (string/join "\\/")))) (defn- path+uri->path-params "Takes a raw route `path` and the actual request `uri`, which it then turns into a map of k:v, if any parameters were used in the `path`." [path uri] - (if (= "/" path) - {} - (let [split-path (string/split path #"/") - split-uri (string/split uri #"/")] - (into {} (map-indexed - (fn [idx item] - (cond - ; required parameter - (and (string/starts-with? item ":") - (not (string/ends-with? item "?"))) - {(keyword (subs item 1)) (get split-uri idx)} - ; optional parameter - (and (string/starts-with? item ":") - (string/ends-with? item "?") - (get split-uri idx)) - {(keyword (-> item - (subs 0 (- (count item) 1)) - (subs 1))) - (get split-uri idx)})) - split-path))))) + (cond (= "/" path) + {} + + (re-find #"\*" path) + (let [index-of-k-start (string/index-of path ":") + k (-> (subs path (+ 1 index-of-k-start)) + keyword) + v (subs uri (+ 1 index-of-k-start))] + {k v}) + :else + (let [split-path (->> (string/split path #"/") + (remove empty?) + vec) + split-uri (->> (string/split uri #"/") + (remove empty?) + vec)] + (into {} (map-indexed + (fn [idx item] + (cond + ; required parameter + (and (string/starts-with? item ":") + (not (string/ends-with? item "?"))) + {(keyword (subs item 1)) (get split-uri idx)} + ; optional parameter + (and (string/starts-with? item ":") + (string/ends-with? item "?") + (get split-uri idx)) + {(keyword (-> item + (subs 0 (- (count item) 1)) + (subs 1))) + (get split-uri idx)})) + split-path))))) (defn- match-route "For a collection of `route`, will attempt to find one that matches @@ -67,7 +84,6 @@ (when route (dissoc route :regex-path)))) - (defn- route+req->response "Given the current route and the current HTTP request, it will attempt to return a response, either directly if it's a map or @@ -96,7 +112,6 @@ {:status 404 :body "Not found."})) - (defn route "For a given collection of `routes` and the current HTTP request as `req`, will attempt to match a route with the HTTP request, which it -- cgit v1.2.3