webapp.demo-webapp.venice Maven / Gradle / Ivy
;; -----------------------------------------------------------------------------
;; Demo "Hello World" Web application
;; -----------------------------------------------------------------------------
;;
(load-module :tomcat ['tomcat :as 'tc])
(load-module :ring)
(load-module :ring-mw)
(load-module :ring-util)
; ensure the Tomcat libs are on the classpath
(tc/check-required-libs)
;; -----------------------------------------------------------------------------
;; Ring handler
;; -----------------------------------------------------------------------------
(defn hello-world-handler [request]
{ :status 200
:headers { "Content-Type" "text/html; charset=utf-8" }
:body (ring-util/html-box-page "Demo" "Hello World") })
(defn test-handler [request]
{ :status 200
:headers { "Content-Type" "text/html; charset=utf-8" }
:body (ring-util/html-box-page "Demo" "Test") })
;; -----------------------------------------------------------------------------
;; Middleware configuration
;; -----------------------------------------------------------------------------
;; A route is defined by a HTTP verb, a URI filter and a handle
;; function.
;; If multiple routes match the route with the longest URI filter
;; will be chosen
(def routes [[:get "/**" hello-world-handler]
[:get "/test" test-handler]
[:get "/test/**" test-handler]])
(defn my-servlet []
(ring/create-servlet (-> (ring/match-routes routes) ; >--+
; |
;(ring-mw/mw-dump-response) ; ^ |
;(ring-mw/mw-dump-request) ; | |
(ring-mw/mw-request-counter) ; | |
(ring-mw/mw-add-session 3600) ; | |
(ring-mw/mw-print-uri) ; | |
(ring-mw/mw-debug :on)))) ; +--+
;; Tomcat server options
(def tomcat-opts { :await? false ;; do not block - return after start
:base-dir "."
:port 8080 })
;; start Tomcat (wires Tomcat with the ring servlets)
(let [server (tc/start (my-servlet) tomcat-opts)]
(defn stop [] (tc/shutdown server)))
(println "Tomcat started on port ~(:port tomcat-opts).")
(println "Open a browser: (sh/open \"http://localhost:8080\")")
(println "Stop it by calling: (stop)")