From 6a2a8a7fe7575b666d0a54c4903528f0ea54e651 Mon Sep 17 00:00:00 2001 From: Liam Chen Date: Mon, 2 May 2022 16:28:39 -0400 Subject: [PATCH] pass the path through to the context-request function This approach is simpler, pass the path down, rather than attaching it to the meta. --- src/compojure/core.clj | 48 ++++++++++++++---------------------- test/compojure/core_test.clj | 4 +-- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/compojure/core.clj b/src/compojure/core.clj index 3c8e871..304cfb8 100644 --- a/src/compojure/core.clj +++ b/src/compojure/core.clj @@ -134,12 +134,12 @@ (defn- wrap-route-info [handler route-info] (fn ([request] - (let [full-route (str (:compojure/context request) (second route-info))] + (let [full-route (str (:compojure/context-path request) (second route-info))] (handler (assoc request :compojure/route route-info :compojure/full-route full-route)))) ([request respond raise] - (let [full-route (str (:compojure/context request) (second route-info))] + (let [full-route (str (:compojure/context-path request) (second route-info))] (handler (assoc request :compojure/route route-info :compojure/full-route full-route) @@ -259,47 +259,34 @@ (defn- remove-suffix [path suffix] (subs path 0 (- (count path) (count suffix)))) -(defn- context-request [request route] +(defn- context-request [request route context-path] (if-let [params (clout/route-matches route request)] (let [uri (:uri request) - path (:path-info request uri) - context (or (:context request) "") subpath (:__path-info params) - params (dissoc params :__path-info) - context-route (:context-route (meta route))] + params (dissoc params :__path-info)] (-> request (assoc-route-params (decode-route-params params)) (assoc :path-info (if (= subpath "") "/" subpath) :context (remove-suffix uri subpath)) - (update :compojure/context (fn [ctx] (str ctx context-route))))))) + (update :compojure/context-path (fn [ctx] (str ctx context-path))))))) (defn- context-route [route] (let [re-context {:__path-info #"|/.*"}] (cond (string? route) - (with-meta (clout/route-compile (str route ":__path-info") re-context) - {:context-route route}) + (clout/route-compile (str route ":__path-info") re-context) (and (vector? route) (literal? route)) - (with-meta (clout/route-compile - (str (first route) ":__path-info") - (merge (apply hash-map (rest route)) re-context)) - {:context-route (first route)}) + (clout/route-compile + (str (first route) ":__path-info") + (merge (apply hash-map (rest route)) re-context)) (vector? route) - `(let [route# ~(first route)] - (with-meta - (clout/route-compile - (str route# ":__path-info") - ~(merge (apply hash-map (rest route)) re-context)) - {:context-route route#})) + `(clout/route-compile + (str ~(first route) ":__path-info") + ~(merge (apply hash-map (rest route)) re-context)) :else - `(let [route# ~route] - (with-meta - (clout/route-compile (str route# ":__path-info") ~re-context) - (if (string? route#) - {:context-route route#} - {})))))) - -(defn ^:no-doc make-context [route make-handler] + `(clout/route-compile (str ~route ":__path-info") ~re-context)))) + +(defn ^:no-doc make-context [route path make-handler] (letfn [(handler ([request] (when-let [context-handler (make-handler request)] @@ -312,10 +299,10 @@ handler (fn ([request] - (if-let [request (context-request request route)] + (if-let [request (context-request request route path)] (handler request))) ([request respond raise] - (if-let [request (context-request request route)] + (if-let [request (context-request request route path)] (handler request respond raise) (respond nil))))))) @@ -331,6 +318,7 @@ [path args & routes] `(make-context ~(context-route path) + ~path (fn [request#] (let-request [~args request#] (routes ~@routes))))) diff --git a/test/compojure/core_test.clj b/test/compojure/core_test.clj index 8da4a92..cf35e70 100644 --- a/test/compojure/core_test.clj +++ b/test/compojure/core_test.clj @@ -332,7 +332,7 @@ (testing "request has matched route information with path prefix" (is (= (request :compojure/route) [:get "/bar/:bar-id"])) - (is (= (request :compojure/context) + (is (= (request :compojure/context-path) "/foo/:foo-id")) (is (= (request :compojure/full-route) "/foo/:foo-id/bar/:bar-id")))) @@ -342,7 +342,7 @@ (GET "/baz/:baz-id" req req))) request (route (mock/request :get "/foo/1/bar/2/baz/3"))] (testing "request has matched route information with multiple path prefix" - (is (= (request :compojure/context) + (is (= (request :compojure/context-path) "/foo/:foo-id/bar/:bar-id")) (is (= (request :compojure/full-route) "/foo/:foo-id/bar/:bar-id/baz/:baz-id")))))