Beginner here. I've been trying to get static resources to work, specifically to serve a css file from the resources/ directory, and I'm missing something. The path to the css file is /resources/public/styles.css. Here's my current router config:
(def app
(ring/ring-handler
(ring/router
[["/" {:get ct/home}]
["/nutrients" {:get ct/nutrients}]
["/edit-nutrient" {:get ct/edit-nutrient
:post ct/save-nutrient}]
["/formulas" {:get view/formulas}]
["/batches" {:get view/batches}]
["/dump" {:get handle-dump}]
]
{:data {:middleware [parameters/parameters-middleware
ring.middleware.reload/wrap-reload]}}
)
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/redirect-trailing-slash-handler {:method :strip})
(ring/create-default-handler)
)
))
If I evaluate (<http://clojure.java.io/resource|clojure.java.io/resource> "public/styles.css")
I get the correct path to the css file returned. But I'm getting a 404 using that path in the browser running the app.
Any help would be appreciated. Thanks.@nando Maybe your resources folder is not in your classpath ?
@admin055 I believe it is ...
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; Web Application
http-kit {:mvn/version "2.4.0"}
ring/ring-core {:mvn/version "1.8.1"}
...
Does that look right to you?
I'm wondering if a middleware is required, as it is in ring itself, but it does not seem you have applied one in your example.OK, I see where the problem lies.
Please do tell !
(ring/routes...)
must be at the same level that (ring/router...)
(def app
(ring/ring-handler
(ring/router
[["/" {:get ct/home}]
["/nutrients" {:get ct/nutrients}]
["/edit-nutrient" {:get ct/edit-nutrient
:post ct/save-nutrient}]
["/formulas" {:get view/formulas}]
["/batches" {:get view/batches}]
["/dump" {:get handle-dump}]]
{:data {:middleware [parameters/parameters-middleware
ring.middleware.reload/wrap-reload]}})
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/redirect-trailing-slash-handler {:method :strip})
(ring/create-default-handler))))
That's the way it seems to be to me.
Maybe it can be useful, I have a complete example based on @seancorfield's usermanager repo here: https://github.com/PrestanceDesign/usermanager-reitit-integrant-example
I've got rainbow parens, and they seem to both be at the same level.
Oh, that might be very helpful.
> I've got rainbow parens, and they seem to both be at the same level. Yes you're right, it is bad formatting when I copy paste I guess.
I checked if redirect-trailing-slash-handler works, and it does.
Did you add the "resources" folder to the classpath before you started your application or after? Have you tried restarting the REPL?
Yes, it has always been there. I'll restart the REPL again now ...
What I assume is that with this (ring/create-resource-handler {:path "/"})
as it is, I should be able to serve any static file from the resources directory. Do I have that right?
And that `resources/' would become the root.
Yes, correct.
Thanks for your help. I will carefully go through the usermanager example you shared and try to spot what I'm doing wrong.
OK, keep posted if you've found it. You can also put project in repo, if you want me help debug.
Thanks. I need to go out now, but will be back at it soon.
@admin055 I pushed this app to github here: https://github.com/dnando/telogenix
If you or anyone else can spot why I can't seem to get static resources working, I'd appreciate it. Again, I'm a beginner ... so it may be something really basic.
@nando I check.
@nando You must remove /public/
in your layout function in view.clj.
The default root are set to public (see the doc). https://cljdoc.org/d/metosin/reitit-ring/0.2.2/api/reitit.ring#create-resource-handler
Ok, I will try that ... again.
Ok, that worked!
I've tried that a number of times before. Have no idea why it works now, but you made my day! Thank you.
You're welcome!