bugfix: short-urls repaired
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# starts the app in :dev mode
|
# starts the app in :dev mode
|
||||||
run:
|
run:
|
||||||
lein run dev
|
@DEVMODE=1 lein run
|
||||||
|
|
||||||
server:
|
server:
|
||||||
redis-server &
|
redis-server &
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
max-title-length = 40
|
max-title-length = 40
|
||||||
domain = http://notehub.org
|
prod-domain = http://notehub.org
|
||||||
|
dev-domain = http://localhost:8080
|
||||||
db-url = http://localhost:6379
|
db-url = http://localhost:6379
|
||||||
|
|||||||
+10
-6
@@ -11,7 +11,11 @@
|
|||||||
|
|
||||||
(def version "1.1")
|
(def version "1.1")
|
||||||
|
|
||||||
(def domain (get-setting :domain))
|
(def domain
|
||||||
|
(get-setting
|
||||||
|
(if (get-setting :dev-mode)
|
||||||
|
:dev-domain
|
||||||
|
:prod-domain)))
|
||||||
|
|
||||||
(defn log
|
(defn log
|
||||||
"Logs args to the server stdout"
|
"Logs args to the server stdout"
|
||||||
@@ -42,9 +46,9 @@
|
|||||||
(assoc (create-response success) :message (apply format message params))))
|
(assoc (create-response success) :message (apply format message params))))
|
||||||
|
|
||||||
(defn- get-path [noteID & description]
|
(defn- get-path [noteID & description]
|
||||||
(if description
|
(let [[year month day title] (split noteID #" ")]
|
||||||
(str domain "/" (storage/get-short-url noteID))
|
(if description
|
||||||
(let [[year month day title] (split noteID #" ")]
|
(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)])))))
|
(apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)])))))
|
||||||
|
|
||||||
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
|
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
|
||||||
@@ -90,10 +94,10 @@
|
|||||||
title (first (drop-while #(storage/note-exists? (build-key date %))
|
title (first (drop-while #(storage/note-exists? (build-key date %))
|
||||||
(cons proposed-title
|
(cons proposed-title
|
||||||
(map #(str proposed-title "-" (+ 2 %)) (range)))))
|
(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
|
(do
|
||||||
(storage/add-note noteID note pid password)
|
(storage/add-note noteID note pid password)
|
||||||
(storage/create-short-url noteID)
|
|
||||||
{:noteID noteID
|
{:noteID noteID
|
||||||
:longURL (get-path noteID)
|
:longURL (get-path noteID)
|
||||||
:shortURL (get-path noteID :short)
|
:shortURL (get-path noteID :short)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
(ns NoteHub.storage
|
(ns NoteHub.storage
|
||||||
(:use [NoteHub.settings]
|
(:use [NoteHub.settings]
|
||||||
[clojure.string :only (blank?)]
|
[clojure.string :only (blank?)]
|
||||||
[noir.util.crypt :only [encrypt]]
|
[noir.util.crypt :only [encrypt]])
|
||||||
[noir.options :only [dev-mode?]])
|
|
||||||
(:require [taoensso.carmine :as car :refer (wcar)]))
|
(:require [taoensso.carmine :as car :refer (wcar)]))
|
||||||
|
|
||||||
(def conn {:pool {} :spec {:uri (get-setting :db-url)}})
|
(def conn {:pool {} :spec {:uri (get-setting :db-url)}})
|
||||||
@@ -112,10 +111,10 @@
|
|||||||
(redis :hdel short-url value))))
|
(redis :hdel short-url value))))
|
||||||
|
|
||||||
(defn create-short-url
|
(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"
|
one if it was already created"
|
||||||
[arg]
|
[arg]
|
||||||
(let [key (if (map? arg) (str (into (sorted-map) arg)) arg)]
|
(let [key (str (into (sorted-map) arg))]
|
||||||
(if (short-url-exists? key)
|
(if (short-url-exists? key)
|
||||||
(redis :hget short-url key)
|
(redis :hget short-url key)
|
||||||
(let [hash-stream (partition 5 (repeatedly #(rand-int 36)))
|
(let [hash-stream (partition 5 (repeatedly #(rand-int 36)))
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
[noir.util.crypt :only [encrypt]]
|
[noir.util.crypt :only [encrypt]]
|
||||||
[hiccup.form]
|
[hiccup.form]
|
||||||
[hiccup.core]
|
[hiccup.core]
|
||||||
[noir.options :only [dev-mode?]]
|
|
||||||
[ring.util.codec :only [url-encode]]
|
[ring.util.codec :only [url-encode]]
|
||||||
[hiccup.element]
|
[hiccup.element]
|
||||||
[hiccup.util :only [escape-html]]
|
[hiccup.util :only [escape-html]]
|
||||||
@@ -38,7 +37,7 @@
|
|||||||
(include-js "/js/main.js")
|
(include-js "/js/main.js")
|
||||||
(include-js "/js/themes.js"))
|
(include-js "/js/themes.js"))
|
||||||
; google analytics code should appear in prod mode only
|
; 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]))
|
[:body {:onload "onLoad()"} content]))
|
||||||
|
|
||||||
; Sets a custom message for each needed HTTP status.
|
; Sets a custom message for each needed HTTP status.
|
||||||
@@ -97,7 +96,7 @@
|
|||||||
[:div.centered.helvetica.markdown (get-message :footer)]))
|
[:div.centered.helvetica.markdown (get-message :footer)]))
|
||||||
|
|
||||||
; Displays the note
|
; 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)]
|
(let [noteID (api/build-key [year month day] title)]
|
||||||
(when (storage/note-exists? noteID)
|
(when (storage/note-exists? noteID)
|
||||||
(let [note (api/get-note noteID)]
|
(let [note (api/get-note noteID)]
|
||||||
@@ -105,7 +104,7 @@
|
|||||||
[:article.bottom-space.markdown (:note note)]
|
[:article.bottom-space.markdown (:note note)]
|
||||||
(let [links (map #(link-to
|
(let [links (map #(link-to
|
||||||
(if (= :short-url %)
|
(if (= :short-url %)
|
||||||
(:shortURL note)
|
(url (storage/create-short-url params))
|
||||||
(str (:longURL note) "/" (name %)))
|
(str (:longURL note) "/" (name %)))
|
||||||
(get-message %))
|
(get-message %))
|
||||||
[:stats :edit :export :short-url])
|
[:stats :edit :export :short-url])
|
||||||
|
|||||||
@@ -45,11 +45,18 @@
|
|||||||
(is (= note (:note get-response)))
|
(is (= note (:note get-response)))
|
||||||
(is (= (:longURL post-response) (:longURL get-response) note-url))
|
(is (= (:longURL post-response) (:longURL get-response) note-url))
|
||||||
(is (= (:shortURL post-response) (:shortURL get-response)))
|
(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 (= (:publisher get-response) pid))
|
||||||
(is (= (:title get-response) (derive-title note)))
|
(is (= (:title get-response) (derive-title note)))
|
||||||
(is (= "1" (get-in get-response [:statistics :views])))
|
(is (= "1" (get-in get-response [:statistics :views])))
|
||||||
(isnt (get-in get-response [:statistics :edited]))
|
(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"
|
(testing "creation with wrong signature"
|
||||||
(let [response (post-note note pid (get-signature pid2 psk note))]
|
(let [response (post-note note pid (get-signature pid2 psk note))]
|
||||||
(isnt (:success (:status response)))
|
(isnt (:success (:status response)))
|
||||||
|
|||||||
Reference in New Issue
Block a user