Skip to content

Commit

Permalink
Use http.DefaultServeMux as default handler
Browse files Browse the repository at this point in the history
Fix for braintree#47 - makes ListenAndServe et al drop-in replacements
for the corresponding methods in net/http.
  • Loading branch information
grazzini committed Jun 12, 2017
1 parent 82a8879 commit d3f03c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
39 changes: 39 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manners
import (
"net"
"net/http"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -76,6 +77,44 @@ func TestRacyClose(t *testing.T) {
}()
}

// Tests that ListenAndServe works with the default handler
func TestDefaultHandler(t *testing.T) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello\n"))
})

go ListenAndServe(":9000", nil)

ok := waitForServer(":9000", 500*time.Millisecond)
if !ok {
t.Fatal("Couldn't start HTTP server")
}

_, err := http.Get("http://localhost:9000/")
if err != nil {
t.Fatal("Simple GET failed.")
}

Close()
}

func waitForServer(addr string, timeout time.Duration) bool {
start := time.Now()

for {
runtime.Gosched()

conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
return true
}
if time.Since(start) > timeout {
return false
}
}
}

// Tests that the server begins to shut down when told to and does not accept
// new requests once shutdown has begun
func TestShutdown(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions static.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func init() {
// ListenAndServe provides a graceful version of the function provided by the
// net/http package. Call Close() to stop the server.
func ListenAndServe(addr string, handler http.Handler) error {
if handler == nil {
handler = http.DefaultServeMux
}
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
defaultServerLock.Unlock()
return defaultServer.ListenAndServe()
Expand All @@ -26,6 +29,9 @@ func ListenAndServe(addr string, handler http.Handler) error {
// ListenAndServeTLS provides a graceful version of the function provided by the
// net/http package. Call Close() to stop the server.
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
if handler == nil {
handler = http.DefaultServeMux
}
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
defaultServerLock.Unlock()
return defaultServer.ListenAndServeTLS(certFile, keyFile)
Expand All @@ -34,6 +40,9 @@ func ListenAndServeTLS(addr string, certFile string, keyFile string, handler htt
// Serve provides a graceful version of the function provided by the net/http
// package. Call Close() to stop the server.
func Serve(l net.Listener, handler http.Handler) error {
if handler == nil {
handler = http.DefaultServeMux
}
defaultServer = NewWithServer(&http.Server{Handler: handler})
defaultServerLock.Unlock()
return defaultServer.Serve(l)
Expand Down

0 comments on commit d3f03c4

Please sign in to comment.