This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-09-01
Channels
- # announcements (6)
- # atom-editor (4)
- # babashka (59)
- # beginners (51)
- # bristol-clojurians (6)
- # calva (5)
- # cider (22)
- # clara (6)
- # cljfx (28)
- # clojure (182)
- # clojure-australia (1)
- # clojure-berlin (1)
- # clojure-dev (21)
- # clojure-europe (12)
- # clojure-italy (8)
- # clojure-nl (7)
- # clojure-norway (13)
- # clojure-uk (20)
- # clojurescript (2)
- # code-reviews (24)
- # conjure (9)
- # cursive (9)
- # datomic (35)
- # defnpodcast (45)
- # fulcro (18)
- # graalvm (3)
- # graphql (1)
- # hugsql (1)
- # jobs (3)
- # jobs-discuss (2)
- # klipse (1)
- # leiningen (1)
- # malli (5)
- # membrane (4)
- # off-topic (7)
- # portal (2)
- # reitit (30)
- # remote-jobs (1)
- # rewrite-clj (5)
- # ring (2)
- # shadow-cljs (25)
- # test-check (2)
- # tools-deps (5)
- # vrac (26)
- # xtdb (17)
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 ( "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.
(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))))
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. Yes you're right, it is bad formatting when I copy paste I guess.
Did you add the "resources" folder to the classpath before you started your application or after? Have you tried restarting the REPL?
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?
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.
@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.
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
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!