refactor: defers
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dawid Wysokiński 2023-01-22 10:08:01 +01:00
parent 170e15b2c7
commit f165b02ade
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
1 changed files with 7 additions and 8 deletions

View File

@ -41,6 +41,8 @@ func New() *cli.Command {
Name: "serve",
Usage: "Runs the http server",
Action: func(c *cli.Context) error {
logger := zap.L()
db, err := internal.NewBunDB()
if err != nil {
return fmt.Errorf("internal.NewBunDB: %w", err)
@ -49,8 +51,6 @@ func New() *cli.Command {
_ = db.Close()
}()
logger := zap.L()
srv, err := newServer(serverConfig{
appVersion: c.App.Version,
logger: logger,
@ -61,16 +61,15 @@ func New() *cli.Command {
}
go runServer(srv, logger)
defer func() {
ctxShutdown, cancelShutdown := context.WithTimeout(c.Context, serverShutdownTimeout)
defer cancelShutdown()
_ = srv.Shutdown(ctxShutdown)
}()
logger.Info("Server is listening on the port "+defaultPort, zap.String("port", defaultPort))
waitForSignal(c.Context)
ctxShutdown, cancelShutdown := context.WithTimeout(c.Context, serverShutdownTimeout)
defer cancelShutdown()
if err := srv.Shutdown(ctxShutdown); err != nil {
return fmt.Errorf("srv.Shutdown: %w", err)
}
return nil
},
}