webapp.two-servlet-webapp.venice Maven / Gradle / Ivy
;; -----------------------------------------------------------------------------
;; Demo with 2 servlets
;; -----------------------------------------------------------------------------
;;
(load-module :tomcat ['tomcat :as 'tc])
(load-module :ring)
; ensure the Tomcat libs are on the classpath
(tc/check-required-libs)
;; -----------------------------------------------------------------------------
;; Ring handler
;; -----------------------------------------------------------------------------
(defn get-servlet-1-info [request]
{ :status 200
:headers { "Content-Type" "text/plain; charset=utf-8" }
:body "Hello from servlet 1!" } )
(defn get-servlet-2-info [request]
{ :status 200
:headers { "Content-Type" "text/plain; charset=utf-8" }
:body "Hello from servlet 2!" } )
;; -----------------------------------------------------------------------------
;; Middleware configuration
;; -----------------------------------------------------------------------------
(def routes-1 [[:get "/servlet-1/info" get-servlet-1-info]])
(def routes-2 [[:get "/servlet-2/info" get-servlet-2-info]])
(defn servlet-1 []
(ring/create-servlet (-> (ring/match-routes routes-1) ; >--+
; |
(ring-mw/mw-dump-response) ; ^ |
(ring-mw/mw-dump-request) ; | |
(ring-mw/mw-debug :on)))) ; +--+
(defn servlet-2 []
(ring/create-servlet (-> (ring/match-routes routes-2) ; >--+
; |
(ring-mw/mw-dump-response) ; ^ |
(ring-mw/mw-dump-request) ; | |
(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 [ [ (servlet-1)
{ :name "servlet-1"
:mapping ["/servlet-1/info"] } ]
[ (servlet-2)
{ :name "servlet-2"
:mapping ["/servlet-2/info"] } ] ]
tomcat-opts)]
(defn stop [] (tc/shutdown server)))
;; -----------------------------------------------------------------------------
(println "Tomcat started on port ~(:port tomcat-opts).")
(println "Stop it by calling: (stop)")
;; -----------------------------------------------------------------------------
;; Venice HTTP Client examples
;; -----------------------------------------------------------------------------
;; GET on Servlet 1
(comment
;; run this Http client in another REPL.
(do
(load-module :http-client-j8 ['http-client-j8 :as 'hc])
(let [res (hc/send :get
"http://localhost:8080/servlet-1/info"
:headers { "Accept" "text/plain" }
:debug true) ]
(println "Status:" (:http-status res))
(println (hc/slurp-response res))))
)
;; GET on Servlet 2
(comment
;; run this Http client in another REPL.
(do
(load-module :http-client-j8 ['http-client-j8 :as 'hc])
(let [res (hc/send :get
"http://localhost:8080/servlet-2/info"
:headers { "Accept" "text/plain" }
:debug true)]
(println "Status:" (:http-status res))
(println (hc/slurp-response res))))
)