如何让Compojure听一个IP

时间:2022-04-16 22:46:51

I have started a Compojure (Jetty) server with:

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

(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.

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

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

传递给run-server以使其在单个IP上侦听的正确参数是什么?

1 个解决方案

#1


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.

我想你将不得不修补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 SocketConnector@0.0.0.0: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 SocketConnector@127.0.0.1:12345

#1


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.

我想你将不得不修补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 SocketConnector@0.0.0.0: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 SocketConnector@127.0.0.1:12345