amocrm/internal/app/app.go

110 lines
2.7 KiB
Go
Raw Normal View History

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"
amoClient2 "amocrm/pkg/amoClient"
2024-04-09 07:42:21 +00:00
"amocrm/pkg/closer"
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"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
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-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
encrypt := utils.NewEncrypt(config.PubKey, config.PrivKey)
socialAithClient := pena_social_auth.NewClient(pena_social_auth.Deps{
PenaSocialAuthURL: config.PenaSocialAuthURL,
Logger: logger,
ReturnURL: config.ReturnURL,
})
amoClient := amoClient2.NewAmoClient(amoClient2.AmoDeps{
BaseApiURL: config.ApiURL,
UserInfoURL: config.UserInfoURL,
Logger: logger,
RedirectionURL: config.ReturnURL,
IntegrationID: config.IntegrationID,
IntegrationSecret: config.IntegrationSecret,
})
2024-04-09 07:42:21 +00:00
repo := repository.NewRepository(repository.Deps{
MdbUser: mdb.Collection("amoUsers"),
Tokens: mdb.Collection("tokens"),
Logger: logger,
2024-04-09 07:42:21 +00:00
})
2024-04-04 09:42:40 +00:00
2024-04-09 07:42:21 +00:00
svc := service.NewService(service.Deps{
Repository: repo,
Logger: logger,
SocialAuthClient: socialAithClient,
AmoClient: amoClient,
Encrypt: encrypt,
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
}