加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Clojure Pedestal root用作应用程序/八位字节流

发布时间:2020-12-14 00:32:23 所属栏目:Linux 来源:网络整理
导读:我试图在 Pedestal 0.5.1中托管静态资产和服务.我使用:: file-path指向托管文件的目录.如果我直接导??航到文件 http://localhost:8888/index.html,这可以正常工作,但如果我转到站点 http://localhost:8888的根目录,它将文件作为application / octet-stream而
我试图在 Pedestal 0.5.1中托管静态资产和服务.我使用:: file-path指向托管文件的目录.如果我直接导??航到文件 http://localhost:8888/index.html,这可以正常工作,但如果我转到站点 http://localhost:8888的根目录,它将文件作为application / octet-stream而不是text / html提供.我改编了 Hello World Sample,它有相同的行为.

SRC /程序hello_world / server.clj

(ns hello-world.server
  (:require [io.pedestal.http :as http]
            [io.pedestal.http.route :as route])
  (:gen-class))

(def routes
  (route/expand-routes [[]]))

(def service
  {:env                 :prod
   ::http/join? false
   ::http/routes routes
   ::http/file-path "/tmp/www"       
   ::http/type          :jetty
   ::http/allowed-origins {:creds true :allowed-origins (constantly true)}       
   ::http/port          8888})

(defonce runnable-service (http/create-server service))

(defn -main
  "The entry-point for 'lein run'"
  [& args]
  (println "nCreating your server...")
  (http/start runnable-service))

开始莱恩跑

$curl -i localhost:8888
HTTP/1.1 200 OK
Date: Fri,18 Nov 2016 16:02:56 GMT
Last-Modified: Fri,18 Nov 2016 15:10:22 GMT
Content-Type: application/octet-stream
Content-Length: 12
Server: Jetty(9.3.8.v20160314)

hello world

$curl -i localhost:8888/index.html
HTTP/1.1 200 OK
Date: Fri,18 Nov 2016 16:03:02 GMT
Last-Modified: Fri,18 Nov 2016 15:10:22 GMT
Content-Type: text/html
Content-Length: 12
Server: Jetty(9.3.8.v20160314)

hello world

有没有办法修复“/”路由以提供正确的内容类型?

解决方法

要获取用作目录索引的文件的正确内容类型,
将拦截器io.pedestal.http.ring-middlewares / file-info添加到
您的基座配置.

这要求您使用覆盖默认拦截器链
你自己的,所以你必须包括所有的默认拦截器
您的应用需要.

例如,您的服务可能如下所示:

(ns hello-world.service
  (:require
   [io.pedestal.http :as http]
   [io.pedestal.http.ring-middlewares :as middlewares]
   [io.pedestal.http.route :as route]
   [io.pedestal.http.route.definition :refer [defroutes]]))

(defroutes routes
  [[]])

(def service
  {::http/type :jetty
   ::http/port 8080
   ::http/interceptors [http/log-request
                        http/not-found
                        middlewares/session
                        route/query-params
                        (middlewares/file-info)  ; HERE
                        (middlewares/file "/tmp/www")
                        ;; ... insert other interceptors ...
                        (route/router #(deref #'routes) :map-tree)]})

有关您可能想要包含的其他默认拦截器的示例,
见default-interceptors.

说明

这可能在实践中并不经常出现,因为许多网络
应用程序使用处理函数来生成主页
返回一个静态文件.

对于备用解决方案,您可以为/编写路由处理程序
使用适当的方法返回index.html的内容的路由
内容类型.

Pedestal的默认拦截器堆栈包括
io.pedestal.http.ring-middlewares/file
和io.pedestal.http.ring-middlewares/content-type.

这些拦截器只包含Ring中间件函数
file-request
和content-type-response,分别.

file-request返回一个java.io.File对象作为HTTP响应.

content-type-response检查请求URI以确定
Content-Type标头的值.由于URI只是/它
默认为application / octet-stream.

相比之下,ring.middleware.file-info(不推荐使用)进行了检查
响应中实际File对象的路径.
见file-info-response.

io.pedestal.http.ring-middlewares / file-info是拦截器ring.middleware.file-info / file-info-response周围的包装器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读