webapp.async-webapp.venice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of venice Show documentation
Show all versions of venice Show documentation
Venice, a sandboxed Lisp implemented in Java.
;; -----------------------------------------------------------------------------
;; Demo Async Service
;; -----------------------------------------------------------------------------
;;
(load-module :tomcat ['tomcat :as 'tc])
(load-module :ring)
; ensure the Tomcat libs are on the classpath
(tc/check-required-libs)
;; -----------------------------------------------------------------------------
;; Ring handler
;; -----------------------------------------------------------------------------
(defn async-demo [request]
(sleep 2_000)
{ :status 200
:headers { "Content-Type" "text/plain; charset=utf-8" }
:body (str/format "Work completed. Async: %b"
(:async? request)) } )
(defn async-fail [request]
(sleep 2_000)
(println "Handler 'async-fail' -> is failing now with an exception!")
(throw (ex :VncException "Failure in async demo!")))
;; -----------------------------------------------------------------------------
;; Middleware configuration
;; -----------------------------------------------------------------------------
(def routes [[:get "/demo" async-demo]
[:get "/fail" async-fail]])
(defn async-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 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 [ [ (async-servlet)
{ :name "async-servlet"
:mapping "/*"
:async-support true } ] ]
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 (demo)
(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/demo"
:headers { "Accept" "text/plain" }
:debug true)]
(println "Status:" (:http-status res))
(println (hc/slurp-response res))))
)