weavejester / compojure

A concise routing library for Ring/Clojure
Eclipse Public License 1.0
4.08k stars 259 forks source link

Is it possible to define multiple resource folders? #156

Closed htmfilho closed 7 years ago

htmfilho commented 7 years ago

In my current resource folder, I have a public folder that contains images, css and js. It works properly.

I also have another folder called content, located in the same level of resource, that contains the content of the website as well as resources associated with those content, like images and documents.

I would like to consider multiple resource folders, the default one already defined and the one inside the folder content. Is it possible?

weavejester commented 7 years ago

Yes, it's certainly possible. How you go about it depends on the directory structure, and whether you want to serve files from a real path or from the classpath. Also, why do you want to do it?

The resources directory is typically on the classpath. This means that, for example, the resources/public/css/main.css file is the public/css/main.css resource. In the same way, src/foo/core.clj is the foo/core.clj resource, because the src directory is also on the classpath.

If you want to build a jar or war of your application, files in resources and src are automatically pulled in and included in the jar. So if you want a content directory, you could add this to your :resource-paths in your project file. However, you'd need a directory like content/public. You wouldn't be able to put your files in content.

Alternatively, you forget about the classpath entirely, and just serve files directly from the directory with the compojure.routes/files function. Then you would be able to have a content directory, but it wouldn't be pulled into your jar file if you made one.

htmfilho commented 7 years ago

Hi @weavejester! Thank you for your reply. The directory structure is the following:

content
content/pages
content/posts
content/public/pages/img
content/public/posts/img
resources
resources/public
resources/public/css
resources/public/fonts
resources/public/img
resources/public/js

The folder resources/publicis published using the following code:

(defroutes app-routes
  ...
  (route/resources "/"))

(def app
  (wrap-defaults app-routes ...))

My intention is to publish the folder content/public as well.

The solution with the :resource-paths seems to be more elegant, but I didn't find any documentation about it. After searching the project weavejester/compojure for the term :resource-paths I didn't get any result. Do you have examples of how to use it to define multiple resource paths?

Thank you very much in advance.

weavejester commented 7 years ago

:resource-paths is an value you can set in your Leiningen project.clj file. By default it's ["resources"], so you could change it to ["resources" "content"]. See the sample project.clj file.

However, I don't know why you just wouldn't put all the resources under the resources directory.