Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Last active August 7, 2016 13:19
Show Gist options
  • Select an option

  • Save gamlerhart/e217c3d5cfe32c0d9ed167abf711d3f8 to your computer and use it in GitHub Desktop.

Select an option

Save gamlerhart/e217c3d5cfe32c0d9ed167abf711d3f8 to your computer and use it in GitHub Desktop.
clojure-realoading
; Allow changing in REPL.
; In a real app this could be admin functionality / more advanced toggle
(def reload-enabled
(atom false))
(defn handler
"Most boring handler. Just returns our example email as JSON"
[request]
; When reload is enabled, then we do a full reload
(when @reload-enabled
(use 'my-friendly-emailer :reload-all))
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/write-str (mail/create-email mail/example-user))})
(use 'our-web-server)
(reset! reload-enabled true)
; In ~/.lein/profiles.clj
{:user {:plugins [[com.jakemccrary/lein-test-refresh "0.16.0"]]}}
(use 'my-friendly-emailer)
(create-email example-user)
(ns my-friendly-emailer
(:require [clojure.string :as str]))
(defn create-text
"Creates a super friendly email text"
[user]
(str
"Hello " (:fullname user) "\n"
"You like " (str/join "," (:likes user))))
(defn create-email
"Creates an email, ready to send"
[user]
{:to (:email user)
:body (create-text user)})
(def example-user
{
:fullname "Jill Awesome"
:username "jill"
:email "[email protected]"
:likes ["Sports", "Movies", "Cars"]
})
(ns my-test-app-setup
(:require
[test-server :as server]
[clojure.data.json :as json]
;Right here, we includ our app
[my-friendly-emailer :as mail]))
; This namespace depends on our app.
; When the app is changed, this file will be reloaded too by test-refresh
; Then we just swap out the request handler of the test jetty server
; Everytime this file is loaded, just swap out the request handler with the current version of the code
(reset! server/test-handler
(fn [request]
; Ok, you would put the entry of the app here =)
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/write-str (mail/create-email mail/example-user))}))
(ns our-web-server
(:require
[my-friendly-emailer :as mail]
[ring.adapter.jetty :as jetty]
[clojure.data.json :as json]))
(comment
"included dependencies, like in lein:"
[[org.clojure/data.json "0.2.6"]
[ring/ring-core "1.5.0"]
[ring/ring-jetty-adapter "1.5.0"]]
)
(defn handler
"Most boring handler. Just returns our example emails as JSON"
[request]
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/write-str (mail/create-email mail/example-user))})
(defn start-server []
(jetty/run-jetty handler {:port 3000 :join? false})
)
(def running-server (start-server))
(use 'my-friendly-emailer :reload)
(create-email example-user)
(use 'uses-emails :reload-all)
(preview-email-example)
(ns test-server
; Only depends on Jetty / Ring.
; Since that won't change, it isn't reloaded by test refresh
(:require [ring.adapter.jetty :as jetty]))
; This will be swapped out by the test setup
; That setup will have the actual app in the dependency list and therefore be reloaded by test-refresh
(def test-handler (atom (fn [request] {:status 503})))
(defn- call-latest-handler [request]
(@test-handler request))
(println "Starting test server")
(jetty/run-jetty call-latest-handler {:port 3000 :join? false})
(ns uses-emails
(:require [my-friendly-emailer :as mail]))
(defn preview-email-example []
(doseq [email (mail/create-email mail/example-user)]
(println (:body email))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment