migrated to compojure
This commit is contained in:
+32
-59
@@ -72,66 +72,14 @@
|
|||||||
(submit-button {:class "button ui-elem"
|
(submit-button {:class "button ui-elem"
|
||||||
:id :publish-button} (get-message command))])])))
|
:id :publish-button} (get-message command))])])))
|
||||||
|
|
||||||
|
; TODO: make sure the status is really set to the response!!!!
|
||||||
#_ (
|
(defn- response
|
||||||
|
"Sets a custom message for each needed HTTP status.
|
||||||
; ######## OLD CODE START
|
The message to be assigned is extracted with a dynamically generated key"
|
||||||
|
[code]
|
||||||
(ns NoteHub.views.pages
|
|
||||||
(:require )
|
|
||||||
(:use
|
|
||||||
|
|
||||||
|
|
||||||
[noir.response :only [redirect status content-type]]
|
|
||||||
[noir.core :only [defpage defpartial]]
|
|
||||||
[noir.statuses]
|
|
||||||
[noir.util.crypt :only [encrypt]]))
|
|
||||||
|
|
||||||
; Sets a custom message for each needed HTTP status.
|
|
||||||
; The message to be assigned is extracted with a dynamically generated key
|
|
||||||
(doseq [code [400 403 404 500]]
|
|
||||||
(set-page! code
|
|
||||||
(let [message (get-message (keyword (str "status-" code)))]
|
(let [message (get-message (keyword (str "status-" code)))]
|
||||||
(layout message
|
(layout message
|
||||||
[:article [:h1 message]]))))
|
[:article [:h1 message]])))
|
||||||
|
|
||||||
|
|
||||||
(defn- response
|
|
||||||
"shortcut for rendering an HTTP status"
|
|
||||||
[code]
|
|
||||||
(status code (get-page code)))
|
|
||||||
|
|
||||||
; Routes
|
|
||||||
; ======
|
|
||||||
|
|
||||||
(defpage [:post "/post-note"] {:keys [session note signature password version]}
|
|
||||||
(if (= signature (api/get-signature session note))
|
|
||||||
(let [pid "NoteHub"
|
|
||||||
psk (storage/get-psk pid)]
|
|
||||||
(if (storage/valid-publisher? pid)
|
|
||||||
(let [resp (api/post-note note pid (api/get-signature pid psk note) {:password password})]
|
|
||||||
(if (and
|
|
||||||
(storage/invalidate-session session)
|
|
||||||
(get-in resp [:status :success]))
|
|
||||||
(redirect (:longURL resp))
|
|
||||||
(response 400)))
|
|
||||||
(response 500)))
|
|
||||||
(response 400)))
|
|
||||||
|
|
||||||
(defpage [:post "/update-note"] {:keys [noteID note password version]}
|
|
||||||
(let [pid "NoteHub"
|
|
||||||
psk (storage/get-psk pid)]
|
|
||||||
(if (storage/valid-publisher? pid)
|
|
||||||
(let [resp (api/update-note noteID note pid
|
|
||||||
(api/get-signature pid psk noteID note password)
|
|
||||||
password)]
|
|
||||||
(if (get-in resp [:status :success])
|
|
||||||
(redirect (:longURL resp))
|
|
||||||
(response 403)))
|
|
||||||
(response 500))))
|
|
||||||
|
|
||||||
; ###### END OLD CODE
|
|
||||||
)
|
|
||||||
|
|
||||||
(defn redirect [url]
|
(defn redirect [url]
|
||||||
{:status 302
|
{:status 302
|
||||||
@@ -199,7 +147,7 @@
|
|||||||
|
|
||||||
(GET "/new" []
|
(GET "/new" []
|
||||||
(input-form "/post-note" :publish
|
(input-form "/post-note" :publish
|
||||||
(html (hidden-field :session (storage/create-session))
|
(html (hidden-field :session (storage/sign (str (rand-int Integer/MAX_VALUE))))
|
||||||
(hidden-field {:id :signature} :signature))
|
(hidden-field {:id :signature} :signature))
|
||||||
(get-message :loading) :set-passwd))
|
(get-message :loading) :set-passwd))
|
||||||
|
|
||||||
@@ -231,6 +179,31 @@
|
|||||||
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
|
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
|
||||||
(redirect long-url))))
|
(redirect long-url))))
|
||||||
|
|
||||||
|
|
||||||
|
(POST "/post-note" [session note signature password version]
|
||||||
|
(if (= signature (storage/sign session note))
|
||||||
|
(let [pid "NoteHub"
|
||||||
|
psk (storage/get-psk pid)]
|
||||||
|
(if (storage/valid-publisher? pid)
|
||||||
|
(let [resp (api/post-note note pid (storage/sign pid psk note) {:password password})]
|
||||||
|
(if (get-in resp [:status :success])
|
||||||
|
(redirect (:longURL resp))
|
||||||
|
(response 400)))
|
||||||
|
(response 500)))
|
||||||
|
(response 400)))
|
||||||
|
|
||||||
|
(POST "/update-note" [noteID note password version]
|
||||||
|
(let [pid "NoteHub"
|
||||||
|
psk (storage/get-psk pid)]
|
||||||
|
(if (storage/valid-publisher? pid)
|
||||||
|
(let [resp (api/update-note noteID note pid
|
||||||
|
(storage/sign pid psk noteID note password)
|
||||||
|
password)]
|
||||||
|
(if (get-in resp [:status :success])
|
||||||
|
(redirect (:longURL resp))
|
||||||
|
(response 403)))
|
||||||
|
(response 500))))
|
||||||
|
|
||||||
(route/resources "/")
|
(route/resources "/")
|
||||||
(route/not-found "Not Found"))
|
(route/not-found "Not Found"))
|
||||||
|
|
||||||
|
|||||||
@@ -38,16 +38,6 @@
|
|||||||
(defn get-psk [pid]
|
(defn get-psk [pid]
|
||||||
(redis :hget :publisher-key pid))
|
(redis :hget :publisher-key pid))
|
||||||
|
|
||||||
(defn create-session []
|
|
||||||
(let [token (sign (str (rand-int Integer/MAX_VALUE)))]
|
|
||||||
(redis :sadd :sessions token)
|
|
||||||
token))
|
|
||||||
|
|
||||||
(defn invalidate-session [token]
|
|
||||||
(let [was-valid (redis :sismember :sessions token)]
|
|
||||||
(redis :srem :sessions token)
|
|
||||||
(= 1 was-valid)))
|
|
||||||
|
|
||||||
(defn edit-note [noteID text]
|
(defn edit-note [noteID text]
|
||||||
(redis :hset :edited noteID (get-current-date))
|
(redis :hset :edited noteID (get-current-date))
|
||||||
(redis :hset :note noteID text))
|
(redis :hset :note noteID text))
|
||||||
|
|||||||
@@ -55,15 +55,6 @@
|
|||||||
"update")))
|
"update")))
|
||||||
(testing "of the note access"
|
(testing "of the note access"
|
||||||
(is (not= (get-note (build-key date test-title)) "any text")))
|
(is (not= (get-note (build-key date test-title)) "any text")))
|
||||||
(testing "session management"
|
|
||||||
(let [s1 (create-session)
|
|
||||||
s2 (create-session)
|
|
||||||
s3 (create-session)]
|
|
||||||
(is (invalidate-session s1))
|
|
||||||
(is (not (invalidate-session (str s1 s2))))
|
|
||||||
(is (invalidate-session s2))
|
|
||||||
(is (not (invalidate-session "wrongtoken")))
|
|
||||||
(is (invalidate-session s3))))
|
|
||||||
(testing "of note existence"
|
(testing "of note existence"
|
||||||
(is (note-exists? (build-key date test-title)))
|
(is (note-exists? (build-key date test-title)))
|
||||||
(is (short-url-exists? test-short-url))
|
(is (short-url-exists? test-short-url))
|
||||||
|
|||||||
Reference in New Issue
Block a user