2024-04-04 09:42:40 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-04-26 16:26:59 +00:00
|
|
|
"amocrm/internal/brokers"
|
2024-04-04 09:42:40 +00:00
|
|
|
"amocrm/internal/controllers"
|
2024-04-09 07:42:21 +00:00
|
|
|
"amocrm/internal/initialize"
|
2024-05-06 20:30:59 +00:00
|
|
|
"amocrm/internal/repository"
|
2024-04-04 09:42:40 +00:00
|
|
|
"amocrm/internal/server/http"
|
|
|
|
"amocrm/internal/service"
|
2024-04-23 17:53:33 +00:00
|
|
|
"amocrm/internal/tools"
|
2024-04-11 13:58:31 +00:00
|
|
|
"amocrm/internal/workers/data_updater"
|
2024-04-22 07:49:46 +00:00
|
|
|
"amocrm/internal/workers/limiter"
|
2024-05-06 14:40:27 +00:00
|
|
|
"amocrm/internal/workers/post_deals_worker"
|
2024-05-04 18:38:36 +00:00
|
|
|
"amocrm/internal/workers/post_fields_worker"
|
2024-04-21 14:52:55 +00:00
|
|
|
"amocrm/internal/workers/queueUpdater"
|
|
|
|
"amocrm/internal/workers_methods"
|
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-05-06 14:40:27 +00:00
|
|
|
redisClient, err := initialize.Redis(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error init redis client", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-21 14:52:55 +00:00
|
|
|
kafka, err := initialize.KafkaConsumerInit(ctx, config)
|
|
|
|
if err != nil {
|
2024-05-06 14:40:27 +00:00
|
|
|
logger.Error("error init kafka consumer", zap.Error(err))
|
2024-04-21 14:52:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:26:59 +00:00
|
|
|
producer := brokers.NewProducer(brokers.ProducerDeps{
|
|
|
|
KafkaClient: kafka,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
|
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-22 07:49:46 +00:00
|
|
|
rateLimiter := limiter.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,
|
|
|
|
Logger: logger,
|
|
|
|
RedirectionURL: config.ReturnURL,
|
|
|
|
IntegrationID: config.IntegrationID,
|
|
|
|
IntegrationSecret: config.IntegrationSecret,
|
2024-04-10 10:53:19 +00:00
|
|
|
RateLimiter: rateLimiter,
|
2024-05-11 16:42:58 +00:00
|
|
|
AmoStorageURL: config.AmoStorageURL,
|
2024-04-09 15:52:37 +00:00
|
|
|
})
|
|
|
|
|
2024-05-06 20:30:59 +00:00
|
|
|
redisRepo := repository.NewRepository(repository.Deps{
|
|
|
|
RedisClient: redisClient,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
|
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-26 16:26:59 +00:00
|
|
|
Producer: producer,
|
2024-04-09 07:42:21 +00:00
|
|
|
})
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-05-14 21:07:39 +00:00
|
|
|
cntrlDeps := controllers.Deps{
|
2024-04-09 07:42:21 +00:00
|
|
|
Service: svc,
|
|
|
|
Logger: logger,
|
2024-04-23 17:53:33 +00:00
|
|
|
Verify: tools.NewVerify(config.IntegrationSecret, config.IntegrationID),
|
2024-05-14 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
controller := controllers.NewController(cntrlDeps)
|
|
|
|
|
|
|
|
webhookController := controllers.NewWebhookController(cntrlDeps)
|
2024-04-04 09:42:40 +00:00
|
|
|
|
2024-04-21 14:52:55 +00:00
|
|
|
workerMethods := workers_methods.NewWorkersMethods(workers_methods.Deps{
|
2024-04-17 12:21:06 +00:00
|
|
|
Repo: amoRepo,
|
2024-04-11 13:58:31 +00:00
|
|
|
AmoClient: amoClient,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
|
2024-04-21 14:52:55 +00:00
|
|
|
dataUpdater := data_updater.NewDataUpdaterWC(data_updater.Deps{
|
2024-04-26 16:26:59 +00:00
|
|
|
Logger: logger,
|
|
|
|
Producer: producer,
|
2024-04-21 14:52:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
queUpdater := queueUpdater.NewQueueUpdater(queueUpdater.Deps{
|
|
|
|
Logger: logger,
|
|
|
|
KafkaClient: kafka,
|
|
|
|
Methods: workerMethods,
|
|
|
|
})
|
|
|
|
|
2024-05-06 14:40:27 +00:00
|
|
|
dealsPoster := post_deals_worker.NewPostDealsWC(post_deals_worker.Deps{
|
2024-05-06 20:30:59 +00:00
|
|
|
AmoRepo: amoRepo,
|
|
|
|
AmoClient: amoClient,
|
|
|
|
RedisRepo: redisRepo,
|
|
|
|
Logger: logger,
|
2024-05-06 14:40:27 +00:00
|
|
|
})
|
|
|
|
|
2024-05-04 18:38:36 +00:00
|
|
|
fieldsPoster := post_fields_worker.NewPostFieldsWC(post_fields_worker.Deps{
|
2024-05-06 20:30:59 +00:00
|
|
|
AmoRepo: amoRepo,
|
|
|
|
AmoClient: amoClient,
|
|
|
|
RedisRepo: redisRepo,
|
|
|
|
Logger: logger,
|
2024-05-04 18:38:36 +00:00
|
|
|
})
|
|
|
|
|
2024-04-11 13:58:31 +00:00
|
|
|
go dataUpdater.Start(ctx)
|
2024-04-21 14:52:55 +00:00
|
|
|
go queUpdater.Start(ctx)
|
2024-05-06 14:40:27 +00:00
|
|
|
go dealsPoster.Start(ctx)
|
2024-05-04 18:38:36 +00:00
|
|
|
go fieldsPoster.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-05-14 21:07:39 +00:00
|
|
|
webhookController,
|
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-05-06 20:35:08 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(redisRepo.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-21 14:52:55 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(queUpdater.Stop))
|
2024-05-06 14:40:27 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(dealsPoster.Stop))
|
2024-05-04 18:38:36 +00:00
|
|
|
shutdownGroup.Add(closer.CloserFunc(fieldsPoster.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
|
|
|
|
}
|