Skip to content

Instantly share code, notes, and snippets.

@raek
Created October 25, 2010 10:47
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 raek/644761 to your computer and use it in GitHub Desktop.
Save raek/644761 to your computer and use it in GitHub Desktop.
(ns se.raek.rhea.http
(:use [net.cgrand.moustache :only (app)]
[ring.adapter.jetty :only (run-jetty)]
[ring.handler.dump :only (handle-dump)]
(ring.middleware [lint :only (wrap-lint)]
[params :only (wrap-params)]
[stacktrace :only (wrap-stacktrace)])
(se.raek.rhea [auth :only (wrap-basic-auth
wrap-require-auth
reset-auth)]
[html :only (base-template
transaction-template)]
[math :only (nearest-numerator)]
[model :only (authenticate
transactions
users)])))
(defn ok [body]
{:status 200
:headers {"Content-Type" "text/html; charset=UTF-8"}
:body body})
(defn list-transactions [{:keys [user]
:as request}]
(-> (for [{:keys [from to amount]
:as transaction} @transactions]
(let [from-amount (nearest-numerator amount (count to))
to-amount (/ from-amount (count to))]
(assoc transaction
:from-name (get-in @users [from :name])
:split (contains? to from)
:to-amount to-amount
:from-amount from-amount
:change (if (= user from)
to-amount
(- from-amount)))))
transaction-template ok))
(defn new-transaction [request]
(ok "ok"))
(def realm "Rhea")
(def routes
(app wrap-stacktrace
wrap-params
(wrap-basic-auth authenticate)
(wrap-require-auth realm)
[] {:get (fn [_] (ok (base-template "Welcome to Rhea" "This is Rhea...")))}
["entries"] {:get list-transactions
:post new-transaction}
["logout"] {:get (reset-auth realm)}
["crash"] {:any (fn [_] (throw (Exception. "BANG!")))}
["dump"] {:any handle-dump}))
(defonce server
(run-jetty #'routes {:port 8080, :join? false}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment