Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static file from compojure and ring

I have created a project using lein and then inside the root of the project I created a directory public to place static content.

However, static content is not served as expected.

Here is defroutes:

(defroutes greeter
  (GET "/greeter/working" []
    (html
      [:html
        [:head [:tile "bla"]]
        [:body [:image "oops.jpg"]]
       ]
      )
    )
  (GET "/greeter/sayhi" [] "say hi")
  (GET "/greeter/" [] "top level")
  (route/files "/" {:root (str (System/getProperty "user.dir") "\\public")})

(defn -main []
  (run-jetty greeter {:port 3000 :join? false}))
like image 329
kostas Avatar asked Mar 24 '23 18:03

kostas


1 Answers

Make sure you know where "user.dir" is actually located. It's not your home directory, it's the working directory of your application, typically where you ran lein ring server.

I created a new Compojure project with the following handler.clj file to debug this and verify where to place public/:

(ns static-files.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(def root (str (System/getProperty "user.dir") "/public"))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/files "/" (do (println root) {:root root}))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))
like image 87
noahlz Avatar answered Apr 02 '23 03:04

noahlz