Find the port bound by snap-server

haskell, haskell-snap-framework

Solution

I wrote this patch, included with `snap-server` 0.9 and above, using which you can write:

let hook dat = print $ socketPort $ head $ getStartupSockets dat
let config = setStartupHook hook $ setPort 0 mempty
httpServer config ...

Now `hook` will be called after the server is ready, and will print the port it started on.

Problem

Using snap-server's `httpServe` method, I can `setPort 0` to instruct the server to connect on the next free port. Unfortunately, once I have started the http server, I can't find any way to determine which port it actually started on. As an example, my first try started on port 2679 - is there any way to determine that number?

Original source