This commit is contained in:
lbenedar
2026-03-19 15:40:23 +03:00
parent c4f290b389
commit 88637b3f0a

View File

@@ -1,6 +1,8 @@
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
@@ -20,23 +22,40 @@ func (app *application) serve() error {
WriteTimeout: 10 * time.Second,
}
shutdownError := make(chan error)
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
app.logger.PrintInfo("caught signal", map[string]string{
"signal": s.String(),
})
os.Exit(0)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
shutdownError <- srv.Shutdown(ctx)
}()
app.logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": app.config.env,
})
return srv.ListenAndServe()
err := srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
return err
}
err = <-shutdownError
if err != nil {
return err
}
app.logger.PrintInfo("stopped server", map[string]string{
"addr": srv.Addr,
})
return nil
}