Last active
March 25, 2023 02:04
-
-
Save zehnpaard/2071c3f55ed319aa8528d54d90f557e3 to your computer and use it in GitHub Desktop.
Simple Ring/Compojure/Hiccup Demo
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 simple-hiccup.core | |
(require | |
[ring.adapter.jetty :refer [run-jetty]] | |
[simple-hiccup.middleware :as m] | |
[simple-hiccup.routes :as r] | |
)) | |
(def app | |
(-> r/routes | |
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 simple-hiccup.middleware | |
(require | |
[ring.middleware.params :as p] | |
)) | |
(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 p/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 simple-hiccup "0.1.0-SNAPSHOT" | |
:dependencies [[org.clojure/clojure "1.8.0"] | |
[ring "1.5.0"] | |
[compojure "1.5.1"] | |
[hiccup "1.0.5"]]) |
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 simple-hiccup.routes | |
(require | |
[compojure.core :refer [defroutes GET POST]] | |
[compojure.route :refer [not-found]] | |
[simple-hiccup.views :as v] | |
)) | |
(defroutes routes | |
(GET "/" req (v/main req)) | |
(GET "/get-form.html" req (v/get-form req)) | |
(GET "/post-form.html" req (v/post-form req)) | |
(GET "/get-submit" req (v/display-result req)) | |
(POST "/post-submit" req (v/display-result req)) | |
(not-found (v/not-found))) |
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 simple-hiccup.views | |
(require | |
[hiccup.core :refer [html h]] | |
)) | |
(defn main [req] | |
(html | |
[:div | |
[:h1 "Hello Hiccup Page with Routing!"] | |
[:p "What would you like to do?"] | |
[:p [:a {:href "./get-form.html"} "Submit a GET request"]] | |
[:p [:a {:href "./post-form.html"} "Submit a POST request"]]])) | |
(defn get-form [req] | |
(html | |
[:div | |
[:h1 "Hello GET Form!"] | |
[:p "Submit a message with GET"] | |
[:form {:method "get" :action "get-submit"} | |
[:input {:type "text" :name "name"}] | |
[:input {:type "submit" :value "submit"}]] | |
[:p [:a {:href ".."} "Return to main page"]]])) | |
(defn post-form [req] | |
(html | |
[:div | |
[:h1 "Hello POST Form!"] | |
[:p "Submit a message with POST"] | |
[:form {:method "post" :action "post-submit"} | |
[:input {:type "text" :name "name"}] | |
[:input {:type "submit" :value "submit"}]] | |
[:p [:a {:href ".."} "Return to main page"]]])) | |
(defn display-result [req] | |
(let [{:keys [params uri]} req | |
param-name (get params "name") | |
req-type (if (= uri "/get-submit") "GET" "POST")] | |
(html | |
[:div | |
[:h1 "Hello " (h param-name) "!"] | |
[:p "Submitted via a " req-type " request."] | |
[:p [:a {:href ".."} "Return to main page"]]]))) | |
(defn not-found [] | |
(html | |
[:h1 "404 Error!"] | |
[:b "Page not found!"] | |
[:p [:a {:href ".."} "Return to main page"]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why params object in the request is empty? it only shows Hello without a "Name" submitted? Does anyone know? thanks