ruspet_portal_back/service/telegram_service/cmd/app.go

102 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
_ "embed"
configcore "bitrix_balancer_api/core/config"
"bitrix_balancer_api/core/lib/external/googlesheets"
clientcontrollers "bitrix_balancer_api/core/lib/internally/client-controllers"
"bitrix_balancer_api/core/lib/internally/monthly_orders"
tgupdatehandler "bitrix_balancer_api/telegram_service/common/update"
"bitrix_balancer_api/telegram_service/scheduler"
"bitrix_balancer_api/telegram_service/types"
"context"
"github.com/sirupsen/logrus"
"log"
"runtime/debug"
)
//go:embed google-creds.json
var googleCredsJSON []byte
func main() {
err := initService()
if err != nil {
log.Println("🔴 failed to init service: ", err)
}
select {}
}
func initService() error {
// Получение конфигурации приложения
clientsExternal, err := getClientsExternal()
if err != nil {
return err
}
ipc, err := initInternalProvider(clientsExternal.Config, clientsExternal)
if err != nil {
return err
}
delegationChecker := scheduler.NewDelegationChecker(ipc)
delegationChecker.Start()
dailyReminder := scheduler.NewDailyReminder(ipc)
dailyReminder.Start()
ctx := context.Background()
gsClient, err := googlesheets.NewClient(ctx, googleCredsJSON)
if err != nil {
logrus.Errorf("Не удалось инициализировать Google Sheets API: %v", err)
}
moService := monthly_orders.NewMonthlyOrderService(
ipc,
gsClient,
"1F1ansQeSuAwhe3Y_mhfm4ja370k5hq67RruE-CVp83w",
"1C4Tl-AQRJENW4THzwH-7yP2Ni0ckMSdm3ndLCrdKr2Y",
)
monthlyOrderChecker := scheduler.NewMonthlyOrderChecker(ipc, moService)
monthlyOrderChecker.Start()
update := tgupdatehandler.NewUpdateHandler(ipc)
go func() {
defer handlePanic("update.GetUpdateTelegram goroutine") // Обработка паник в горутине
update.GetUpdateTelegram()
}()
return nil
}
func getClientsExternal() (clientcontrollers.ClientsExternal, error) {
options := &configcore.ConfigLoadOptions{
Redis: true,
Database: true,
Telegram: true,
Server: configcore.ExposedServiceOptions{
TelegramService: true,
},
}
return clientcontrollers.ReturnClientsExternal(options)
}
func initInternalProvider(cfg *configcore.Config, clientsExternal clientcontrollers.ClientsExternal) (*types.InternalProviderControl, error) {
return &types.InternalProviderControl{
Config: cfg,
Database: clientsExternal.DB,
RedisClient: clientsExternal.Redis,
TGBotClient: clientsExternal.Telegram,
}, nil
}
// handlePanic перехватывает паники и логирует их
func handlePanic(context string) {
if r := recover(); r != nil {
log.Printf("🚨 Panic recovered in %s: %v", context, r)
log.Printf("🚨 Stack trace:\n%s", debug.Stack())
}
}