diff --git a/http/server.go b/http/server.go index dc463a653a..80d46c6679 100644 --- a/http/server.go +++ b/http/server.go @@ -15,7 +15,6 @@ import ( "crypto/tls" "net" "net/http" - "sync/atomic" "time" "github.com/go-chi/chi/v5" @@ -111,11 +110,6 @@ func WithTLSKeyPath(path string) ServerOpt { // Server struct holds the Handler for the HTTP API. type Server struct { - // address is the assigned listen address for the server. - // - // The value is atomic to avoid a race condition between - // the listener starting and calling AssignedAddr. - address atomic.Value options *ServerOptions server *http.Server } @@ -143,21 +137,12 @@ func NewServer(handler http.Handler, opts ...ServerOpt) (*Server, error) { Handler: mux, } - var address atomic.Value - address.Store("") - return &Server{ - address: address, options: options, server: server, }, nil } -// AssignedAddr returns the address that was assigned to the server on calls to listen. -func (s *Server) AssignedAddr() string { - return s.address.Load().(string) -} - // Shutdown gracefully shuts down the server without interrupting any active connections. func (s *Server) Shutdown(ctx context.Context) error { return s.server.Shutdown(ctx) @@ -177,7 +162,6 @@ func (s *Server) listenAndServe() error { if err != nil { return err } - s.address.Store(listener.Addr().String()) return s.server.Serve(listener) } @@ -197,6 +181,5 @@ func (s *Server) listenAndServeTLS() error { if err != nil { return err } - s.address.Store(listener.Addr().String()) return s.server.Serve(tls.NewListener(listener, config)) } diff --git a/http/server_test.go b/http/server_test.go index 0c89506e54..4065267c26 100644 --- a/http/server_test.go +++ b/http/server_test.go @@ -102,7 +102,7 @@ func TestServerListenAndServeWithAddress(t *testing.T) { // wait for server to start <-time.After(time.Second * 1) - res, err := http.Get("http://" + srv.AssignedAddr()) + res, err := http.Get("http://127.0.0.1:30001") require.NoError(t, err) defer res.Body.Close() @@ -125,7 +125,7 @@ func TestServerListenAndServeWithTLS(t *testing.T) { // wait for server to start <-time.After(time.Second * 1) - res, err := insecureClient.Get("https://" + srv.AssignedAddr()) + res, err := insecureClient.Get("https://127.0.0.1:8443") require.NoError(t, err) defer res.Body.Close() @@ -136,7 +136,7 @@ func TestServerListenAndServeWithTLS(t *testing.T) { } func TestServerListenAndServeWithAllowedOrigins(t *testing.T) { - srv, err := NewServer(testHandler, WithAllowedOrigins("localhost")) + srv, err := NewServer(testHandler, WithAllowedOrigins("localhost"), WithAddress("127.0.0.1:30001")) require.NoError(t, err) go func() { @@ -147,7 +147,7 @@ func TestServerListenAndServeWithAllowedOrigins(t *testing.T) { // wait for server to start <-time.After(time.Second * 1) - req, err := http.NewRequest(http.MethodOptions, "http://"+srv.AssignedAddr(), nil) + req, err := http.NewRequest(http.MethodOptions, "http://127.0.0.1:30001", nil) require.NoError(t, err) req.Header.Add("origin", "localhost")