Fork me on GitHub
#reitit
<
2020-09-01
>
nando10:09:56

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.

Michaël Salihi11:09:24

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

nando11:09:57

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

Michaël Salihi11:09:05

OK, I see where the problem lies.

nando11:09:22

Please do tell !

Michaël Salihi11:09:11

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

Michaël Salihi11:09:19

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

nando11:09:08

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

Michaël Salihi11:09:32

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

👍 3
nando11:09:14

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

nando11:09:37

Oh, that might be very helpful.

Michaël Salihi11:09:19

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

nando11:09:10

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

Michaël Salihi11:09:49

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

nando11:09:57

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

nando11:09:17

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?

nando11:09:18

And that `resources/' would become the root.

nando11:09:58

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

Michaël Salihi12:09:43

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

nando12:09:35

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

nando16:09:59

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.

Michaël Salihi18:09:01

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

👍 3
nando19:09:30

Ok, I will try that ... again.

nando19:09:35

Ok, that worked!

nando19:09:27

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

👍 3