reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
nando 2020-09-01T10:15:56.005200Z

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.

2020-09-01T11:03:24.006Z

@nando Maybe your resources folder is not in your classpath ?

nando 2020-09-01T11:10:57.008600Z

@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.

2020-09-01T11:15:05.009100Z

OK, I see where the problem lies.

nando 2020-09-01T11:16:22.010300Z

Please do tell !

2020-09-01T11:17:11.010400Z

(ring/routes...) must be at the same level that (ring/router...)

2020-09-01T11:17:19.010700Z

(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))))

nando 2020-09-01T11:19:08.012300Z

That's the way it seems to be to me.

2020-09-01T11:19:32.012700Z

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

👍 1
nando 2020-09-01T11:20:14.013100Z

I've got rainbow parens, and they seem to both be at the same level.

nando 2020-09-01T11:20:37.013600Z

Oh, that might be very helpful.

2020-09-01T11:22:19.014300Z

> 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.

nando 2020-09-01T11:24:10.014800Z

I checked if redirect-trailing-slash-handler works, and it does.

2020-09-01T11:32:49.015100Z

Did you add the "resources" folder to the classpath before you started your application or after? Have you tried restarting the REPL?

nando 2020-09-01T11:34:57.015800Z

Yes, it has always been there. I'll restart the REPL again now ...

nando 2020-09-01T11:45:17.017100Z

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?

nando 2020-09-01T11:47:18.018400Z

And that `resources/' would become the root.

2020-09-01T11:51:06.018600Z

Yes, correct.

nando 2020-09-01T11:54:58.019800Z

Thanks for your help. I will carefully go through the usermanager example you shared and try to spot what I'm doing wrong.

2020-09-01T12:05:43.021400Z

OK, keep posted if you've found it. You can also put project in repo, if you want me help debug.

nando 2020-09-01T12:07:35.022Z

Thanks. I need to go out now, but will be back at it soon.

nando 2020-09-01T16:50:06.023100Z

@admin055 I pushed this app to github here: https://github.com/dnando/telogenix

nando 2020-09-01T16:52:59.025400Z

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.

2020-09-01T18:44:49.025700Z

@nando I check.

2020-09-01T18:55:01.026500Z

@nando You must remove /public/ in your layout function in view.clj.

👍 1
2020-09-01T18:57:49.027700Z

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

nando 2020-09-01T19:33:30.028400Z

Ok, I will try that ... again.

nando 2020-09-01T19:35:35.028900Z

Ok, that worked!

nando 2020-09-01T19:36:27.029800Z

I've tried that a number of times before. Have no idea why it works now, but you made my day! Thank you.

👍 1
2020-09-01T19:37:36.030200Z

You're welcome!