-
-
Save KirinDave/f3509ac3fe7859fe527d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns example.web-app | |
(:use compojure.core)) | |
;; Simple storage mechanism | |
(def *shorturls* (ref {})) | |
(defn put-url [key url] | |
(dosync (alter *shorturls* assoc key url))) | |
(defn get-url [key] | |
(get @*shorturls* key)) | |
(defn url-exists? [key] | |
(contains? @*shorturls* key)) | |
(defn handle-put-req [name newurl] | |
(let [code (if (url-exists? name) 204 201)] | |
(put-url name newurl) | |
{:status code})) | |
(defroutes bookmark-app | |
(GET "/" [] "Send me your bookmarks.") | |
(GET "/:name" [name] (if (url-exists? name) | |
{:status 301 :headers {"Location" (get-url name)}} | |
{:status 404 :body "No such bookmark."})) | |
(PUT "/:name" [name newurl] (if (and name newurl) | |
(handle-put-req name newurl) | |
{:status 400 :body "No \"newurl\" parameter provided!"}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment