2024-04-04 09:42:40 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"amocrm/internal/controllers"
|
2024-04-09 07:42:21 +00:00
|
|
|
"amocrm/internal/initialize"
|
2024-04-04 09:42:40 +00:00
|
|
|
"amocrm/internal/repository"
|
|
|
|
"amocrm/internal/server/http"
|
|
|
|
"amocrm/internal/service"
|
2024-04-09 07:42:21 +00:00
|
|
|
"amocrm/pkg/closer"
|
2024-04-04 09:42:40 +00:00
|
|
|
"context"
|
2024-04-09 07:42:21 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
func Run(ctx context.Context, config initialize.Config, logger *zap.Logger) error {
|
2024-04-04 09:42:40 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Error("Recovered from a panic", zap.Any("error", r))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger.Info("App started", zap.Any("config", config))
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
shutdownGroup := closer.NewCloserGroup()
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
mdb, err := initialize.MongoDB(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed initialize mongo")
|
|
|
|
return err
|
|
|
|
}
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
repo := repository.NewRepository(repository.Deps{
|
|
|
|
MdbUser: mdb.Collection("amoUsers"),
|
|
|
|
})
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
svc := service.NewService(service.Deps{
|
2024-04-09 09:17:39 +00:00
|
|
|
Repository: repo,
|
|
|
|
Logger: logger,
|
|
|
|
ConnectLink: config.ConnectLink,
|
2024-04-09 07:42:21 +00:00
|
|
|
})
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
controller := controllers.NewController(controllers.Deps{
|
|
|
|
Service: svc,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
2024-04-04 09:42:40 +00:00
|
|
|
|
|
|
|
server := http.NewServer(http.ServerConfig{
|
|
|
|
Controllers: []http.Controller{
|
2024-04-09 07:42:21 +00:00
|
|
|
controller,
|
2024-04-04 09:42:40 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
2024-04-09 07:42:21 +00:00
|
|
|
if err := server.Start(config.HTTPHost + ":" + config.HTTPPort); err != nil {
|
2024-04-04 09:42:40 +00:00
|
|
|
logger.Error("Server startup error", zap.Error(err))
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
server.ListRoutes()
|
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(server.Shutdown))
|
|
|
|
shutdownGroup.Add(closer.CloserFunc(mdb.Client().Disconnect))
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
<-ctx.Done()
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer timeoutCancel()
|
|
|
|
if err := shutdownGroup.Call(timeoutCtx); err != nil {
|
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
logger.Error("Shutdown timed out", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
logger.Error("Failed to shutdown services gracefully", zap.Error(err))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
logger.Info("Application has stopped")
|
2024-04-04 09:42:40 +00:00
|
|
|
return nil
|
|
|
|
}
|