diff --git a/cmd/dummy/main.go b/cmd/dummy/main.go index affc168..42ee55b 100644 --- a/cmd/dummy/main.go +++ b/cmd/dummy/main.go @@ -2,9 +2,14 @@ package main import ( "context" + "errors" "flag" "fmt" + "net/http" "os" + "os/signal" + "syscall" + "time" "github.com/cristalhq/acmd" @@ -49,7 +54,24 @@ func run() error { h := server.NewHandlers(api, l) s := server.NewServer(cfg.Server, l, h) - return s.Run() + go func() { + if err := s.Run(); !errors.Is(err, http.ErrServerClosed) { + l.Logger.Err(err).Msg("run server") + } + }() + + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) + + x := <-interrupt + l.Info().Msgf("received `%v`", x) + + const timeout = 5 * time.Second + + ctx, cancelFunc := context.WithTimeout(ctx, timeout) + defer cancelFunc() + + return s.Stop(ctx) }, }, } diff --git a/internal/api/build_test.go b/internal/api/build_test.go index d140785..c039ed0 100644 --- a/internal/api/build_test.go +++ b/internal/api/build_test.go @@ -2,10 +2,10 @@ package api_test import ( "fmt" - "github.com/go-dummy/openapi" "strconv" "testing" + "github.com/go-dummy/openapi" "github.com/stretchr/testify/require" "github.com/go-dummy/dummy/internal/api" diff --git a/internal/server/server.go b/internal/server/server.go index 0edbae6..2670fad 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,6 +1,7 @@ package server import ( + "context" "net/http" "github.com/go-dummy/dummy/internal/config" @@ -47,3 +48,7 @@ func (s *Server) Run() error { return nil } + +func (s *Server) Stop(ctx context.Context) error { + return s.Server.Shutdown(ctx) +}