Skip to content

Commit

Permalink
remove AssignedAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Feb 6, 2024
1 parent 6528764 commit 59a42aa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
17 changes: 0 additions & 17 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"crypto/tls"
"net"
"net/http"
"sync/atomic"
"time"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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))
}
8 changes: 4 additions & 4 deletions http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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() {
Expand All @@ -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")

Expand Down

0 comments on commit 59a42aa

Please sign in to comment.