Skip to content

Instantly share code, notes, and snippets.

@narkisr
Created October 5, 2010 23:06
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 narkisr/612515 to your computer and use it in GitHub Desktop.
Save narkisr/612515 to your computer and use it in GitHub Desktop.
(ns blog.server
(:import org.joda.time.DateTime)
(:gen-class)
(:require [ring.util.response :as ring-res] [ring.middleware.reload :as reload] [ring.middleware.stacktrace :as strace]
[ring.adapter.jetty :as ring-jet] [ring.middleware.file :as rfile] [ring.middleware.file-info :as rfile-info]
[compojure.route :as route] [blog.view.layout :as layout]
)
(:use
(compojure core)
(blog.persistency couchdb)
(blog.view contents comments rss recaptcha)
(blog.control validations)
(blog util config)))
(defn- ip [request]
(or ((:headers request) "x-forwarded-for") (:remote-addr request)))
(defn- year-now [] (. (DateTime.) getYear))
(def default (str "/blog/" (last layout/years)))
(defroutes blog-routes
(GET "/" [] (ring-res/redirect default))
(GET "/blog" [] (ring-res/redirect default))
(GET "/blog/" [] (ring-res/redirect default))
(GET "/blog/:year" {params :params}
(validate params #(blog (Integer/valueOf (params "year")))))
(GET "/blog/:year/:id" {params :params}
(validate params #(blog (Integer/valueOf (params "year")) (params "id")))))
(defroutes static-routes
(GET "/blog/rss.xml" [] (rss-index)))
(defroutes error-routes
(route/not-found (!404!)))
(defroutes ajax-routes
(GET "/get-form/:id" [id] (comment-form id))
(GET "/get-cap" [] (recaptcha-section-html))
(POST "/add-comment/:id" request
(let [params (:params request)]
(if (captcha-valid? (ip request) params)
(do (add-comment params) {:body (captcha-success)})
{:body (captcha-fail)}))))
(defn with-layout [handler]
(fn [request]
(when-let [response (handler request)]
(assoc response
:headers (merge (:headers response) {"Content-Type" "text/html;charset=UTF-8"})
:body (layout/page (:title response) (:uri request) (:body response))))))
(wrap! blog-routes with-layout #_strace/wrap-stacktrace)
(wrap! static-routes (rfile/wrap-file "public") rfile-info/wrap-file-info )
(defroutes all-routes blog-routes ajax-routes static-routes error-routes)
(defn -main [] (let [follower (future-call (bound-fn [] (follow-couch-changes))) server (ring-jet/run-jetty all-routes {:port 8080 :join? false})]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment