Description

Package manners provides a wrapper for a standard net/http server that ensures all active HTTP client have completed their current request before the server shuts down.

It can be used a drop-in replacement for the standard http package, or can wrap a pre-configured Server.

eg.

http.Handle("/hello", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Hello\n"))
})

log.Fatal(manners.ListenAndServe(":8080", nil))

or for a customized server:

s := manners.NewWithServer(&http.Server{
	Addr:           ":8080",
	Handler:        myHandler,
	ReadTimeout:    10 * time.Second,
	WriteTimeout:   10 * time.Second,
	MaxHeaderBytes: 1 << 20,
})
log.Fatal(s.ListenAndServe())

The server will shut down cleanly when the Close() method is called:

go func() {
	sigchan := make(chan os.Signal, 1)
	signal.Notify(sigchan, os.Interrupt, os.Kill)
	<-sigchan
	log.Info("Shutting down...")
	manners.Close()
}()

http.Handle("/hello", myHandler)
log.Fatal(manners.ListenAndServe(":8080", nil))

manners is referenced in 1 repository

github.com/yudai/gotty