fix(webserver): always panic in case of an issue in webserver
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE 2023-08-12 19:22:33 +00:00
parent 45c43b44a0
commit a7b1989231
Signed by: mderasse
GPG Key ID: 55141C777B16A705
3 changed files with 8 additions and 9 deletions

View File

@ -80,7 +80,6 @@ steps:
``` {{commit.message}} ``` ``` {{commit.message}} ```
🌐 {{ build.link }} 🌐 {{ build.link }}
format: markdown
when: when:
status: status:
- failure - failure

2
go.mod
View File

@ -1,6 +1,6 @@
module git.dev.m-and-m.ovh/mderasse/gocommon module git.dev.m-and-m.ovh/mderasse/gocommon
go 1.21 go 1.21.0
require ( require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2

View File

@ -3,7 +3,6 @@ package webserver
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -25,10 +24,11 @@ type ExecuteServerParams struct {
// ExecuteServer will launche a http.Server and handle the "exit" signals and gracefully stop the server // ExecuteServer will launche a http.Server and handle the "exit" signals and gracefully stop the server
// and call the cleanup function. // and call the cleanup function.
func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerParams) error { func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerParams) {
if srv == nil { if srv == nil {
return fmt.Errorf("missing http.server params") log.Panicf("missing http.server params")
return
} }
if srv.ReadTimeout == 0 { if srv.ReadTimeout == 0 {
@ -44,6 +44,7 @@ func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerP
go func() { go func() {
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Panicf("HTTP server error: %v", err) log.Panicf("HTTP server error: %v", err)
return
} }
log.Printf("Stopped serving new connections.") log.Printf("Stopped serving new connections.")
}() }()
@ -57,16 +58,15 @@ func ExecuteServer(ctx context.Context, srv *http.Server, params *ExecuteServerP
if err := srv.Shutdown(shutdownCtx); err != nil { if err := srv.Shutdown(shutdownCtx); err != nil {
log.Panicf("HTTP shutdown error: %v", err) log.Panicf("HTTP shutdown error: %v", err)
return
} }
if params != nil && params.Cleanup != nil { if params != nil && params.Cleanup != nil {
if err := params.Cleanup(); err != nil { if err := params.Cleanup(); err != nil {
log.Printf("Impossible to cleanup correctly after shutdown. error : %v", err) log.Panicf("Impossible to cleanup correctly after shutdown. error : %v", err)
return nil return
} }
} }
log.Print("Graceful shutdown complete.") log.Print("Graceful shutdown complete.")
return nil
} }