Skip to content

Instantly share code, notes, and snippets.

@KirinDave
Created October 17, 2010 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KirinDave/f3509ac3fe7859fe527d to your computer and use it in GitHub Desktop.
Save KirinDave/f3509ac3fe7859fe527d to your computer and use it in GitHub Desktop.
(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