86 lines
1.6 KiB
Go
86 lines
1.6 KiB
Go
|
package app
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db"
|
||
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger"
|
||
|
"github.com/mymmrac/telego"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
type App struct {
|
||
|
Logger *zap.SugaredLogger
|
||
|
Telegram *telego.Bot
|
||
|
Config *Config
|
||
|
DB *db.Repositories
|
||
|
}
|
||
|
|
||
|
func (a *App) Start() error {
|
||
|
if err := a.loadConfig(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := a.initLogger(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if a.Config.Debug {
|
||
|
a.Logger.Debugf("loaded configuration: %+v", a.Config)
|
||
|
}
|
||
|
if err := a.migrateDB(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := a.initDB(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := a.initTelegram(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
a.Logger.Info("Vega Poker Bot is running")
|
||
|
return a.longPoll()
|
||
|
}
|
||
|
|
||
|
func (a *App) loadConfig() error {
|
||
|
config, err := LoadConfig()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
a.Config = config
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (a *App) initLogger() error {
|
||
|
var err error
|
||
|
a.Logger, err = logger.NewLogger(a.Config.Debug)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (a *App) migrateDB() error {
|
||
|
return db.Migrate(a.Config.PostgresDSN)
|
||
|
}
|
||
|
|
||
|
func (a *App) initDB() error {
|
||
|
conn, err := db.Connect(a.Config.PostgresDSN)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
a.DB = db.InitRepositories(conn)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (a *App) initTelegram() error {
|
||
|
var err error
|
||
|
a.Telegram, err = telego.NewBot(a.Config.TelegramToken, logger.WrapForTelego(a.Logger, a.Config.Debug))
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (a *App) longPoll() error {
|
||
|
updates, err := a.Telegram.UpdatesViaLongPolling(nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer a.Telegram.StopLongPolling()
|
||
|
for update := range updates {
|
||
|
fmt.Printf("Update: %+v\n", update)
|
||
|
}
|
||
|
return nil
|
||
|
}
|