Howto bind Compojure to a single IP

18 views
Skip to first unread message

l0st3d

unread,
Jul 22, 2009, 11:09:11 AM7/22/09
to Compojure
Hi,

I'm pretty new to clojure, and compojure and indeed jetty, and would
like a little help getting compojure to bind to just one IP on my
box. I have tried

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

but netstat -anp shows it is still listening on 0.0.0.0. I can't find
this mentioned anywhere, maybe it's not something most people need to
do. I guess that the options to run-server are mapped to the jetty
options in some way, and the jetty config property seems to be called
jetty.host, so I guessed that :host would work (to match jetty.port).

Any help would be much appreciated.

Many thanks

Luke Renn

unread,
Jul 22, 2009, 10:56:35 PM7/22/09
to Compojure
> I guess that the options to run-server are mapped to the jetty
> options in some way, and the jetty config property seems to be called
> jetty.host, so I guessed that :host would work (to match jetty.port).
>
> Any help would be much appreciated.

Not quite. The Jetty wrapper only reads the :port option and a few
others related to SSL. You can pass a host name with the context ("/
*" -> "http://localhost/*") but it doesn't actually use that to setup
Jetty so that doesn't really help. Setting the jetty.host system
property doesn't seem to work either. If you want it right away, you
can open up jetty.clj and change the create-server method to this:

(defn- create-server
"Construct a Jetty Server instance."
[options servlets]
(let [port (options :port 80)
connector (doto (SocketConnector.)
(.setPort port)
(.setHost (options :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))

Also add the following import to the ns at the top of the file:

(:import org.mortbay.jetty.bio.SocketConnector)

Recompile and pass a :host option as you were trying before and it'll
work.

Luke

Ed Bowler

unread,
Jul 23, 2009, 5:39:31 AM7/23/09
to comp...@googlegroups.com
That's great, many thanks Luke. I will clone the repo on git hub and
put it there when I get a chance. How do I go about turning this into
a feature request ?

Luke Renn

unread,
Jul 23, 2009, 10:19:49 AM7/23/09
to Compojure
On Jul 23, 5:39 am, Ed Bowler <ed.bow...@gmail.com> wrote:
> That's great, many thanks Luke.  I will clone the repo on git hub and
> put it there when I get a chance.  How do I go about turning this into
> a feature request ?

I didn't have time last night to fully look at this and just wanted to
give you a working solution as quickly as possible. Normally I'd make
a proper patch, push it and send James a pull request. When I try to
guess how James wants something fixed or implemented I tend to guess
wrong, so as soon as he chimes in I can push a proper patch :) I did
test this before sending it and it works fine. If you don't specify
a :host option it'll bind to all address just as before.

Luke

James Reeves

unread,
Jul 28, 2009, 11:15:54 AM7/28/09
to Compojure
On Jul 23, 3:19 pm, Luke Renn <luke.r...@gmail.com> wrote:
> I didn't have time last night to fully look at this and just wanted to
> give you a working solution as quickly as possible.  Normally I'd make
> a proper patch, push it and send James a pull request.  When I try to
> guess how James wants something fixed or implemented I tend to guess
> wrong, so as soon as he chimes in I can push a proper patch :)

Your implementation seems fine, so add it in.

Sorry for the delay in replying; I've been on vacation this week.
Currently typing this from the airport wireless ;)
Reply all
Reply to author
Forward
0 new messages