Skip to content

Commit

Permalink
update the http serve function
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Apr 21, 2024
1 parent 93ab487 commit 0c761ae
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// ServeWithListener is used to start the http server with listener
// until it is stopped.
var ServeWithListener func(server *http.Server, ln net.Listener) = serveWithListener
var ServeWithListener func(server *http.Server, ln net.Listener) = ServeWithListenerForDefault

// New returns a new http server with the handler.
func New(addr string, handler http.Handler) *http.Server {
Expand All @@ -53,7 +53,12 @@ func Serve(server *http.Server) {
"protocol", "tcp", "addr", server.Addr, "err", err)
return
}
ServeWithListener(server, ln)

if ServeWithListener != nil {
ServeWithListener(server, ln)
} else {
ServeWithListenerForDefault(server, ln)
}
}

// Start is a convenient function to start the http server with addr and handler.
Expand All @@ -66,9 +71,9 @@ func Stop(server *http.Server) {
_ = server.Shutdown(context.Background())
}

// ServeWithListener starts the http server with listener until it is stopped.
func serveWithListener(server *http.Server, ln net.Listener) {
atexit.OnExit(func() { _ = server.Shutdown(context.Background()) })
// ServeWithListenerForDefault is the default implementation to start the http server.
func ServeWithListenerForDefault(server *http.Server, ln net.Listener) {
atexit.OnExit(func() { Stop(server) })
serve(server, ln)
atexit.Execute()
atexit.Wait()
Expand Down

0 comments on commit 0c761ae

Please sign in to comment.