StartHTTPServer starts an HTTP server bound to 0.0.0.0 and returns a URL reachable from processes that cannot access 127.0.0.1 due to namespace isolation. It also returns a cleanup function that stops the server.
(handler http.Handler)
| 82 | // from processes that cannot access 127.0.0.1 due to namespace isolation. |
| 83 | // It also returns a cleanup function that stops the server. |
| 84 | func StartHTTPServer(handler http.Handler) (url string, stop func(), err error) { |
| 85 | l, err := net.Listen("tcp", "0.0.0.0:0") |
| 86 | if err != nil { |
| 87 | return "", nil, err |
| 88 | } |
| 89 | srv := &httptest.Server{Config: &http.Server{Handler: handler}} |
| 90 | srv.Listener = l |
| 91 | srv.Start() |
| 92 | hostIP, herr := nettestutil.NonLoopbackIPv4() |
| 93 | if herr != nil { |
| 94 | srv.Close() |
| 95 | return "", nil, herr |
| 96 | } |
| 97 | _, port, perr := net.SplitHostPort(l.Addr().String()) |
| 98 | if perr != nil { |
| 99 | srv.Close() |
| 100 | return "", nil, perr |
| 101 | } |
| 102 | return "http://" + hostIP.String() + ":" + port, func() { srv.Close() }, nil |
| 103 | } |
searching dependent graphs…