Reputation: 1501
I created a site in Clojure, Pedestal and Boot Cljs. A tutorial I was using was this http://pedestal.io/guides/hello-world-content-types
Then I was trying to add an image to the site that was not shown in the tutorial. I put the image a455.jpg
to src
folder. I noticed that in a file build.boot
a :resources-paths
key is set to src
folder.
s@lokal:~/Dropbox$ tree ~/Dropbox/clojure-boot-heists//home/s/Dropbox/clojure-boot-heists/
├── build.boot
├── favicon.ico
├── profile.boot
├── project.clj
├── src
│ ├── a455.jpg
│ ├── favicon.ico
│ └── main.clj
└── target
└── classes
--
s@lokal:~/Dropbox$ cat ~/Dropbox/clojure-boot-heists/build.boot
(set-env!
:resource-paths #{"src"}
:dependencies '[[onetom/boot-lein-generate "0.1.3" :scope "test"]
[io.pedestal/pedestal.service "0.5.1"]
[io.pedestal/pedestal.route "0.5.1"]
[io.pedestal/pedestal.jetty "0.5.1"]
[org.clojure/data.json "0.2.6"]
[org.slf4j/slf4j-simple "1.7.21"]])
(require 'boot.lein)
(boot.lein/generate)
s@lokal:~/Dropbox$
So the src
folder in this application is something like a public
folder in common web applications. Isn't it?
I tested it web browser, curl and Clojure response-for
function. The site worked but without the image. Here you are curl
results:
s@lokal:~/Dropbox$ curl -si -H "Accept: text/html" http://localhost:8890
HTTP/1.1 200 OK
Date: Mon, 14 Jan 2019 23:22:09 GMT
Strict-Transport-Security: max-age=31536000; includeSubdomains
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Type: text/html
Transfer-Encoding: chunked
Server: Jetty(9.3.8.v20160314)
<!doctype html>
<html lang='pl'>
<head>
<meta charset='utf-8'>
<title>Hi world!</title>
</head>
<body>
<img src='a455.jpg'>
</body>
</html>
--
s@lokal:~/Dropbox$ curl -si -H "Accept: text/html" http://localhost:8890/a455.jpg
HTTP/1.1 404 Not Found
Date: Mon, 14 Jan 2019 23:22:27 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Server: Jetty(9.3.8.v20160314)
And here you are the application
(ns main
(:require [clojure.data.json :as json]
[io.pedestal.http :as http]
[io.pedestal.http.route :as route]
[io.pedestal.http.content-negotiation :as conneg]))
(defn ok [body]
{:status 200 :body body})
(defn not-found []
{:status 404 :body "Not found\n"})
(defn main-for [nm]
(cond
(unmentionables nm) nil
(empty? nm) "<!doctype html>
<html lang='pl'>
<head>
<meta charset='utf-8'>
<title>Hi world!</title>
</head>
<body>
<img src='a455.jpg'>
</body>
</html>
"
:else (str "Hello, " nm "\n")))
(defn respond-main [request]
(let [nm (get-in request [:query-params :name])
resp (main-for nm)]
(if resp
(ok resp)
(not-found))))
(def supported-types ["text/html" "application/edn" "application/json" "text/plain"])
(def content-neg-intc (conneg/negotiate-content supported-types))
(defn accepted-type
[context]
(get-in context [:request :accept :field] "text/plain"))
(defn transform-content
[body content-type]
(case content-type
"text/html" body
"text/plain" body
"application/edn" (pr-str body)
"application/json" (json/write-str body)))
(defn coerce-to
[response content-type]
(-> response
(update :body transform-content content-type)
(assoc-in [:headers "Content-Type"] content-type)))
(def coerce-body
{:name ::coerce-body
:leave
(fn [context]
(cond-> context
(nil? (get-in context [:response :headers "Content-Type"]))
(update-in [:response] coerce-to (accepted-type context))))})
(def routes
(route/expand-routes
#{["/" :get [coerce-body content-neg-intc respond-main] :route-name :main]}))
(def service-map
{::http/routes routes
::http/type :jetty
::http/port 8890})
(defn start []
(http/start (http/create-server service-map)))
(defonce server (atom nil))
(defn start-dev []
(reset! server
(http/start (http/create-server
(assoc service-map
::http/join? false)))))
(defn stop-dev []
(http/stop @server))
(defn restart []
(stop-dev)
(start-dev))
How to make the image display on the site?
Upvotes: 0
Views: 307
Reputation: 29984
Typically images and other items are put in the ./resources
folder. They may also be in subfolders, depending on the exact configuration in lein
or boot
.
Here is a sample lein project: http://[email protected]:pedestal/hello.git
with a sample file ./resources/music.txt
file. These are typically read with (:require [clojure.java.io :as io])
as follows:
(slurp (io/resource "music.txt"))
as you can see from the echo-intc
in src/hello/core.clj
. You can see the file access in action by issuing a lein run
and then navigating your browser to localhost:8890
Upvotes: 0