routing subsystem

integration-tests
Josha von Gizycki 6 years ago
parent d56f20dab3
commit c7ef20f09b

@ -5,18 +5,23 @@
[buddy.hashers :as hashers] [buddy.hashers :as hashers]
[hiccup.form :as hform] [hiccup.form :as hform]
[wanijo.framework.view :as view] [wanijo.framework.view :as view]
[wanijo.framework.routing :refer [register path]]
[wanijo.user.domain :as user-domain])) [wanijo.user.domain :as user-domain]))
(register :auth-login "/login")
(register :auth-login-check "/login-check")
(register :auth-logout "/logout")
(defn- login-check! [req] (defn- login-check! [req]
(let [{{:keys [uname pw]} :params} req (let [{{:keys [uname pw]} :params} req
unode (user-domain/find! uname) unode (user-domain/find! uname)
pwmatch (when-let [hash (:pw unode)] pwmatch (when-let [hash (:pw unode)]
(hashers/check pw hash))] (hashers/check pw hash))]
(if pwmatch (if pwmatch
(-> (redirect "/") (-> (redirect (path :home))
(assoc-in [:session :ident] uname) (assoc-in [:session :ident] uname)
(assoc-in [:session :uuid] (:uuid unode))) (assoc-in [:session :uuid] (:uuid unode)))
(-> (redirect "/login") (-> (redirect (path :auth-login))
(assoc :flash :invalid-credentials))))) (assoc :flash :invalid-credentials)))))
(defn login! [req] (defn login! [req]
@ -24,7 +29,7 @@
:content :content
[[:h1 "Kama ken"] [[:h1 "Kama ken"]
(hform/form-to (hform/form-to
[:post "/login-check"] [:post (path :auth-login-check)]
(when (:flash req) [:section.flash (:flash req)]) (when (:flash req) [:section.flash (:flash req)])
;; ;;
(hform/label "uname" "Nimi") (hform/label "uname" "Nimi")

@ -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))

@ -1,6 +1,7 @@
(ns wanijo.framework.view (ns wanijo.framework.view
(:require [hiccup.page :refer (:require [hiccup.page :refer
[html5 include-css]])) [html5 include-css]]
[wanijo.framework.routing :refer [path]]))
(defn btnlink (defn btnlink
([target caption] ([target caption]
@ -35,7 +36,7 @@
(str "O, " ident)])] (str "O, " ident)])]
(when authed? (when authed?
[:section.header-content [: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")])] (btnlink "/logout" "Lape!" "header-content__link")])]
[:nav (when authed? "nav")] [:nav (when authed? "nav")]
(vec (concat [:main] content)) (vec (concat [:main] content))

@ -1,6 +1,9 @@
(ns wanijo.home.routes (ns wanijo.home.routes
(:require [compojure.core :refer [defroutes GET]] (:require [compojure.core :refer [defroutes GET]]
[wanijo.framework.routing :refer [register]]
[wanijo.home.view :as home-view])) [wanijo.home.view :as home-view]))
(register :home "/")
(defroutes routes (defroutes routes
(GET "/" [] home-view/root!)) (GET "/" [] home-view/root!))

@ -3,6 +3,7 @@
[ring.util.response :as resp] [ring.util.response :as resp]
[wanijo.framework.view :as view] [wanijo.framework.view :as view]
[wanijo.framework.form :as form] [wanijo.framework.form :as form]
[wanijo.framework.routing :refer [register]]
[wanijo.schema.domain :as domain] [wanijo.schema.domain :as domain]
[wanijo.schema.view :as view-schema] [wanijo.schema.view :as view-schema]
[wanijo.attribute.domain :as attr-domain])) [wanijo.attribute.domain :as attr-domain]))
@ -29,6 +30,11 @@
(resp/redirect "/schema")) (resp/redirect "/schema"))
{:status 403})) {:status 403}))
(register :schema-overview "/schema")
(register :schema-show "/schema/:uuid")
(register :schema-new "/schema/new")
(register :schema-delete "/schema/:uuid")
(defroutes routes (defroutes routes
(GET "/schema" [] view-schema/overview!) (GET "/schema" [] view-schema/overview!)
(GET "/schema/:uuid" [uuid :as req] (show-schema! uuid (:session req))) (GET "/schema/:uuid" [uuid :as req] (show-schema! uuid (:session req)))

@ -3,6 +3,7 @@
[ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.anti-forgery :refer [anti-forgery-field]]
[wanijo.framework.view :as view] [wanijo.framework.view :as view]
[wanijo.framework.form :as form] [wanijo.framework.form :as form]
[wanijo.framework.routing :refer [path]]
[wanijo.schema.domain :as domain])) [wanijo.schema.domain :as domain]))
(def new-form (def new-form
@ -27,12 +28,12 @@
(for [schema schemas] (for [schema schemas]
[:tr [:tr
[:td [:td
[:a {:href (str "/schema/" (:uuid schema))} [:a {:href (path :schema-show schema)}
(:name schema)]] (:name schema)]]
[:td (:created_at schema)]])]] [:td (:created_at schema)]])]]
[:h1 "Pali sin e jaki ijo"] [:h1 "Pali sin e jaki ijo"]
(hform/form-to (hform/form-to
[:post "/schema/new"] [:post (path :schema-new)]
(form/field new-form :schema-name req) (form/field new-form :schema-name req)
(hform/submit-button "Pali") (hform/submit-button "Pali")
(anti-forgery-field))]))) (anti-forgery-field))])))
@ -51,6 +52,6 @@
[:h2 "Mute pali"] [:h2 "Mute pali"]
(hform/form-to (hform/form-to
{:class "inline"} {:class "inline"}
[:delete (str "/schema/" (:uuid schema))] [:delete (path :schema-delete schema)]
(anti-forgery-field) (anti-forgery-field)
(hform/submit-button "Pakala!"))])) (hform/submit-button "Pakala!"))]))

Loading…
Cancel
Save