Skip to content

Use http.DefaultServeMux as default handler #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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