All Downloads are FREE. Search and download functionalities are using the official Maven repository.

webapp.fileserve-webapp.venice Maven / Gradle / Ivy

There is a newer version: 1.12.34
Show newest version
;; -----------------------------------------------------------------------------
;; File serving application
;; -----------------------------------------------------------------------------
;;

(load-module :tomcat ['tomcat :as 'tc])
(load-module :ring)
(load-module :mimetypes)


; ensure the Tomcat libs are on the classpath
(tc/check-required-libs)


(def files-base-dir (io/file-absolute (io/file "." "resources")))



;; -----------------------------------------------------------------------------
;; Ring handler
;; -----------------------------------------------------------------------------

(defn not-found-handler [request]
  { :status 404
    :headers { "Content-Type" "text/plain; charset=utf-8" }
    :body "Not Found!" })


(defn file-handler [dir request]
  (if (str/starts-with? (:uri request) "/static/")
    (let [uri      (:uri request)
          file     (io/file dir (str/strip-start uri "/static/"))
          mimetype (or (mimetypes/probe-content-type file)
                       "application/octet-stream")]
      (if (and (io/exists-file? file)
               (io/file-can-read? file)
               (io/file-within-dir? dir file))
        { :status 200
          :headers { "Content-Type" mimetype }
          :body file }  ;; may be a string, bytebuf, :InputSream, :File
        (not-found-handler request)))
    (not-found-handler request)))


;; -----------------------------------------------------------------------------
;; Middleware configuration
;; -----------------------------------------------------------------------------

(when-not (io/exists-dir? files-base-dir)
  (throw (ex :VncException
             "The file base dir ~{files-base-dir} does not exits!")))

;; 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 "/**"         not-found-handler]
             [:get "/static/**"  (partial file-handler files-base-dir)]])


(defn fileserve-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 [ [ (fileserve-servlet)
                           { :name     "fileserve-servlet"  
                             :mapping  "/*" } ] ]
                       tomcat-opts)]
  (defn stop [] (tc/shutdown server)))

(println "File server dir: " + files-base-dir)
(println)

(println "Tomcat started on port ~(:port tomcat-opts).")
(println "Open a browser:      (sh/open \"http://localhost:8080\")")
(println "Stop it by calling:  (stop)")




© 2015 - 2024 Weber Informatics LLC | Privacy Policy