我在Clojure,Pedestal和Boot Cljs中创建了一个站点。 我正在使用的教程是这个 http://pedestal.io/guides/hello-world-content-types
然后,我尝试将教程中未显示的图像添加到站点。我将图像a455.jpg
放到src
文件夹中。我注意到在文件build.boot
中,:resources-paths
键设置为src
文件夹。
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$
因此,此应用程序中的src
文件夹类似于常见 Web 应用程序中的public
文件夹。不是吗?
我测试了它的网络浏览器,curl和Clojure response-for
功能。该网站工作但没有图像。这是curl
结果:
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)
这就是您的应用程序
(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 foundn"})
(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))
如何使图像在网站上显示?
和其他项目放在./resources
文件夹中。 它们也可能位于子文件夹中,具体取决于 lein
或 boot
中的确切配置。
下面是一个示例 lein 项目:http://git@gitlab.com:pedestal/hello.git
与示例文件./resources/music.txt
文件。这些通常按如下方式(:require [clojure.java.io :as io])
阅读:
(slurp (io/resource "music.txt"))
正如您从src/hello/core.clj
echo-intc
中看到的那样. 您可以通过发出lein run
,然后将浏览器导航到localhost:8890