blob: b5395067598c6e1086c3c59003ce6ad1167eb41b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package manners
import (
"net"
"net/http"
"sync"
)
var (
defaultServer *GracefulServer
defaultServerLock = &sync.Mutex{}
)
func init() {
defaultServerLock.Lock()
}
// 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 {
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
defaultServerLock.Unlock()
return defaultServer.ListenAndServe()
}
// 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 {
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
defaultServerLock.Unlock()
return defaultServer.ListenAndServeTLS(certFile, keyFile)
}
// 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 {
defaultServer = NewWithServer(&http.Server{Handler: handler})
defaultServerLock.Unlock()
return defaultServer.Serve(l)
}
// Shuts down the default server used by ListenAndServe, ListenAndServeTLS and
// Serve. It returns true if it's the first time Close is called.
func Close() bool {
defaultServerLock.Lock()
return defaultServer.Close()
}
|