ring

yogidevbear 2018-01-16T22:14:11.000567Z

Hello everyone. I'm trying to figure out the serving of static files (e.g. css / js) within compojure. My current understanding is that I should be making use of the compojure.route/resources function. I've defined (route/resources "/") inside of my (defroutes app ... declaration. I also added (app {:uri "/public/main.css" :request-method :get}) after the defroutes function. If I try browsing to <http://localhost:12345/public/main.css>, I keep getting the route/not-found route.

seancorfield 2018-01-16T22:49:44.000678Z

You put main.css in resources/public/ in your project?

seancorfield 2018-01-16T22:50:06.000077Z

(and remove /public from the URL)

seancorfield 2018-01-16T22:50:33.000314Z

I believe the default behavior for (route/resources "/") is to assume resources/public/ as the root for assets?

seancorfield 2018-01-16T22:51:54.000515Z

> I also added (app {:uri "/public/main.css" :request-method :get}) after the defroutes function. Why did you do that @yogidevbear? Just to test in the REPL what it will do?

yogidevbear 2018-01-16T22:56:08.000338Z

I was reading through https://learnxinyminutes.com/docs/compojure/ which showed that usage

yogidevbear 2018-01-16T22:58:02.000561Z

I didn't have the public folder inside of resources. Moved it and testing again now

yogidevbear 2018-01-16T23:00:52.000057Z

So I have resources/public/main.css in my example. I have (route/resources "/") in my defroutes, and I've removed (app {:uri "/public/main.css" :request-method :get}). I'm still seeing page not found when trying to access <http://localhost:12345/public/main.css>

seancorfield 2018-01-16T23:02:20.000304Z

Remove /public from that URL.

yogidevbear 2018-01-16T23:02:47.000116Z

ah

yogidevbear 2018-01-16T23:02:57.000187Z

Bingo

yogidevbear 2018-01-16T23:03:00.000356Z

Thanks Sean 🙂

seancorfield 2018-01-16T23:03:06.000216Z

The public part is on the file system (so web-accessible stuff is in that folder and non-web-accessible resources such as config files are outside it).

yogidevbear 2018-01-16T23:03:12.000253Z

All makes sense now that it's working

seancorfield 2018-01-16T23:03:33.000317Z

You can override that behavior by specifying a configuration option map in the route/resources call.

yogidevbear 2018-01-16T23:03:43.000114Z

In particular this part https://github.com/weavejester/compojure/blob/master/src/compojure/route.clj#L22-L23

seancorfield 2018-01-16T23:04:05.000295Z

Yup, there ya go!

seancorfield 2018-01-16T23:04:38.000085Z

Actually that's for files, https://github.com/weavejester/compojure/blob/master/src/compojure/route.clj#L41 is for resources

yogidevbear 2018-01-16T23:04:40.000545Z

So which library within Clojure knows to look inside resources by default? Is that a Ring thing?

seancorfield 2018-01-16T23:06:14.000549Z

The compojure middleware defers to https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/util/response.clj#L320

seancorfield 2018-01-16T23:07:17.000227Z

and that uses <http://clojure.java.io/resource|clojure.java.io/resource> -- which looks on the classpath and the convention is that both resources and src are on your classpath in most Clojure tooling.

yogidevbear 2018-01-16T23:07:34.000354Z

TIL 🙂

seancorfield 2018-01-16T23:13:11.000214Z

In a typical web app at work, we have resources/public/assets containing css, img, and js subfolders -- and URLs have /assets/css etc -- and we have resources/templates containing HTML files that are used as templates and rendered via Selmer (our preferred templating engine -- inspired by Django).

seancorfield 2018-01-16T23:13:31.000192Z

And we also often have resources/public/favicon.ico for browsers 🙂

seancorfield 2018-01-16T23:13:55.000508Z

Other files in resources include EDN config files etc.

yogidevbear 2018-01-16T23:16:00.000166Z

And anything inside resources, but outside of public is not accessible via the browser URL field?

yogidevbear 2018-01-16T23:16:40.000323Z

e.g. resources/my.edn wouldn't be accessible from <http://example.com/my.edn|example.com/my.edn> or similar route?