moving to compojure; api done
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# starts the app in :dev mode
|
||||
run:
|
||||
@DEVMODE=1 lein run
|
||||
@DEVMODE=1 lein ring server
|
||||
|
||||
server:
|
||||
redis-server &
|
||||
|
||||
+6
-3
@@ -5,6 +5,9 @@
|
||||
[cheshire "5.3.1"]
|
||||
[ring/ring-core "1.1.0"]
|
||||
[com.taoensso/carmine "2.4.4"]
|
||||
[noir "1.3.0-beta1"]]
|
||||
:jvm-opts ["-Dfile.encoding=utf-8"]
|
||||
:main NoteHub.server)
|
||||
[compojure "1.1.6"]]
|
||||
:plugins [[lein-ring "0.8.10"]]
|
||||
:ring {:handler notehub.handler/app}
|
||||
:profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
|
||||
[ring-mock "0.1.5"]]}}
|
||||
:jvm-opts ["-Dfile.encoding=utf-8"])
|
||||
|
||||
+5
-14
@@ -1,15 +1,15 @@
|
||||
(ns NoteHub.api
|
||||
(ns notehub.api
|
||||
(:import
|
||||
[java.util Calendar])
|
||||
(:use
|
||||
[NoteHub.settings]
|
||||
[notehub.settings]
|
||||
[ring.util.codec :only [url-encode]]
|
||||
[clojure.string :rename {replace sreplace}
|
||||
:only [replace blank? lower-case split-lines split]])
|
||||
(:require
|
||||
[ring.util.codec]
|
||||
[hiccup.util :as util]
|
||||
[NoteHub.storage :as storage]))
|
||||
[notehub.storage :as storage]))
|
||||
|
||||
(def version "1.1")
|
||||
|
||||
@@ -60,15 +60,6 @@
|
||||
(str domain "/" (storage/create-short-url token {:year year :month month :day day :title title}))
|
||||
(str domain (url year month day title))))))
|
||||
|
||||
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
|
||||
(defn get-signature
|
||||
"Returns the MD5 hash for the concatenation of all passed parameters"
|
||||
[& args]
|
||||
(let [input (sreplace (apply str args) #"[\r\n]" "")]
|
||||
(do (.reset md5Instance)
|
||||
(.update md5Instance (.getBytes input))
|
||||
(.toString (new java.math.BigInteger 1 (.digest md5Instance)) 16)))))
|
||||
|
||||
(defn get-note [noteID]
|
||||
(if (storage/note-exists? noteID)
|
||||
(let [note (storage/get-note noteID)]
|
||||
@@ -87,7 +78,7 @@
|
||||
;(log "post-note: %s" {:pid pid :signature signature :password password :note note})
|
||||
(let [errors (filter identity
|
||||
[(when-not (storage/valid-publisher? pid) "pid invalid")
|
||||
(when-not (= signature (get-signature pid (storage/get-psk pid) note))
|
||||
(when-not (= signature (storage/sign pid (storage/get-psk pid) note))
|
||||
"signature invalid")
|
||||
(when (blank? note) "note is empty")])]
|
||||
(if (empty? errors)
|
||||
@@ -121,7 +112,7 @@
|
||||
;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note})
|
||||
(let [errors (filter identity
|
||||
[(when-not (storage/valid-publisher? pid) "pid invalid")
|
||||
(when-not (= signature (get-signature pid (storage/get-psk pid) noteID note password))
|
||||
(when-not (= signature (storage/sign pid (storage/get-psk pid) noteID note password))
|
||||
"signature invalid")
|
||||
(when (blank? note) "note is empty")
|
||||
(when-not (storage/valid-password? noteID password) "password invalid")])]
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
(ns NoteHub.views.pages
|
||||
(:require [hiccup.util :as util]
|
||||
[NoteHub.api :as api]
|
||||
[NoteHub.storage :as storage]
|
||||
[cheshire.core :refer :all])
|
||||
(:use
|
||||
[NoteHub.settings]
|
||||
(ns notehub.handler
|
||||
(:use compojure.core
|
||||
[notehub.settings]
|
||||
[clojure.string :rename {replace sreplace}
|
||||
:only [escape split replace blank? split-lines lower-case]]
|
||||
[clojure.core.incubator :only [-?>]]
|
||||
[noir.util.crypt :only [encrypt]]
|
||||
[hiccup.form]
|
||||
[hiccup.core]
|
||||
[hiccup.element]
|
||||
[hiccup.util :only [escape-html]]
|
||||
[hiccup.page :only [include-js html5]]
|
||||
[noir.response :only [redirect status content-type]]
|
||||
[noir.core :only [defpage defpartial]]
|
||||
[noir.statuses]))
|
||||
[hiccup.page :only [include-js html5]])
|
||||
(:require [compojure.handler :as handler]
|
||||
[compojure.route :as route]
|
||||
[hiccup.util :as util]
|
||||
[notehub.api :as api]
|
||||
[notehub.storage :as storage]
|
||||
[cheshire.core :refer :all]))
|
||||
|
||||
(when-not (storage/valid-publisher? "NoteHub")
|
||||
(storage/register-publisher "NoteHub"))
|
||||
|
||||
; Creates the main html layout
|
||||
(defpartial layout
|
||||
(defn layout
|
||||
[title & content]
|
||||
(html5
|
||||
[:head
|
||||
@@ -40,6 +36,32 @@
|
||||
(if-not (get-setting :dev-mode) (include-js "/js/google-analytics.js"))]
|
||||
[:body {:onload "onLoad()"} content]))
|
||||
|
||||
|
||||
(defn md-node
|
||||
"Returns an HTML element with a textarea inside
|
||||
containing the markdown text (to keep all chars unescaped)"
|
||||
([cls input] (md-node cls {} input))
|
||||
([cls opts input]
|
||||
[(keyword (str (name cls) ".markdown")) opts
|
||||
[:textarea input]]))
|
||||
|
||||
#_ (
|
||||
|
||||
; ######## OLD CODE START
|
||||
|
||||
(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]]))
|
||||
|
||||
(when-not (storage/valid-publisher? "NoteHub")
|
||||
(storage/register-publisher "NoteHub"))
|
||||
|
||||
(defn sanitize
|
||||
"Breakes all usages of <script> & <iframe>"
|
||||
[input]
|
||||
@@ -80,13 +102,6 @@
|
||||
(defn generate-session []
|
||||
(encrypt (str (rand-int Integer/MAX_VALUE))))
|
||||
|
||||
(defn md-node
|
||||
"Returns an HTML element with a textarea inside
|
||||
containing the markdown text (to keep all chars unescaped)"
|
||||
([cls input] (md-node cls {} input))
|
||||
([cls opts input]
|
||||
[(keyword (str (name cls) ".markdown")) opts
|
||||
[:textarea input]]))
|
||||
|
||||
; Routes
|
||||
; ======
|
||||
@@ -135,13 +150,6 @@
|
||||
[:tr [:td (str (get-message %) ":")] [:td (% stats)]])
|
||||
[:published :edited :publisher :views])])))
|
||||
|
||||
(defpage "/:short-url" {:keys [short-url]}
|
||||
(when-let [params (storage/resolve-url short-url)]
|
||||
(let [{:keys [year month day title]} params
|
||||
rest-params (dissoc params :year :month :day :title)
|
||||
core-url (api/url year month day title)
|
||||
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
|
||||
(redirect long-url))))
|
||||
|
||||
(defpage "/:year/:month/:day/:title/edit" {:keys [year month day title]}
|
||||
(let [noteID (api/build-key [year month day] title)]
|
||||
@@ -181,23 +189,48 @@
|
||||
(response 403)))
|
||||
(response 500))))
|
||||
|
||||
; Here lives the API
|
||||
; ###### END OLD CODE
|
||||
)
|
||||
|
||||
(defpage "/api" args
|
||||
(layout (get-message :api-title)
|
||||
(defn redirect [url]
|
||||
{:status 302
|
||||
:headers {"Location" (str url)}
|
||||
:body ""})
|
||||
|
||||
(defroutes api-routes
|
||||
|
||||
(GET "/" [] (layout (get-message :api-title)
|
||||
(md-node :article (slurp "API.md"))))
|
||||
|
||||
(defpage [:get "/api/note"] {:keys [version noteID]}
|
||||
(GET "/note" [version noteID]
|
||||
(generate-string (api/get-note noteID)))
|
||||
|
||||
(defpage [:post "/api/note"] {:keys [version note pid signature password] :as params}
|
||||
(POST "/note" {params :params}
|
||||
(generate-string
|
||||
(api/post-note
|
||||
note
|
||||
pid
|
||||
signature
|
||||
(:note params)
|
||||
(:pid params)
|
||||
(:signature params)
|
||||
{:params (dissoc params :version :note :pid :signature :password)
|
||||
:password password})))
|
||||
:password (:password params)})))
|
||||
|
||||
(defpage [:put "/api/note"] {:keys [version noteID note pid signature password]}
|
||||
(generate-string (api/update-note noteID note pid signature password)))
|
||||
(PUT "/note" [version noteID note pid signature password]
|
||||
(generate-string (api/update-note noteID note pid signature password))))
|
||||
|
||||
(defroutes app-routes
|
||||
(context "/api" [] api-routes)
|
||||
(GET "/" [] "Hello World")
|
||||
|
||||
(GET "/:short-url" [short-url]
|
||||
(when-let [params (storage/resolve-url short-url)]
|
||||
(let [{:keys [year month day title]} params
|
||||
rest-params (dissoc params :year :month :day :title)
|
||||
core-url (api/url year month day title)
|
||||
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
|
||||
(redirect long-url))))
|
||||
|
||||
(route/resources "/resources")
|
||||
(route/not-found "Not Found"))
|
||||
|
||||
(def app
|
||||
(handler/site app-routes))
|
||||
@@ -1,10 +0,0 @@
|
||||
(ns NoteHub.server
|
||||
(:require [noir.server :as server]))
|
||||
|
||||
(server/load-views "src/NoteHub/views/")
|
||||
|
||||
(defn -main [& m]
|
||||
(let [mode (keyword (or (first m) :prod))
|
||||
port (Integer. (get (System/getenv) "PORT" "8080"))]
|
||||
(server/start port {:mode mode :ns 'NoteHub})))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(ns NoteHub.settings
|
||||
(ns notehub.settings
|
||||
(:refer-clojure :exclude [replace reverse])
|
||||
(:use [clojure.string]))
|
||||
|
||||
|
||||
+14
-6
@@ -1,11 +1,19 @@
|
||||
(ns NoteHub.storage
|
||||
(:use [NoteHub.settings]
|
||||
[clojure.string :only (blank?)]
|
||||
[noir.util.crypt :only [encrypt]])
|
||||
(ns notehub.storage
|
||||
(:use [notehub.settings]
|
||||
[clojure.string :only (blank? replace) :rename {replace sreplace}])
|
||||
(:require [taoensso.carmine :as car :refer (wcar)]))
|
||||
|
||||
(def conn {:pool {} :spec {:uri (get-setting :db-url)}})
|
||||
|
||||
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
|
||||
(defn sign
|
||||
"Returns the MD5 hash for the concatenation of all passed parameters"
|
||||
[& args]
|
||||
(let [input (sreplace (apply str args) #"[\r\n]" "")]
|
||||
(do (.reset md5Instance)
|
||||
(.update md5Instance (.getBytes input))
|
||||
(.toString (new java.math.BigInteger 1 (.digest md5Instance)) 16)))))
|
||||
|
||||
(defmacro redis [cmd & body]
|
||||
`(car/wcar conn
|
||||
(~(symbol "car" (name cmd))
|
||||
@@ -20,7 +28,7 @@
|
||||
(defn register-publisher [pid]
|
||||
"Returns nil if given PID exists or a PSK otherwise"
|
||||
(when (not (valid-publisher? pid))
|
||||
(let [psk (encrypt (str (rand-int Integer/MAX_VALUE) pid))]
|
||||
(let [psk (sign (str (rand-int Integer/MAX_VALUE) pid))]
|
||||
(redis :hset :publisher-key pid psk)
|
||||
psk)))
|
||||
|
||||
@@ -31,7 +39,7 @@
|
||||
(redis :hget :publisher-key pid))
|
||||
|
||||
(defn create-session []
|
||||
(let [token (encrypt (str (rand-int Integer/MAX_VALUE)))]
|
||||
(let [token (sign (str (rand-int Integer/MAX_VALUE)))]
|
||||
(redis :sadd :sessions token)
|
||||
token))
|
||||
|
||||
|
||||
+32
-21
@@ -1,9 +1,9 @@
|
||||
(ns NoteHub.test.api
|
||||
(ns notehub.test.api
|
||||
(:require
|
||||
[cheshire.core :refer :all]
|
||||
[NoteHub.storage :as storage])
|
||||
(:use [NoteHub.api]
|
||||
[noir.util.test]
|
||||
[notehub.storage :as storage])
|
||||
(:use [notehub.api]
|
||||
[notehub.handler]
|
||||
[clojure.test]))
|
||||
|
||||
(def note "hello world!\nThis is a _test_ note!")
|
||||
@@ -15,6 +15,15 @@
|
||||
|
||||
(defmacro isnt [arg] `(is (not ~arg)))
|
||||
|
||||
(defn send-request
|
||||
([resource] (send-request resource {}))
|
||||
([resource params]
|
||||
(let [[method url] (if (vector? resource) resource [:get resource])]
|
||||
(app-routes {:request-method method :uri url :params params}))))
|
||||
|
||||
(defn has-status [input status]
|
||||
(= status (:status input)))
|
||||
|
||||
(defn register-publisher-fixture [f]
|
||||
(def psk (storage/register-publisher pid))
|
||||
(f)
|
||||
@@ -37,8 +46,8 @@
|
||||
(isnt (storage/valid-publisher? pid2))))
|
||||
(testing "note publishing & retrieval"
|
||||
(isnt (:success (:status (get-note "some note id"))))
|
||||
(is (= "note is empty" (:message (:status (post-note "" pid (get-signature pid psk ""))))))
|
||||
(let [post-response (post-note note pid (get-signature pid psk note))
|
||||
(is (= "note is empty" (:message (:status (post-note "" pid (storage/sign pid psk ""))))))
|
||||
(let [post-response (post-note note pid (storage/sign pid psk note))
|
||||
get-response (get-note (:noteID post-response))]
|
||||
(is (:success (:status post-response)))
|
||||
(is (:success (:status get-response)))
|
||||
@@ -48,8 +57,10 @@
|
||||
(is (storage/note-exists? (:noteID post-response)))
|
||||
(let [su (last (clojure.string/split (:shortURL get-response) #"/"))]
|
||||
(is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su)))))
|
||||
(let [resp (send-request
|
||||
(let [_ (println "DEBUG I" (clojure.string/replace (:shortURL get-response) domain ""))
|
||||
resp (send-request
|
||||
(clojure.string/replace (:shortURL get-response) domain ""))
|
||||
_ (println "DEBUG II" ((:headers resp) "Location"))
|
||||
resp (send-request ((:headers resp) "Location"))]
|
||||
(is (substring? "hello world"(:body resp))))
|
||||
(is (= (:publisher get-response) pid))
|
||||
@@ -58,38 +69,38 @@
|
||||
(isnt (get-in get-response [:statistics :edited]))
|
||||
(is (= "3" (get-in (get-note (:noteID post-response)) [:statistics :views])))))
|
||||
(testing "creation with wrong signature"
|
||||
(let [response (post-note note pid (get-signature pid2 psk note))]
|
||||
(let [response (post-note note pid (storage/sign pid2 psk note))]
|
||||
(isnt (:success (:status response)))
|
||||
(is (= "signature invalid" (:message (:status response)))))
|
||||
(let [response (post-note note pid (get-signature pid2 psk "any note"))]
|
||||
(let [response (post-note note pid (storage/sign pid2 psk "any note"))]
|
||||
(isnt (:success (:status response)))
|
||||
(is (= "signature invalid" (:message (:status response)))))
|
||||
(isnt (:success (:status (post-note note pid (get-signature pid "random_psk" note)))))
|
||||
(is (:success (:status (post-note note pid (get-signature pid psk note)))))
|
||||
(isnt (:success (:status (post-note note pid (storage/sign pid "random_psk" note)))))
|
||||
(is (:success (:status (post-note note pid (storage/sign pid psk note)))))
|
||||
(let [randomPID "randomPID"
|
||||
psk2 (storage/register-publisher randomPID)
|
||||
_ (storage/revoke-publisher randomPID)
|
||||
response (post-note note randomPID (get-signature randomPID psk2 note))]
|
||||
response (post-note note randomPID (storage/sign randomPID psk2 note))]
|
||||
(isnt (:success (:status response)))
|
||||
(is (= (:message (:status response)) "pid invalid"))))
|
||||
(testing "note update"
|
||||
(let [post-response (post-note note pid (get-signature pid psk note) {:password "passwd"})
|
||||
(let [post-response (post-note note pid (storage/sign pid psk note) {:password "passwd"})
|
||||
note-id (:noteID post-response)
|
||||
new-note "a new note!"]
|
||||
(is (:success (:status post-response)))
|
||||
(is (:success (:status (get-note note-id))))
|
||||
(is (= note (:note (get-note note-id))))
|
||||
(let [update-response (update-note note-id new-note pid (get-signature pid psk new-note) "passwd")]
|
||||
(let [update-response (update-note note-id new-note pid (storage/sign pid psk new-note) "passwd")]
|
||||
(isnt (:success (:status update-response)))
|
||||
(is (= "signature invalid" (:message (:status update-response)))))
|
||||
(is (= note (:note (get-note note-id))))
|
||||
(let [update-response (update-note note-id new-note pid
|
||||
(get-signature pid psk note-id new-note "passwd") "passwd")]
|
||||
(storage/sign pid psk note-id new-note "passwd") "passwd")]
|
||||
(is (= { :success true } (:status update-response)))
|
||||
(isnt (= nil (get-in (get-note note-id) [:statistics :edited])))
|
||||
(is (= new-note (:note (get-note note-id)))))
|
||||
(let [update-response (update-note note-id "aaa" pid
|
||||
(get-signature pid psk note-id "aaa" "pass") "pass")]
|
||||
(storage/sign pid psk note-id "aaa" "pass") "pass")]
|
||||
(isnt (:success (:status update-response)))
|
||||
(is (= "password invalid" (:message (:status update-response)))))
|
||||
(is (= new-note (:note (get-note note-id))))
|
||||
@@ -100,7 +111,7 @@
|
||||
(let [response (send-request [:post "/api/note"]
|
||||
{:note note
|
||||
:pid pid
|
||||
:signature (get-signature pid psk note)
|
||||
:signature (storage/sign pid psk note)
|
||||
:version "1.0"})
|
||||
body (parse-string (:body response))
|
||||
noteID (body "noteID")]
|
||||
@@ -118,7 +129,7 @@
|
||||
(let [response (send-request [:post "/api/note"]
|
||||
{:note note
|
||||
:pid pid
|
||||
:signature (get-signature pid psk note)
|
||||
:signature (storage/sign pid psk note)
|
||||
:version "1.0"
|
||||
:theme "dark"
|
||||
:text-font "Helvetica"})
|
||||
@@ -140,7 +151,7 @@
|
||||
(let [response (send-request [:post "/api/note"]
|
||||
{:note note
|
||||
:pid pid
|
||||
:signature (get-signature pid psk note)
|
||||
:signature (storage/sign pid psk note)
|
||||
:version "1.0"
|
||||
:password "qwerty"})
|
||||
body (parse-string (:body response))
|
||||
@@ -156,7 +167,7 @@
|
||||
{:noteID noteID
|
||||
:note "WRONG pass"
|
||||
:pid pid
|
||||
:signature (get-signature pid psk noteID "WRONG pass" "qwerty1")
|
||||
:signature (storage/sign pid psk noteID "WRONG pass" "qwerty1")
|
||||
:password "qwerty1"
|
||||
:version "1.0"})
|
||||
body (parse-string (:body response))]
|
||||
@@ -172,7 +183,7 @@
|
||||
{:noteID noteID
|
||||
:note "UPDATED CONTENT"
|
||||
:pid pid
|
||||
:signature (get-signature pid psk noteID "UPDATED CONTENT" "qwerty")
|
||||
:signature (storage/sign pid psk noteID "UPDATED CONTENT" "qwerty")
|
||||
:password "qwerty"
|
||||
:version "1.0"}))) ["status" "success"]))
|
||||
(isnt (= nil (((parse-string
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
(ns notehub.test.handler
|
||||
(:use clojure.test
|
||||
ring.mock.request
|
||||
notehub.handler))
|
||||
|
||||
#_(
|
||||
|
||||
(ns NoteHub.test.views.pages
|
||||
(:use [NoteHub.views.pages]
|
||||
[NoteHub.api :only [build-key get-signature get-date url]]
|
||||
@@ -112,7 +119,22 @@
|
||||
(is (has-status (send-request [:post "/post-note"]) 400)))
|
||||
(testing "valid accesses"
|
||||
;(is (has-status (send-request "/new") 200) "accessing /new")
|
||||
(is (has-status (send-request "/api") 200) "accessing API")
|
||||
(is (has-status (send-request (url 2012 6 3 "some-title")) 200) "accessing test note")
|
||||
(is (has-status (send-request (url 2012 6 3 "some-title" "export")) 200) "accessing test note's export")
|
||||
(is (has-status (send-request (url 2012 6 3 "some-title" "stats")) 200) "accessing test note's stats")
|
||||
(is (has-status (send-request "/") 200) "accessing landing page"))))
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
(deftest test-app
|
||||
(testing "main route"
|
||||
(let [response (app (request :get "/"))]
|
||||
(is (= (:status response) 200))
|
||||
(is (= (:body response) "Hello World"))))
|
||||
|
||||
(testing "not-found route"
|
||||
(let [response (app (request :get "/invalid"))]
|
||||
(is (= (:status response) 404)))))
|
||||
@@ -1,7 +1,6 @@
|
||||
(ns NoteHub.test.storage
|
||||
(:use [NoteHub.storage]
|
||||
[NoteHub.api :only [build-key]]
|
||||
[NoteHub.views.pages]
|
||||
(ns notehub.test.storage
|
||||
(:use [notehub.storage]
|
||||
[notehub.api :only [build-key]]
|
||||
[clojure.test])
|
||||
(:require [taoensso.carmine :as car :refer (wcar)]))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user