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/server/http"
|
|
|
|
"amocrm/internal/service"
|
2024-04-11 13:58:31 +00:00
|
|
|
"amocrm/internal/workers/data_updater"
|
2024-04-12 13:18:16 +00:00
|
|
|
"amocrm/pkg/amoClient"
|
2024-04-09 07:42:21 +00:00
|
|
|
"amocrm/pkg/closer"
|
2024-04-09 12:13:21 +00:00
|
|
|
pena_social_auth "amocrm/pkg/pena-social-auth"
|
2024-04-04 09:42:40 +00:00
|
|
|
"context"
|
2024-04-09 07:42:21 +00:00
|
|
|
"errors"
|
2024-04-17 12:21:06 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
2024-04-09 07:42:21 +00:00
|
|
|
"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-17 12:21:06 +00:00
|
|
|
amoRepo, err := dal.NewAmoDal(ctx, config.PostgresCredentials)
|
2024-04-09 07:42:21 +00:00
|
|
|
if err != nil {
|
2024-04-17 12:21:06 +00:00
|
|
|
logger.Error("error init amo repo in common repo", zap.Error(err))
|
2024-04-09 07:42:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-09 12:13:21 +00:00
|
|
|
socialAithClient := pena_social_auth.NewClient(pena_social_auth.Deps{
|
|
|
|
PenaSocialAuthURL: config.PenaSocialAuthURL,
|
|
|
|
Logger: logger,
|
|
|
|
ReturnURL: config.ReturnURL,
|
|
|
|
})
|
|
|
|
|
2024-04-12 13:18:16 +00:00
|
|
|
rateLimiter := amoClient.NewRateLimiter(ctx, 6, 1500*time.Millisecond)
|
2024-04-10 10:53:19 +00:00
|
|
|
|
2024-04-12 13:18:16 +00:00
|
|
|
amoClient := amoClient.NewAmoClient(amoClient.AmoDeps{
|
2024-04-09 15:52:37 +00:00
|
|
|
BaseApiURL: config.ApiURL,
|
2024-04-10 08:54:18 +00:00
|
|
|
UserInfoURL: config.UserInfoURL,
|
2024-04-09 15:52:37 +00:00
|
|
|
Logger: logger,
|
|
|
|
RedirectionURL: config.ReturnURL,
|
|
|
|
IntegrationID: config.IntegrationID,
|
|
|
|
IntegrationSecret: config.IntegrationSecret,
|
2024-04-10 10:53:19 +00:00
|
|
|
RateLimiter: rateLimiter,
|
2024-04-09 15:52:37 +00:00
|
|
|
})
|
|
|
|
|
2024-04-09 07:42:21 +00:00
|
|
|
svc := service.NewService(service.Deps{
|
2024-04-17 12:21:06 +00:00
|
|
|
Repository: amoRepo,
|
2024-04-09 12:13:21 +00:00
|
|
|
Logger: logger,
|
|
|
|
SocialAuthClient: socialAithClient,
|
2024-04-09 15:52:37 +00:00
|
|
|
AmoClient: amoClient,
|
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
|
|
|
|
2024-04-11 13:58:31 +00:00
|
|
|
dataUpdater := data_updater.NewDataUpdaterWC(data_updater.Deps{
|
2024-04-17 12:21:06 +00:00
|
|
|
Repo: amoRepo,
|
2024-04-11 13:58:31 +00:00
|
|
|
AmoClient: amoClient,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
|
|
|
|
go dataUpdater.Start(ctx)
|
2024-04-10 16:50:41 +00:00
|
|
|
|
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))
|
2024-04-17 12:21:06 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(amoRepo.Close))
|
2024-04-10 10:53:19 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(rateLimiter.Stop))
|
2024-04-11 13:58:31 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(dataUpdater.Stop))
|
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
|
|
|
|
}
|