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}} ```
🌐 {{ build.link }}
format: markdown
when:
status:
- failure

2
go.mod
View File

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

View File

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