From 2600aa03a5eb72d2ed708308fd3e7472e57c44bf Mon Sep 17 00:00:00 2001 From: Bas Kloosterman Date: Thu, 8 Jun 2023 16:23:58 +0200 Subject: [PATCH] Add error reporting on http server startup failure --- dvzaservice/main.go | 5 ++++- dvzaservice/srv.go | 4 ++-- whiteboxservice/main.go | 5 ++++- whiteboxservice/srv.go | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/dvzaservice/main.go b/dvzaservice/main.go index 15c689a..f0688c5 100644 --- a/dvzaservice/main.go +++ b/dvzaservice/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net" + "net/http" "os" "os/signal" "sync" @@ -96,7 +97,9 @@ func main() { go func() { wg.Add(1) - srv.ListenAndServe() + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + panic(err) + } }() <-stop diff --git a/dvzaservice/srv.go b/dvzaservice/srv.go index c659481..8e96a6e 100644 --- a/dvzaservice/srv.go +++ b/dvzaservice/srv.go @@ -34,12 +34,12 @@ func (srv *UIService) Addr() string { return srv.srv.Addr } -func (srv *UIService) ListenAndServe() { +func (srv *UIService) ListenAndServe() error { if !srv.inited { srv.init() } log.Printf("Listening on %v\n", srv.srv.Addr) - srv.srv.ListenAndServe() + return srv.srv.ListenAndServe() } func (srv *UIService) Shutdown(ctx context.Context) error { diff --git a/whiteboxservice/main.go b/whiteboxservice/main.go index 6ebcc77..d581d83 100644 --- a/whiteboxservice/main.go +++ b/whiteboxservice/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "net" + "net/http" "os" "os/signal" "sync" @@ -103,7 +104,9 @@ func main() { go func() { wg.Add(1) - srv.ListenAndServe() + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + panic(err) + } }() <-stop diff --git a/whiteboxservice/srv.go b/whiteboxservice/srv.go index 9430a77..fe41fd8 100644 --- a/whiteboxservice/srv.go +++ b/whiteboxservice/srv.go @@ -39,12 +39,12 @@ func (srv *UIService) Addr() string { return srv.srv.Addr } -func (srv *UIService) ListenAndServe() { +func (srv *UIService) ListenAndServe() error { if !srv.inited { srv.init() } - log.Println("Listening on %v", srv.srv.Addr) - srv.srv.ListenAndServe() + log.Printf("Listening on %v\n", srv.srv.Addr) + return srv.srv.ListenAndServe() } func (srv *UIService) Shutdown(ctx context.Context) error {