15357fd3c0
yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarba
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
// main.go с graceful shutdown
|
||
package main
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
|
||
"api_bb/internal/app"
|
||
"api_bb/internal/config"
|
||
"api_bb/pkg/logger"
|
||
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
func main() {
|
||
// Загрузка конфигурации
|
||
cfg := config.Load()
|
||
|
||
// Инициализация логгера
|
||
if err := logger.Init(
|
||
os.Getenv("LOG_LEVEL"),
|
||
os.Getenv("ENVIRONMENT"),
|
||
); err != nil {
|
||
log.Printf("Failed to initialize logger: %v", err)
|
||
os.Exit(1)
|
||
}
|
||
defer logger.Sync()
|
||
|
||
// Логируем начало работы
|
||
logger.LogApplicationStart(os.Getenv("REST_API_VERSION"), os.Getenv("ENVIRONMENT"), "")
|
||
|
||
// Создание и инициализация приложения
|
||
application := app.NewApp(cfg)
|
||
if err := application.Initialize(); err != nil {
|
||
logger.Get().Fatal("failed to initialize application", zap.Error(err))
|
||
}
|
||
|
||
// Канал для graceful shutdown
|
||
done := make(chan bool, 1)
|
||
quit := make(chan os.Signal, 1)
|
||
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
|
||
|
||
// Запуск сервера в горутине
|
||
go func() {
|
||
if err := application.Start(); err != nil {
|
||
logger.Get().Fatal("failed to start server", zap.Error(err))
|
||
}
|
||
done <- true
|
||
}()
|
||
|
||
// Ожидание сигнала shutdown
|
||
<-quit
|
||
logger.Get().Info("shutdown signal received")
|
||
|
||
// Graceful shutdown приложения
|
||
if err := application.Shutdown(); err != nil {
|
||
logger.Get().Fatal("could not gracefully shutdown the application", zap.Error(err))
|
||
}
|
||
|
||
logger.LogApplicationShutdown("graceful shutdown")
|
||
<-done
|
||
} |