Fix start lifecycle
This commit is contained in:
+10
-10
@@ -1,6 +1,7 @@
|
||||
package box
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"runtime"
|
||||
@@ -17,15 +18,9 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
var debugHTTPServer *http.Server
|
||||
|
||||
func applyDebugListenOption(options option.DebugOptions) {
|
||||
if debugHTTPServer != nil {
|
||||
debugHTTPServer.Close()
|
||||
debugHTTPServer = nil
|
||||
}
|
||||
func startDebugHTTPServer(options option.DebugOptions) (*http.Server, error) {
|
||||
if options.Listen == "" {
|
||||
return
|
||||
return nil, nil
|
||||
}
|
||||
r := chi.NewMux()
|
||||
r.Route("/debug", func(r chi.Router) {
|
||||
@@ -63,14 +58,19 @@ func applyDebugListenOption(options option.DebugOptions) {
|
||||
r.HandleFunc("/trace", pprof.Trace)
|
||||
})
|
||||
})
|
||||
debugHTTPServer = &http.Server{
|
||||
server := &http.Server{
|
||||
Addr: options.Listen,
|
||||
Handler: r,
|
||||
}
|
||||
listener, err := net.Listen("tcp", options.Listen)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "listen debug HTTP server")
|
||||
}
|
||||
go func() {
|
||||
err := debugHTTPServer.ListenAndServe()
|
||||
err := server.Serve(listener)
|
||||
if err != nil && !E.IsClosed(err) {
|
||||
log.Error(E.Cause(err, "serve debug HTTP server"))
|
||||
}
|
||||
}()
|
||||
return server, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user