Created
July 6, 2011 17:35
-
-
Save edw/1067826 to your computer and use it in GitHub Desktop.
Aleph, redis, compojure
This file contains hidden or 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 cms.core | |
| (:use [clojure [pprint :only [pprint]]] | |
| [compojure core] | |
| [ring.adapter jetty] | |
| [ring.middleware reload stacktrace] | |
| [hiccup core page-helpers] | |
| [clojure.contrib json] | |
| [aleph redis] | |
| [lamina core]) | |
| (:require [compojure [route :as route]]) | |
| (:import [java.net URI])) | |
| (def *server-port*) | |
| (def env (System/getenv)) | |
| (def default-redis-config | |
| "redis://edw:XXXXPASSWORDXXXX@icefish.redistogo.com:9220/") | |
| (def redis-uri (URI. (or (get env "REDISTOGO_URL") default-redis-config))) | |
| (defn redis-uri-to-options [uri] | |
| (let [host (.getHost uri) | |
| port (.getPort uri) | |
| user-pass (.getUserInfo uri) | |
| pass (and user-pass (last (clojure.string/split user-pass #":"))) | |
| options {:host host}] | |
| (merge {:host host} | |
| (if (> port 0) {:port port} {}) | |
| (if pass {:password pass})))) | |
| (def redis (redis-client (redis-uri-to-options redis-uri))) | |
| (defn- text-response [s] | |
| {:status 200 :headers {"Content-type" "text/plain;charset=utf-8"} :body s}) | |
| (defroutes main-routes | |
| (GET "/REDIS_KEYS" [] | |
| (text-response (with-out-str (pprint @(redis [:keys "*"]))))) | |
| (GET "/REDIS/:key" [key] | |
| (text-response (with-out-str | |
| (pprint {(keyword key) @(redis [:get key])})))) | |
| ;; Not found | |
| (HEAD "*" [] {:status 404}) | |
| (ANY "*" [] {:status 404 :headers {"Content-type" "text/html"} | |
| :body "<h1>404 Error: Not found</h1>"})) | |
| (defn app [] (-> main-routes (wrap-reload '(cms.core)) (wrap-stacktrace))) | |
| (defn start-server | |
| "Returns a started server. Use (.stop *1) to stop it." | |
| [] (run-jetty (app) {:port *server-port*})) | |
| (defn -main [] | |
| (let [env (System/getenv)] | |
| (binding [*server-port* (Integer/parseInt (get env "PORT" "8080"))] | |
| (start-server)))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On executing /REDIS/foo repeatedly: