Created
November 6, 2011 04:08
-
-
Save tensorpudding/1342462 to your computer and use it in GitHub Desktop.
compojure, ring, enlive blog webapp
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 blog.backend) | |
(def posts (atom {:1 {:title "Hello" :body "World"} })) | |
(defn get-post-title "Get title of post of given id" | |
[id] (((deref posts) (keyword id)) :title)) | |
(defn get-post-body "Get body of post of given id" | |
[id] (((deref posts) (keyword id)) :body)) | |
(defn find-unused-id "Find an unused id" | |
[posts] (loop [x 1] | |
(if (not (contains? posts (keyword (str x)))) | |
x | |
(recur (inc x))))) | |
(defn store-post! "Store the post" | |
[posttitle postbody] | |
(let [newid (find-unused-id (deref posts))] | |
(do (swap! posts #(assoc %1 (keyword (str newid)) | |
{:title posttitle :body postbody})) | |
newid))) |
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 blog.post | |
(:require [blog.backend :as backend] | |
[net.cgrand.enlive-html :as enlive] | |
[ring.util.response :as r])) | |
(defn response "Return an HTTP response for a new POST" | |
[posttitle postbody] | |
(let [postid (backend/store-post! posttitle postbody)] | |
(r/redirect (str "/view/" postid)))) |
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 blog.routes | |
(:require [compojure.core :as compojure] | |
[compojure.route :as route] | |
;; [blog.viewall :as viewall] | |
[blog.post :as post] | |
;; [blog.notfound :as notfound ] | |
[blog.view :as view] | |
[blog.new :as newpost])) | |
(compojure/defroutes main-routes | |
(compojure/GET ["/view/:id", :id #"[0-9]+"] [id] | |
;; Route for view model | |
(view/response id)) | |
;; (compojure/GET "/view/" | |
;; (viewall/response)) | |
(compojure/GET "/new" [] | |
;; Route for new model | |
(newpost/response)) | |
(compojure/POST "/new" [title body] | |
;; Route for posting | |
(post/response title body)) | |
(route/resources "/") | |
(route/not-found | |
;; (notfound/response) | |
"<h1>Page not found</h1>")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment