routing subsystem

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

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

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

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

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

@ -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!"))]))

Loading…
Cancel
Save