diff --git a/Makefile b/Makefile index 6ee21be..832af07 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # starts the app in :dev mode run: - lein run dev + @DEVMODE=1 lein run server: redis-server & diff --git a/settings b/settings index e55fd78..b3bfbf0 100644 --- a/settings +++ b/settings @@ -1,3 +1,4 @@ max-title-length = 40 -domain = http://notehub.org +prod-domain = http://notehub.org +dev-domain = http://localhost:8080 db-url = http://localhost:6379 diff --git a/src/NoteHub/api.clj b/src/NoteHub/api.clj index 9915c79..e1aaaf5 100644 --- a/src/NoteHub/api.clj +++ b/src/NoteHub/api.clj @@ -11,7 +11,11 @@ (def version "1.1") -(def domain (get-setting :domain)) +(def domain + (get-setting + (if (get-setting :dev-mode) + :dev-domain + :prod-domain))) (defn log "Logs args to the server stdout" @@ -42,9 +46,9 @@ (assoc (create-response success) :message (apply format message params)))) (defn- get-path [noteID & description] - (if description - (str domain "/" (storage/get-short-url noteID)) - (let [[year month day title] (split noteID #" ")] + (let [[year month day title] (split noteID #" ")] + (if description + (str domain "/" (storage/create-short-url {:year year :month month :day day :title title})) (apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)]))))) (let [md5Instance (java.security.MessageDigest/getInstance "MD5")] @@ -90,10 +94,10 @@ title (first (drop-while #(storage/note-exists? (build-key date %)) (cons proposed-title (map #(str proposed-title "-" (+ 2 %)) (range))))) - noteID (build-key date title)] + noteID (build-key date title) + short-url (storage/create-short-url {:year year :month month :day day :title title})] (do (storage/add-note noteID note pid password) - (storage/create-short-url noteID) {:noteID noteID :longURL (get-path noteID) :shortURL (get-path noteID :short) diff --git a/src/NoteHub/storage.clj b/src/NoteHub/storage.clj index f0c7cbb..9f7a48e 100644 --- a/src/NoteHub/storage.clj +++ b/src/NoteHub/storage.clj @@ -1,8 +1,7 @@ (ns NoteHub.storage (:use [NoteHub.settings] [clojure.string :only (blank?)] - [noir.util.crypt :only [encrypt]] - [noir.options :only [dev-mode?]]) + [noir.util.crypt :only [encrypt]]) (:require [taoensso.carmine :as car :refer (wcar)])) (def conn {:pool {} :spec {:uri (get-setting :db-url)}}) @@ -112,10 +111,10 @@ (redis :hdel short-url value)))) (defn create-short-url - "Creates a short url for the given request metadata or noteID or extracts + "Creates a short url for the given request metadata or extracts one if it was already created" [arg] - (let [key (if (map? arg) (str (into (sorted-map) arg)) arg)] + (let [key (str (into (sorted-map) arg))] (if (short-url-exists? key) (redis :hget short-url key) (let [hash-stream (partition 5 (repeatedly #(rand-int 36))) diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index 3061fcf..0174d40 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -11,7 +11,6 @@ [noir.util.crypt :only [encrypt]] [hiccup.form] [hiccup.core] - [noir.options :only [dev-mode?]] [ring.util.codec :only [url-encode]] [hiccup.element] [hiccup.util :only [escape-html]] @@ -38,7 +37,7 @@ (include-js "/js/main.js") (include-js "/js/themes.js")) ; google analytics code should appear in prod mode only - (if-not (dev-mode?) (include-js "/js/google-analytics.js"))] + (if-not (get-setting :dev-mode?) (include-js "/js/google-analytics.js"))] [:body {:onload "onLoad()"} content])) ; Sets a custom message for each needed HTTP status. @@ -97,7 +96,7 @@ [:div.centered.helvetica.markdown (get-message :footer)])) ; Displays the note -(defpage "/:year/:month/:day/:title" {:keys [year month day title]} +(defpage "/:year/:month/:day/:title" {:keys [year month day title] :as params} (let [noteID (api/build-key [year month day] title)] (when (storage/note-exists? noteID) (let [note (api/get-note noteID)] @@ -105,7 +104,7 @@ [:article.bottom-space.markdown (:note note)] (let [links (map #(link-to (if (= :short-url %) - (:shortURL note) + (url (storage/create-short-url params)) (str (:longURL note) "/" (name %))) (get-message %)) [:stats :edit :export :short-url]) @@ -191,4 +190,4 @@ (generate-string (api/post-note note pid signature password))) (defpage [:put "/api/note"] {:keys [version noteID note pid signature password]} - (generate-string (api/update-note noteID note pid signature password))) \ No newline at end of file + (generate-string (api/update-note noteID note pid signature password))) diff --git a/test/NoteHub/test/api.clj b/test/NoteHub/test/api.clj index c9932db..3777910 100644 --- a/test/NoteHub/test/api.clj +++ b/test/NoteHub/test/api.clj @@ -45,11 +45,18 @@ (is (= note (:note get-response))) (is (= (:longURL post-response) (:longURL get-response) note-url)) (is (= (:shortURL post-response) (:shortURL get-response))) + (is (storage/note-exists? (:noteID post-response))) + (let [su (last (clojure.string/split (:shortURL get-response) #"/"))] + (is (= su (storage/create-short-url (storage/resolve-url su))))) + (let [resp (send-request + (clojure.string/replace (:shortURL get-response) domain "")) + resp (send-request ((:headers resp) "Location"))] + (is (substring? "hello world"(:body resp)))) (is (= (:publisher get-response) pid)) (is (= (:title get-response) (derive-title note))) (is (= "1" (get-in get-response [:statistics :views]))) (isnt (get-in get-response [:statistics :edited])) - (is (= "2" (get-in (get-note (:noteID post-response)) [:statistics :views]))))) + (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))] (isnt (:success (:status response)))