From c7ef20f09b7024e76228e922fd66243910a42fef Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 18 Jul 2018 16:02:00 +0200 Subject: [PATCH] routing subsystem --- src/wanijo/framework/auth.clj | 11 +++++-- src/wanijo/framework/routing.clj | 49 ++++++++++++++++++++++++++++++++ src/wanijo/framework/view.clj | 5 ++-- src/wanijo/home/routes.clj | 3 ++ src/wanijo/schema/routes.clj | 6 ++++ src/wanijo/schema/view.clj | 7 +++-- 6 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/wanijo/framework/routing.clj diff --git a/src/wanijo/framework/auth.clj b/src/wanijo/framework/auth.clj index a70d679..fe6e347 100644 --- a/src/wanijo/framework/auth.clj +++ b/src/wanijo/framework/auth.clj @@ -5,18 +5,23 @@ [buddy.hashers :as hashers] [hiccup.form :as hform] [wanijo.framework.view :as view] + [wanijo.framework.routing :refer [register path]] [wanijo.user.domain :as user-domain])) +(register :auth-login "/login") +(register :auth-login-check "/login-check") +(register :auth-logout "/logout") + (defn- login-check! [req] (let [{{:keys [uname pw]} :params} req unode (user-domain/find! uname) pwmatch (when-let [hash (:pw unode)] (hashers/check pw hash))] (if pwmatch - (-> (redirect "/") + (-> (redirect (path :home)) (assoc-in [:session :ident] uname) (assoc-in [:session :uuid] (:uuid unode))) - (-> (redirect "/login") + (-> (redirect (path :auth-login)) (assoc :flash :invalid-credentials))))) (defn login! [req] @@ -24,7 +29,7 @@ :content [[:h1 "Kama ken"] (hform/form-to - [:post "/login-check"] + [:post (path :auth-login-check)] (when (:flash req) [:section.flash (:flash req)]) ;; (hform/label "uname" "Nimi") diff --git a/src/wanijo/framework/routing.clj b/src/wanijo/framework/routing.clj new file mode 100644 index 0000000..9af06d3 --- /dev/null +++ b/src/wanijo/framework/routing.clj @@ -0,0 +1,49 @@ +(ns wanijo.framework.routing + (:require [clojure.string :as string])) + +(def all-routes + (atom {})) + +(defn register [id path] + (swap! all-routes + assoc id path)) + +(defn check-params [id path params] + (when (nil? path) + (throw + (ex-info + (str "Path " id " is not registered") + {:id id + :params params}))) + (let [matcher (re-matcher #":[^/]+" path) + matches (take-while some? (repeatedly #(re-find matcher)))] + (doseq [match matches + :let [kwparam (keyword (subs match 1)) + exists? (contains? params kwparam)]] + (when-not exists? + (throw + (ex-info + (str "Key '" kwparam + "' does not exist in parameter map for path " + path) + {:path path + :params params + :id id})))))) + +(defn path + ([id] + (path id {})) + ([id params] + (let [path (id @all-routes)] + (check-params id path params) + (reduce-kv + (fn [new-path param-id param-value] + (string/replace + new-path + (str ":" (name param-id)) + param-value)) + path + params)))) + +(defn raw-path [id] + (id @all-routes)) diff --git a/src/wanijo/framework/view.clj b/src/wanijo/framework/view.clj index 4e3cb9e..51561bd 100644 --- a/src/wanijo/framework/view.clj +++ b/src/wanijo/framework/view.clj @@ -1,6 +1,7 @@ (ns wanijo.framework.view (:require [hiccup.page :refer - [html5 include-css]])) + [html5 include-css]] + [wanijo.framework.routing :refer [path]])) (defn btnlink ([target caption] @@ -35,7 +36,7 @@ (str "O, " ident)])] (when authed? [:section.header-content - (btnlink "/schema" "Jaki ijo" "header-content__link") + (btnlink (path :schema-overview) "Jaki ijo" "header-content__link") (btnlink "/logout" "Lape!" "header-content__link")])] [:nav (when authed? "nav")] (vec (concat [:main] content)) diff --git a/src/wanijo/home/routes.clj b/src/wanijo/home/routes.clj index 32c66ac..fc73112 100644 --- a/src/wanijo/home/routes.clj +++ b/src/wanijo/home/routes.clj @@ -1,6 +1,9 @@ (ns wanijo.home.routes (:require [compojure.core :refer [defroutes GET]] + [wanijo.framework.routing :refer [register]] [wanijo.home.view :as home-view])) +(register :home "/") + (defroutes routes (GET "/" [] home-view/root!)) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index 7e5a39e..786170f 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -3,6 +3,7 @@ [ring.util.response :as resp] [wanijo.framework.view :as view] [wanijo.framework.form :as form] + [wanijo.framework.routing :refer [register]] [wanijo.schema.domain :as domain] [wanijo.schema.view :as view-schema] [wanijo.attribute.domain :as attr-domain])) @@ -29,6 +30,11 @@ (resp/redirect "/schema")) {:status 403})) +(register :schema-overview "/schema") +(register :schema-show "/schema/:uuid") +(register :schema-new "/schema/new") +(register :schema-delete "/schema/:uuid") + (defroutes routes (GET "/schema" [] view-schema/overview!) (GET "/schema/:uuid" [uuid :as req] (show-schema! uuid (:session req))) diff --git a/src/wanijo/schema/view.clj b/src/wanijo/schema/view.clj index 976b7cb..38f0a76 100644 --- a/src/wanijo/schema/view.clj +++ b/src/wanijo/schema/view.clj @@ -3,6 +3,7 @@ [ring.util.anti-forgery :refer [anti-forgery-field]] [wanijo.framework.view :as view] [wanijo.framework.form :as form] + [wanijo.framework.routing :refer [path]] [wanijo.schema.domain :as domain])) (def new-form @@ -27,12 +28,12 @@ (for [schema schemas] [:tr [:td - [:a {:href (str "/schema/" (:uuid schema))} + [:a {:href (path :schema-show schema)} (:name schema)]] [:td (:created_at schema)]])]] [:h1 "Pali sin e jaki ijo"] (hform/form-to - [:post "/schema/new"] + [:post (path :schema-new)] (form/field new-form :schema-name req) (hform/submit-button "Pali") (anti-forgery-field))]))) @@ -51,6 +52,6 @@ [:h2 "Mute pali"] (hform/form-to {:class "inline"} - [:delete (str "/schema/" (:uuid schema))] + [:delete (path :schema-delete schema)] (anti-forgery-field) (hform/submit-button "Pakala!"))]))