Skip to content

Instantly share code, notes, and snippets.

@verydapeng
Created August 15, 2015 04:01
Show Gist options
  • Save verydapeng/eee3dc25136ea98a4459 to your computer and use it in GitHub Desktop.
Save verydapeng/eee3dc25136ea98a4459 to your computer and use it in GitHub Desktop.
a generic clojure web app
(defn app
"create a new application. returns function to stop the application"
[configurations]
(let [state1 (ref {})
state2 (ref {})
background-worker (schedule #(log-app-states
(let [data1 (rest-query (:source1 configurations))
data2 (rest-query (:source2 configurations))]
(dosync (let [[s1 s2] (generate-new-states-which-is-pure data1 data2 @state1 @state2)]
(ref-set state1 s1)
(ref-set state2 s2)
[data1 data2 s1 s2])))))
ring-app (routes
(GET "/update-state1/:value" [value]
(dosync (ref-set state1 (app-specific-logic1-which-is-pure @state1 value))))
(GET "/update-state2/:value" [value]
(dosync (ref-set state2 (app-specific-logic2-which-is-pure @state2 value))))
(route/not-found "PAGE_NOT_FOUND"))
server (jetty/run-jetty ring-app {:port (int (:port configurations)) :join? false})]
;; start the background workder now
(.start background-worker)
; return a function to *stop* the application for simple debugging
#(do (.stop server)
(.stop background-worker))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment