You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.0 KiB
131 lines
3.0 KiB
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"github.com/rs/zerolog"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"gopkg.in/tucnak/telebot.v3"
|
||
|
"gopkg.in/tucnak/telebot.v3/layout"
|
||
|
"gorm.io/driver/postgres"
|
||
|
"gorm.io/gorm"
|
||
|
"muskrat/bot"
|
||
|
"muskrat/bot/handlers"
|
||
|
"muskrat/storage"
|
||
|
"muskrat/types"
|
||
|
"muskrat/userbot"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
// Log settings
|
||
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||
|
pretty, err := strconv.ParseBool(os.Getenv("LOG_PRETTY"))
|
||
|
if err != nil {
|
||
|
pretty = false
|
||
|
}
|
||
|
if pretty {
|
||
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||
|
}
|
||
|
switch os.Getenv("LOG_LEVEL") {
|
||
|
case "DISABLED":
|
||
|
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||
|
case "PANIC":
|
||
|
zerolog.SetGlobalLevel(zerolog.PanicLevel)
|
||
|
case "FATAL":
|
||
|
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
||
|
case "ERROR":
|
||
|
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
||
|
case "WARN":
|
||
|
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||
|
case "DEBUG":
|
||
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||
|
case "TRACE":
|
||
|
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
||
|
default:
|
||
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||
|
}
|
||
|
|
||
|
// Postgres
|
||
|
storage.DB, err = gorm.Open(postgres.Open(os.Getenv("POSTGRES_DSN")), &gorm.Config{})
|
||
|
if err != nil {
|
||
|
log.Fatal().Err(err).Send()
|
||
|
}
|
||
|
// Connection pools
|
||
|
db, err := storage.DB.DB()
|
||
|
if err != nil {
|
||
|
log.Fatal().Err(err).Send()
|
||
|
}
|
||
|
db.SetMaxIdleConns(10)
|
||
|
db.SetMaxOpenConns(100)
|
||
|
db.SetConnMaxLifetime(time.Hour)
|
||
|
|
||
|
err = storage.DB.AutoMigrate(&types.Account{})
|
||
|
if err != nil {
|
||
|
log.Fatal().Err(err).Send()
|
||
|
}
|
||
|
|
||
|
// Redis
|
||
|
redisURI, err := redis.ParseURL(os.Getenv("REDIS_URI"))
|
||
|
if err != nil {
|
||
|
log.Fatal().Str("module", "redis").Err(err).Send()
|
||
|
}
|
||
|
storage.Redis = redis.NewClient(redisURI)
|
||
|
defer storage.Redis.Close()
|
||
|
err = storage.Redis.Ping(context.TODO()).Err()
|
||
|
if err != nil {
|
||
|
log.Fatal().Str("module", "redis").Err(err).Send()
|
||
|
}
|
||
|
|
||
|
// Userbots
|
||
|
accounts := make([]types.Account, 0)
|
||
|
storage.DB.Find(&accounts)
|
||
|
|
||
|
userbot.Userbots = make(map[int64]*userbot.Userbot)
|
||
|
|
||
|
for _, account := range accounts {
|
||
|
current := account
|
||
|
|
||
|
userbot.Userbots[account.ID] = &userbot.Userbot{
|
||
|
Account: ¤t,
|
||
|
}
|
||
|
|
||
|
err = userbot.Userbots[account.ID].Start(context.Background())
|
||
|
if err != nil {
|
||
|
storage.DB.Unscoped().Delete(¤t)
|
||
|
userbot.Userbots[account.ID].Stop()
|
||
|
delete(userbot.Userbots, account.ID)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Layout
|
||
|
bot.Layout, err = layout.New("assets/layout.yml")
|
||
|
if err != nil {
|
||
|
log.Fatal().Str("module", "bot").Err(err).Send()
|
||
|
}
|
||
|
|
||
|
// Bot
|
||
|
bot.Bot, err = telebot.NewBot(bot.Layout.Settings())
|
||
|
if err != nil {
|
||
|
log.Fatal().Str("module", "bot").Err(err).Send()
|
||
|
}
|
||
|
|
||
|
// Middlewares
|
||
|
bot.Bot.Use(bot.Layout.Middleware("ru"))
|
||
|
|
||
|
// Handlers
|
||
|
bot.Bot.Handle("/add", handlers.Add)
|
||
|
bot.Bot.Handle("/delete", handlers.Delete)
|
||
|
bot.Bot.Handle("/adel", handlers.AdminDelete)
|
||
|
bot.Bot.Handle("\frules_accept", handlers.Accept)
|
||
|
bot.Bot.Handle("\faccount", handlers.Account)
|
||
|
bot.Bot.Handle("\fedit", handlers.Edit)
|
||
|
bot.Bot.Handle("\fdelete", handlers.DeleteButton)
|
||
|
|
||
|
bot.Bot.Handle(telebot.OnText, handlers.Router)
|
||
|
|
||
|
bot.Bot.Start()
|
||
|
}
|