How to wait for http.ListenAndServeTLS()

378 views
Skip to first unread message

xtalk1

unread,
Aug 15, 2016, 8:33:54 PM8/15/16
to golang-nuts
If possible, I would like to write a Go program that does the following:

1. Calls http.ListenAndServe() or http.ListenAndServeTLS() (in its own goroutine, of course)

2. Waits until http.ListenAndServe() or http.ListenAndServeTLS() is accepting connections

3. Starts a web browser pointed at the host and port sent to http.ListenAndServe() or http.ListenAndServeTLS()

Steps #1 and #3 are, of course, no problem.  #2 is a little more difficult.

Of course, calling http.Serve() is an option, and works great as an alternative to the http.ListenAndServe() function.

However, replacing http.ListenAndServeTLS() presents a lot more difficulty...  I'm looking for solutions that aren't a giant copy and paste of the source, modified for my needs, and updated each time a new release of Go comes out.

Any suggestions are appreciated, thanks!

Dave Cheney

unread,
Aug 15, 2016, 10:03:27 PM8/15/16
to golang-nuts
Listen and serve is just a wrapper around serve with a provided listener. Create the listener then pass it to serve, at that point you know the socket is open and will accept connections into its backlog.

Dave Cheney

unread,
Aug 15, 2016, 10:04:28 PM8/15/16
to golang-nuts
Alternatively, make a connection to your server locally to check it is listening, the fire off the browser.

Matt Harden

unread,
Aug 16, 2016, 11:04:41 PM8/16/16
to Dave Cheney, golang-nuts
I think the point was that, while ListenAndServe is easily broken out into net.Listen and http.Serve calls, ListenAndServeTLS is a whole lot more complicated. Unfortunately there is currently no way to duplicate its functionality other than to copy the source code.

On Mon, Aug 15, 2016 at 7:04 PM Dave Cheney <da...@cheney.net> wrote:
Alternatively, make a connection to your server locally to check it is listening, the fire off the browser.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dup2...@gmail.com

unread,
Aug 17, 2016, 12:01:05 PM8/17/16
to golang-nuts
wrap listener and hack it, It maybe can work

在 2016年8月16日星期二 UTC+8上午8:33:54,xtalk1写道:
Reply all
Reply to author
Forward
0 new messages