braintree / manners

A polite Go HTTP server that shuts down gracefully.
MIT License
997 stars 103 forks source link

Panic... #39

Closed zquestz closed 8 years ago

zquestz commented 8 years ago

So was giving this library a try, but I can't seem to get it working. Wrote a tiny sample app just to make sure there was nothing special in my code.

package main

import (
    "log"
    "net/http"

    "github.com/braintree/manners"
)

func main() {
    http.Handle("/hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello\n"))
    }))
    log.Fatal(manners.ListenAndServe(":8080", nil))
}

This panics and returns the following stack trace.

2016/05/19 16:49:55 http: panic serving [::1]:39150: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc8200ce000)
    /usr/lib/go/src/net/http/server.go:1389 +0xc1
panic(0x711720, 0xc82000a230)
    /usr/lib/go/src/runtime/panic.go:443 +0x4e9
github.com/braintree/manners.(*gracefulHandler).ServeHTTP(0xc8200babe0, 0x7f381b775130, 0xc8200e2000, 0xc8200de000)
    /home/quest/go/src/github.com/braintree/manners/server.go:277 +0x78
net/http.serverHandler.ServeHTTP(0xc82007a080, 0x7f381b775130, 0xc8200e2000, 0xc8200de000)
    /usr/lib/go/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc8200ce000)
    /usr/lib/go/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
    /usr/lib/go/src/net/http/server.go:2137 +0x44e
2016/05/19 16:49:57 http: panic serving [::1]:39156: runtime error: invalid memory address or nil pointer dereference

Am I doing something wrong? The docs don't wrap the Handle passed function with http.HandlerFunc, but that is required for it to compile. Has this been tested against Go 1.6?

zquestz commented 8 years ago

NOTE: this does work if I pass the handler directly into the ListenAndServe method. Just not if I use http.Handle.

ghost commented 8 years ago

I think the examples floating around for Manners might be a bit mixed up. It seems to imply you can setup the net/http library handlers as per usual and then fire manners ListenAndServe() to start the server.

It actually doesn't go looking for the default mux if you use Handle() or HandleFunc() against the http library. Something along the lines of the following seems to work if you want to do it this way -

    http.HandleFunc("/doSomething/", doSomething)
    manners.ListenAndServe(":8080", http.DefaultServeMux)