16 Dec 2013, 21:55
Generic-user-small

Konrad (2 posts)

Hi I’m trying to run the liberator-service application. The home.html is not loading and http://localhost:3000/ displays “Resource not found.”.

I’ve added println statement to :exists?

:exists?
    (fn [context]
      (let [f (file (str (io/resource-path) "/home.html"))]
        (println (str (io/resource-path) "/home.html"))
        [(.exists f) {::file f}]))

the result is: /C:/Workspace/Clojure/web-devith-clojure/liberator-service/resources/public%5c/home.html

it looks like “%5c” which in URL encode is \ is added to my path. Anyone has any idea why?

15 Dec 2013, 20:49
Profile_pic_pragsmall

Dmitri Sotnikov (35 posts)

I saw this problem from a few other Windows users. The (io/resource-path) returns a java.net.URL, and it looks like on Windows toString call on the URL will produce a malformed path.

Could you try calling getFile on the resulting URL to see if that helps?

(-> (clojure.java.io/resource ".") (.getFile) (str "/home.html"))
16 Dec 2013, 21:51
Generic-user-small

Konrad (2 posts)

Thanks Dmitri, you set me in the right direction. I finally got the code working.

My first problem was that I had the following project structure: liberator-service\resources\public\home.html

Accessing just resource folder couldn’t find home.html file. I had to go public/home.html

This is my copy that works:

(defresource home
    :available-media-types ["text/html"]

    :exists?
    (fn [context]
      (let [f (file (-> (clojure.java.io/resource "public/home.html")))]

        ; sanity-check: print the resource path:
        (println (str 
                   (clojure.java.io/resource home.html" )))

        ; sanity-check: does the file exist?
        (println (.exists f))

        [(.exists f) {::file f}]))

    :handle-ok
    (fn [{{{resource :resource} :route-params} :request}]
      (file  (-> (clojure.java.io/resource "public/home.html") )))

    :last-modified
    (fn [{{{resource :resource} :route-params} :request}]
      (.lastModified (file (-> (clojure.java.io/resource "public/home.html") )))))
16 Dec 2013, 23:08
Profile_pic_pragsmall

Dmitri Sotnikov (35 posts)

Ah good to hear that worked, by the way you don’t need the -> if you only have a single operation like (clojure.java.io/resource "public/home.html"). The -> macro lets you chain expressions so you could write:

(-> (clojure.java.io/resource "public/home.html") file)

this passes the output of io/resource to file and it’s same as writing:

(file (clojure.java.io/resource "public/home.html"))
22 Feb 2014, 09:24
Portrait-robert_pragsmall

Robert Dober (20 posts)

output? That hurts for a functional language ;)

Let us say result, agreed?

01 Mar 2014, 14:10
Profile_pic_pragsmall

Dmitri Sotnikov (35 posts)

result would be more proper :)

  You must be logged in to comment