|
|
|
@ -41,119 +41,120 @@ |
|
|
|
:body content}) |
|
|
|
:body content}) |
|
|
|
|
|
|
|
|
|
|
|
(defroutes api-routes |
|
|
|
(defroutes api-routes |
|
|
|
(GET "/note" {params :params} |
|
|
|
(GET "/note" {params :params} |
|
|
|
(generate-string (api/version-manager api/get-note params))) |
|
|
|
(generate-string (api/version-manager api/get-note params))) |
|
|
|
|
|
|
|
|
|
|
|
(POST "/note" {params :params} |
|
|
|
(POST "/note" {params :params} |
|
|
|
(generate-string (api/version-manager api/post-note params))) |
|
|
|
(generate-string (api/version-manager api/post-note params))) |
|
|
|
|
|
|
|
|
|
|
|
(PUT "/note" {params :params} |
|
|
|
(PUT "/note" {params :params} |
|
|
|
(generate-string (api/version-manager api/update-note params)))) |
|
|
|
(generate-string (api/version-manager api/update-note params)))) |
|
|
|
|
|
|
|
|
|
|
|
(defroutes app-routes |
|
|
|
(defroutes app-routes |
|
|
|
(GET "/api" [] (layout :no-js (get-message :api-title) |
|
|
|
(GET "/api" [] (layout :no-js (get-message :api-title) |
|
|
|
[:article (md-to-html (slurp "API.md"))])) |
|
|
|
[:article (md-to-html (slurp "API.md"))])) |
|
|
|
|
|
|
|
|
|
|
|
(context "/api" [] |
|
|
|
(context "/api" [] |
|
|
|
#(ring.util.response/content-type (api-routes %) "application/json")) |
|
|
|
#(ring.util.response/content-type (api-routes %) "application/json")) |
|
|
|
|
|
|
|
|
|
|
|
(GET "/" [] landing-page) |
|
|
|
(GET "/" [] landing-page) |
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title/export" [year month day title] |
|
|
|
(GET "/:year/:month/:day/:title/export" [year month day title] |
|
|
|
(when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] |
|
|
|
(when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] |
|
|
|
(return-content-type "text/plain; charset=utf-8" md-text))) |
|
|
|
(return-content-type "text/plain; charset=utf-8" md-text))) |
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title/stats" [year month day title] |
|
|
|
(GET "/:year/:month/:day/:title/stats" [year month day title] |
|
|
|
(let [note-id (api/build-key year month day title)] |
|
|
|
(let [note-id (api/build-key year month day title)] |
|
|
|
(statistics-page (api/derive-title (storage/get-note note-id)) |
|
|
|
(statistics-page (api/derive-title (storage/get-note note-id)) |
|
|
|
(storage/get-note-statistics note-id)))) |
|
|
|
(storage/get-note-statistics note-id) |
|
|
|
|
|
|
|
(storage/get-publisher note-id)))) |
|
|
|
(GET "/:year/:month/:day/:title/edit" [year month day title] |
|
|
|
|
|
|
|
(let [note-id (api/build-key year month day title)] |
|
|
|
(GET "/:year/:month/:day/:title/edit" [year month day title] |
|
|
|
(note-update-page |
|
|
|
(let [note-id (api/build-key year month day title)] |
|
|
|
note-id |
|
|
|
(note-update-page |
|
|
|
(:note (api/get-note {:noteID note-id}))))) |
|
|
|
note-id |
|
|
|
|
|
|
|
(:note (api/get-note {:noteID note-id}))))) |
|
|
|
(GET "/new" [] (new-note-page |
|
|
|
|
|
|
|
(str |
|
|
|
(GET "/new" [] (new-note-page |
|
|
|
(current-timestamp) |
|
|
|
(str |
|
|
|
(storage/sign (rand-int Integer/MAX_VALUE))))) |
|
|
|
(current-timestamp) |
|
|
|
|
|
|
|
(storage/sign (rand-int Integer/MAX_VALUE))))) |
|
|
|
(GET "/:year/:month/:day/:title" [year month day title :as params] |
|
|
|
|
|
|
|
(let [params (assoc (:query-params params) |
|
|
|
(GET "/:year/:month/:day/:title" [year month day title :as params] |
|
|
|
:year year :month month :day day :title title) |
|
|
|
(let [params (assoc (:query-params params) |
|
|
|
note-id (api/build-key year month day title) |
|
|
|
:year year :month month :day day :title title) |
|
|
|
short-url (storage/create-short-url note-id params)] |
|
|
|
note-id (api/build-key year month day title) |
|
|
|
(when (storage/note-exists? note-id) |
|
|
|
short-url (storage/create-short-url note-id params)] |
|
|
|
(if (cache/has? @page-cache short-url) |
|
|
|
(when (storage/note-exists? note-id) |
|
|
|
(do |
|
|
|
(if (cache/has? @page-cache short-url) |
|
|
|
(swap! page-cache cache/hit short-url) |
|
|
|
(do |
|
|
|
(storage/increment-note-view note-id)) |
|
|
|
(swap! page-cache cache/hit short-url) |
|
|
|
(swap! page-cache cache/miss short-url |
|
|
|
(storage/increment-note-view note-id)) |
|
|
|
(note-page (api/get-note {:noteID note-id}) |
|
|
|
(swap! page-cache cache/miss short-url |
|
|
|
(api/url short-url)))) |
|
|
|
(note-page (api/get-note {:noteID note-id}) |
|
|
|
(cache/lookup @page-cache short-url)))) |
|
|
|
(api/url short-url)))) |
|
|
|
|
|
|
|
(cache/lookup @page-cache short-url)))) |
|
|
|
(GET "/:short-url" [short-url] |
|
|
|
|
|
|
|
(when-let [params (storage/resolve-url short-url)] |
|
|
|
(GET "/:short-url" [short-url] |
|
|
|
(let [{:keys [year month day title]} params |
|
|
|
(when-let [params (storage/resolve-url short-url)] |
|
|
|
rest-params (dissoc params :year :month :day :title) |
|
|
|
(let [{:keys [year month day title]} params |
|
|
|
core-url (api/url year month day title) |
|
|
|
rest-params (dissoc params :year :month :day :title) |
|
|
|
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] |
|
|
|
core-url (api/url year month day title) |
|
|
|
(redirect long-url)))) |
|
|
|
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] |
|
|
|
|
|
|
|
(redirect long-url)))) |
|
|
|
(POST "/post-note" [session note signature password] |
|
|
|
|
|
|
|
(if (and session |
|
|
|
(POST "/post-note" [session note signature password] |
|
|
|
(.startsWith session |
|
|
|
(if (and session |
|
|
|
(str (current-timestamp))) |
|
|
|
(.startsWith session |
|
|
|
(= signature (storage/sign session note))) |
|
|
|
(str (current-timestamp))) |
|
|
|
(let [pid "NoteHub" |
|
|
|
(= signature (storage/sign session note))) |
|
|
|
psk (storage/get-psk pid) |
|
|
|
(let [pid "NoteHub" |
|
|
|
params {:note note |
|
|
|
psk (storage/get-psk pid) |
|
|
|
:pid pid |
|
|
|
params {:note note |
|
|
|
:signature (storage/sign pid psk note) |
|
|
|
:pid pid |
|
|
|
:password password}] |
|
|
|
:signature (storage/sign pid psk note) |
|
|
|
(if (storage/valid-publisher? pid) |
|
|
|
:password password}] |
|
|
|
(let [resp (api/post-note params)] |
|
|
|
(if (storage/valid-publisher? pid) |
|
|
|
(if (get-in resp [:status :success]) |
|
|
|
(let [resp (api/post-note params)] |
|
|
|
(redirect (:longURL resp)) |
|
|
|
(if (get-in resp [:status :success]) |
|
|
|
(response 400))) |
|
|
|
(redirect (:longURL resp)) |
|
|
|
(response 500))) |
|
|
|
(response 400))) |
|
|
|
(response 400))) |
|
|
|
(response 500))) |
|
|
|
|
|
|
|
(response 400))) |
|
|
|
(POST "/update-note" [noteID note password] |
|
|
|
|
|
|
|
(let [pid "NoteHub" |
|
|
|
(POST "/update-note" [noteID note password] |
|
|
|
psk (storage/get-psk pid) |
|
|
|
(let [pid "NoteHub" |
|
|
|
params {:noteID noteID :note note :password password :pid pid}] |
|
|
|
psk (storage/get-psk pid) |
|
|
|
(if (storage/valid-publisher? pid) |
|
|
|
params {:noteID noteID :note note :password password :pid pid}] |
|
|
|
(let [resp (api/update-note (assoc params |
|
|
|
(if (storage/valid-publisher? pid) |
|
|
|
:signature |
|
|
|
(let [resp (api/update-note (assoc params |
|
|
|
(storage/sign pid psk noteID note password)))] |
|
|
|
:signature |
|
|
|
(if (get-in resp [:status :success]) |
|
|
|
(storage/sign pid psk noteID note password)))] |
|
|
|
(do |
|
|
|
(if (get-in resp [:status :success]) |
|
|
|
(doseq [url (storage/get-short-urls noteID)] |
|
|
|
(do |
|
|
|
(swap! page-cache cache/evict url)) |
|
|
|
(doseq [url (storage/get-short-urls noteID)] |
|
|
|
(redirect (:longURL resp))) |
|
|
|
(swap! page-cache cache/evict url)) |
|
|
|
(response 403))) |
|
|
|
(redirect (:longURL resp))) |
|
|
|
(response 500)))) |
|
|
|
(response 403))) |
|
|
|
|
|
|
|
(response 500)))) |
|
|
|
(route/resources "/") |
|
|
|
|
|
|
|
(route/not-found (response 404))) |
|
|
|
(route/resources "/") |
|
|
|
|
|
|
|
(route/not-found (response 404))) |
|
|
|
|
|
|
|
|
|
|
|
(def app |
|
|
|
(def app |
|
|
|
(let [handler (handler/site app-routes)] |
|
|
|
(let [handler (handler/site app-routes)] |
|
|
|
(fn [request] |
|
|
|
(fn [request] |
|
|
|
(let [{:keys [server-name server-port]} request |
|
|
|
(let [{:keys [server-name server-port]} request |
|
|
|
hostURL (str "https://" server-name |
|
|
|
hostURL (str "https://" server-name |
|
|
|
(when (not= 80 server-port) (str ":" server-port))) |
|
|
|
(when (not= 80 server-port) (str ":" server-port))) |
|
|
|
request (assoc-in request [:params :hostURL] hostURL)] |
|
|
|
request (assoc-in request [:params :hostURL] hostURL)] |
|
|
|
(if (get-setting :dev-mode) |
|
|
|
(if (get-setting :dev-mode) |
|
|
|
(handler request) |
|
|
|
(handler request) |
|
|
|
(try (handler request) |
|
|
|
(try (handler request) |
|
|
|
(catch Exception e |
|
|
|
(catch Exception e |
|
|
|
(do |
|
|
|
(do |
|
|
|
;TODO (log e) |
|
|
|
;TODO (log e) |
|
|
|
(response 500))))))))) |
|
|
|
(response 500))))))))) |
|
|
|
|
|
|
|
|
|
|
|
(defn -main [& [port]] |
|
|
|
(defn -main [& [port]] |
|
|
|
(jetty/run-jetty #'app |
|
|
|
(jetty/run-jetty #'app |
|
|
|
|