clojure / ring / jetty:我使用>瘦环服务器。如何配置实例化的jetty实例? [英] clojure/ring/jetty: I am using > lein ring server. How do I configure the jetty instance that gets instantiated?

查看:152
本文介绍了clojure / ring / jetty:我使用>瘦环服务器。如何配置实例化的jetty实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我直接调用jetty处理程序时,我可以传入一个配置器,如下:

When I was calling the jetty handler directly, I was able to pass in a configurator like so:

(def header-buffer-size 8388608)
(defn start [port]
  (ring/run-jetty
   (var app)
   {:port port
    :join? false
    :host "127.0.0.1"
    :configurator
    (fn [jetty]
      (doseq [connector (.getConnectors jetty)]
        (.setHeaderBufferSize connector header-buffer-size)))}))

我不得不这样做,发布时出错。现在我重构要使用> lein环服务器直接,它从命令行调用。

I had to do this because I kept getting a FULL HEAD error when posting. Now I refactored things to use > lein ring server directly, which gets called from the command line.

> lein ring server

这使用我的project.clj中指定的一些配置:

This uses some configuration specified in my project.clj:

:ring {:handler caribou.api.core/app
       :servlet-name "caribou-api"
       :init caribou.api.core/init
       :port 33443}

再次获得FULL HEAD问题。所以我试着在里面添加配置器:

This works great, but now I am getting the FULL HEAD issue again. So I tried to add a configurator in there:

:ring {:handler caribou.api.core/app
       :servlet-name "caribou-api"
       :init caribou.api.core/init
       :configurator
       (fn [jetty]
         (doseq [connector (.getConnectors jetty)]
           (.setHeaderBufferSize connector 8388608)))
       :port 33443})

这个失败与这个stacktrace:

And this fails with this stacktrace:

Exception in thread "main" java.lang.ClassCastException: 
clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
  at ring.adapter.jetty$run_jetty.invoke(jetty.clj:66)
  at ring.server.standalone$serve$fn__833.invoke(standalone.clj:78)
  at ring.server.standalone$try_port.invoke(standalone.clj:12)
  at ring.server.standalone$serve.doInvoke(standalone.clj:75)
  at clojure.lang.RestFn.invoke(RestFn.java:423)
  at ring.server.leiningen$serve.invoke(leiningen.clj:20)

我想这必须把函数直接放在地图中,所以我在项目之外定义它(在caribou.api.core)和尝试引用它,像我做其他地方定义的函数的其余部分:

I figured this had to do with putting the function directly in the map like that, so I defined it outside the project (in caribou.api.core) and tried referring to it like I do the rest of the functions defined elsewhere:

;; in caribou/api/core.clj
(def header-buffer-size 8388608)
(defn full-head-avoidance
  [jetty]
  (doseq [connector (.getConnectors jetty)]
    (.setHeaderBufferSize connector header-buffer-size)))

;; in project.clj
:ring {:handler caribou.api.core/app
       :servlet-name "caribou-api"
       :init caribou.api.core/init
       :configurator caribou.api.core/full-head-avoidance
       :port 33443})

这旋转了应用程序,但我仍然得到413:FULL HEAD错误时,发布。有任何想法吗?谢谢!

This spins up the app, but I still get the 413: FULL HEAD error when posting. Any ideas? Thanks!

推荐答案

写在 defproject 默认,但您可以在 defproject 中使用(unquote):

Stuff written inside a defproject form is not evaluated by default, but you can use ~ (unquote) in defproject when you need it to:

(defproject foo "1.2.3"
  ...
  :some-fn-key ~(fn [& args] ...))

在这种特殊情况下, > fn 形式定义你的配置器函数(没有取消引用时,它被用作一个包含一堆符号的列表,而不是编译成一个函数)。

In this particular situation, you'd unquote the fn form defining your configurator function (which without unquoting is being used as a list containing a bunch of symbols, rather than compiled into a function).

或者,您可以在 defproject 表单之外定义配置器,并在 defproject 第二种方法,但是你需要在 defproject 中引用名称,否则它将被视为一个符号。 NB。在后一种情况下,不会产生异常,因为符号实际上是可调用的并且接受任意的参数(但是当使用地图或集合以外的东西调用时,只会返回 nil 与地图或集合,他们看自己在他们的参数)。

Alternatively, you can define the configurator outside the defproject form and refer to its name inside defproject as in your second approach, but then you need to unquote the name inside defproject -- otherwise it'll be treated as a symbol. NB. in the latter case no exception is produced, because symbols are in fact callable and accept arbitrary arguments (but only ever return nil when called with something other than a map or a set; with maps or sets, they look themselves up in their arguments).

这篇关于clojure / ring / jetty:我使用>瘦环服务器。如何配置实例化的jetty实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆