How to serve static resources in Yada

182 views Asked by At

With Compojure I can serve static resources like so:

(defroutes routes
  (route/resources "/"))

Following the Yada docs I have this working:

(def server
  (listener
    ["/"
     [["hello" (as-resource "Hello World!")]
      ["test" (resource {:produces "text/plain"
                         :response "This is a test!"})]
      [true (as-resource nil)]]]
    {:port 3000}))

But how do I make Yada serve resources from the file system?

2

There are 2 answers

0
Jacob O'Bryant On BEST ANSWER

I ended up finding the answer here: Wrapping resource handlers with bidi

(ns yada-test
  (:require [yada.yada :refer [listener as-resource]]
            [bidi.ring :refer [resources]]))

(def server
  (listener
    ["/"
     [["" (resources {:prefix "public/"})]]]
    {:port 3001}))
1
Martin Harrigan On

Try the following:

(ns yada-test
  (:require [clojure.java.io :as io]
            [yada.yada :refer [listener as-resource]]]))

(def server
  (listener
    ["/"
     [["talks" (as-resource (io/file (io/resource "docs")))]
      [true (as-resource nil)]]]
    {:port 3001}))

You need to add a resource directory to your classpath and create the docs folder in there.