kostas
kostas

Reputation: 2019

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

Upvotes: 4

Views: 3084

Answers (2)

Damien Mattei
Damien Mattei

Reputation: 384

the important line is :

(route/resources "/")

with leiningen the public files are in resources/public directory of your project

Upvotes: 0

noahlz
noahlz

Reputation: 10311

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

Upvotes: 7

Related Questions