Created
October 29, 2016 13:32
-
-
Save zehnpaard/55f186d5059a8c8c3f7e567280c518d4 to your computer and use it in GitHub Desktop.
Simple Ring Demo with Routing
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 ring-with-routing.core | |
(require | |
[ring.adapter.jetty :refer [run-jetty]] | |
[ring-with-routing.handler :as h] | |
[ring-with-routing.middleware :as m] | |
)) | |
(def app | |
(-> h/handler-with-routing | |
m/logger | |
m/req-res-displayer | |
m/wrap-params)) | |
(defonce server | |
(run-jetty #'app {:port 8080 :join? false})) |
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 ring-with-routing.handler | |
(require | |
[ring.util.response :as res] | |
)) | |
(defn main [req] | |
(-> (res/response | |
"<div> | |
<h1>Hello Web Page with Routing!</h1> | |
<p>What would you like to do?</p> | |
<p><a href='./get-form.html'>Submit a GET request</a></p> | |
<p><a href='./post-form.html'>Submit a POST request</a></p> | |
</div>") | |
(res/content-type "text/html"))) | |
(defn get-form [req] | |
(-> (res/response | |
"<div> | |
<h1>Hello GET Form!</h1> | |
<p>Submit a message with GET</p> | |
<form method=\"get\" action=\"get-submit\"> | |
<input type=\"text\" name=\"name\" /> | |
<input type=\"submit\" value\"submit\" /> | |
</form> | |
<p><a href='..'>Return to main page</p> | |
</div>") | |
(res/content-type "text/html"))) | |
(defn post-form [req] | |
(-> (res/response | |
"<div> | |
<h1>Hello POST Form!</h1> | |
<p>Submit a message with POST</p> | |
<form method=\"post\" action=\"post-submit\"> | |
<input type=\"text\" name=\"name\" /> | |
<input type=\"submit\" value\"submit\" /> | |
</form> | |
<p><a href='..'>Return to main page</p> | |
</div>") | |
(res/content-type "text/html"))) | |
(defn display-result [req] | |
(let [{:keys [params uri]} req | |
param-name (get params "name") | |
req-type (if (= uri "/get-submit") "GET" "POST")] | |
(-> (res/response | |
(str | |
"<div> | |
<h1>Hello " param-name "!</h1> | |
<p>Submitted via a " req-type " request.</p> | |
<p><a href='..'>Return to main page</p> | |
</div>")) | |
(res/content-type "text/html")))) | |
(defn not-found [req] | |
(-> (res/not-found "<h1>404 Error!</h1> | |
<b>Page not found!</b> | |
<p><a href='..'>Return to main page</p>") | |
(res/content-type "text/html"))) | |
(defn handler-with-routing [req] | |
(println "In Routing Handler!") | |
(let [{:keys [uri params request-method]} req] | |
(cond | |
(= uri "/") (main req) | |
(= uri "/get-form.html") (get-form req) | |
(= uri "/post-form.html") (post-form req) | |
(= uri "/get-submit") (display-result req) | |
(= uri "/post-submit") (display-result req) | |
:else (not-found req)))) |
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 ring-with-routing.middleware | |
(require | |
[ring.middleware.params :as mparams] | |
)) | |
(defonce log-atom (atom [])) | |
(defn logger [handler] | |
(fn [req] | |
(let [res (handler req)] | |
(swap! log-atom #(conj % {:request req :response res})) | |
res))) | |
(defn req-res-displayer [handler] | |
(fn [req] | |
(let [res (handler req)] | |
(println "\nRequest:") | |
(clojure.pprint/pprint req) | |
(println "\nResponse:") | |
(clojure.pprint/pprint res) | |
res))) | |
(def wrap-params | |
mparams/wrap-params) |
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
(defproject ring-with-routing "0.1.0-SNAPSHOT" | |
:dependencies [[org.clojure/clojure "1.7.0"] | |
[ring "1.5.0"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment