func main() {
server := &http.Server{
Addr: ":8080",
Handler: engin,
}
listenErr := make(chan error, 1)
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
listenErr <- err
}
}()
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-listenErr:
logger.Logger(context.Background()).Error("failed to listen and serve", zap.Error(err))
case <-shutdown:
}
logger.Logger(context.Background()).Info("shutting down...")
wg := &sync.WaitGroup{}
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.Logger(context.Background()).Error("failed to shutdown server", zap.Error(err))
}
}()
wg.Add(1)
wg.Wait()
}