clojureresourcescompojure

wrap-file through a specific route


I am setting up static resources for my compojure app. I need to use wrap-file instead of wrap-resource since I need the static files to be served from the File System.

I followed this wiki and configured wrap-file

Now I am able to serve my static assets from http://localhost/static-file.css

What I want to do is to serve my static assets in a specific context http://localhost/context/static-file.css


Solution

  • With Compojure routes, it is important to remember that any route can be wrapped in middleware, not just the top level collection of routes. With that in mind, we can use Compojure's context to mount the file system routes where needed.

    (defroutes app-routes
      (GET "/" [] "Hello World")
      (context "/context" []
               (-> (route/not-found "File Not Found")
                   (wrap-file "path-to/static-files")))
      (route/not-found "Not Found"))
    
    (def app
      (-> app-routes
          (wrap-defaults site-defaults)))