如何让 Compojure 监听单个 IP

发布于 07-29 08:06 字数 237 浏览 9 评论 0原文

我已经启动了一个 Compojure (Jetty) 服务器:

(defonce *server* (run-server {:host "localhost" :port 8080} "/*" (servlet routes)))

但 netstat 仍然显示它正在监听 0.0.0.0:8080,即所有 IP。

传递给运行服务器以使其侦听单个 IP 的正确参数是什么?

I have started a Compojure (Jetty) server with:

(defonce *server* (run-server {:host "localhost" :port 8080} "/*" (servlet routes)))

but netstat still shows that it is listening on 0.0.0.0:8080, i.e. all IPs.

What is the correct parameter to pass to run-server to make it listen on a single IP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇2024-08-05 08:06:19

我认为你必须修补 Compojure。 它不会对您的 :host 参数执行任何操作。 对 server/jetty.clj 进行此更改似乎有效,但我尚未对其进行彻底测试。

(defn- create-server
  "Construct a Jetty Server instance."
  [options servlets]
  (let [port     (options :port 80)
        host     (options :host "0.0.0.0")
        connector (doto (org.mortbay.jetty.bio.SocketConnector.)
                    (.setPort port)
                    (.setHost host))
        server   (doto (Server.)
                   (.addConnector connector))
        servlets (partition 2 servlets)]
    (when (or (options :ssl) (options :ssl-port))
      (add-ssl-connector! server options))
    (doseq [[url-or-path servlet] servlets]
      (add-servlet! server url-or-path servlet))
    server))
user> (run-server {:port 12346})
2009-07-22 13:48:53.999::INFO:  jetty-6.1.15
2009-07-22 13:48:54.002::INFO:  Started [email protected]:12346
nil
user> (run-server {:host "127.0.0.1" :port 12345})
2009-07-22 13:48:08.061::INFO:  jetty-6.1.15
2009-07-22 13:48:08.129::INFO:  Started [email protected]:12345

I think you're going to have to patch Compojure. It's not doing anything with your :host parameter. Making this change to server/jetty.clj seems to work, but I haven't tested it thoroughly.

(defn- create-server
  "Construct a Jetty Server instance."
  [options servlets]
  (let [port     (options :port 80)
        host     (options :host "0.0.0.0")
        connector (doto (org.mortbay.jetty.bio.SocketConnector.)
                    (.setPort port)
                    (.setHost host))
        server   (doto (Server.)
                   (.addConnector connector))
        servlets (partition 2 servlets)]
    (when (or (options :ssl) (options :ssl-port))
      (add-ssl-connector! server options))
    (doseq [[url-or-path servlet] servlets]
      (add-servlet! server url-or-path servlet))
    server))
user> (run-server {:port 12346})
2009-07-22 13:48:53.999::INFO:  jetty-6.1.15
2009-07-22 13:48:54.002::INFO:  Started [email protected]:12346
nil
user> (run-server {:host "127.0.0.1" :port 12345})
2009-07-22 13:48:08.061::INFO:  jetty-6.1.15
2009-07-22 13:48:08.129::INFO:  Started [email protected]:12345
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文