Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Web applications often need to serve static content, such as images or stylesheets. Ring provides two middleware functions to do this.
wrap-file. This serves static content from a directory on the local filesystem:
(use 'ring.middleware.file) (def app (wrap-file your-handler "/var/www/public"))
The other is
wrap-resource. This serves static content from the JVM classpath:
(use 'ring.middleware.resource) (def app (wrap-resource your-handler "public"))
If you're using a Clojure build tool like Leiningen then the non-source-file resources for a project are kept in the
resources directory. Files in this directory are automatically included in jar or war files.
So in the above example, files placed in the
resources/public directory will be served up as static files.
Often you'll want to combine
wrap-resource with other middleware, usually
(use 'ring.middleware.resource 'ring.middleware.content-type 'ring.middleware.not-modified) (def app (-> your-handler (wrap-resource "public") (wrap-content-type) (wrap-not-modified))
wrap-content-type middleware chooses a content-type based on the file extension. For instance, a file called
hello.txt would get a content-type of
wrap-not-modified middleware checks the
Last-Modified header on the response against the
If-Modified-Since header on the request. This saves bandwidth by ensuring clients don't need to download resources they've already cached.
Note that this extra middleware needs to wrap around (i.e. come after) the