2017-10-04 17:56:18 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package appcontext
|
|
|
|
|
|
|
|
|
|
import (
|
2017-10-07 19:58:14 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-10-18 07:03:34 +04:00
|
|
|
|
"github.com/jmoiron/sqlx"
|
2017-11-19 22:16:11 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/chatter/chatterinterface"
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/config"
|
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/connections"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/forwarder/forwarderinterface"
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/getters/gettersinterface"
|
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/migrations/migrationsinterface"
|
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/parsers/parsersinterface"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/pinner/pinnerinterface"
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/router/routerinterface"
|
2017-11-19 22:16:11 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/squader/squaderinterface"
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/talkers/talkersinterface"
|
2017-11-13 22:51:50 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/welcomer/welcomerinterface"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"lab.pztrn.name/golibs/mogrus"
|
|
|
|
|
"os"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// Context is an application context struct
|
2017-10-04 17:56:18 +04:00
|
|
|
|
type Context struct {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
Cfg *config.Config
|
2017-11-14 03:44:21 +04:00
|
|
|
|
Log *mogrus.LoggerHandler
|
2017-10-18 07:03:34 +04:00
|
|
|
|
Bot *tgbotapi.BotAPI
|
2017-11-14 03:44:21 +04:00
|
|
|
|
Forwarder forwarderinterface.ForwarderInterface
|
2017-10-18 07:03:34 +04:00
|
|
|
|
Migrations migrationsinterface.MigrationsInterface
|
|
|
|
|
Router routerinterface.RouterInterface
|
|
|
|
|
Parsers parsersinterface.ParsersInterface
|
|
|
|
|
Db *sqlx.DB
|
|
|
|
|
Talkers talkersinterface.TalkersInterface
|
|
|
|
|
Getters gettersinterface.GettersInterface
|
2017-11-13 22:51:50 +04:00
|
|
|
|
Welcomer welcomerinterface.WelcomerInterface
|
2017-11-14 03:44:21 +04:00
|
|
|
|
Pinner pinnerinterface.PinnerInterface
|
2017-11-19 22:16:11 +04:00
|
|
|
|
Chatter chatterinterface.ChatterInterface
|
|
|
|
|
Squader squaderinterface.SquaderInterface
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// Init is a initialization function for context
|
2017-10-04 17:56:18 +04:00
|
|
|
|
func (c *Context) Init() {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Cfg = config.New()
|
|
|
|
|
c.Cfg.Init()
|
2017-11-14 03:44:21 +04:00
|
|
|
|
|
|
|
|
|
l := mogrus.New()
|
|
|
|
|
l.Initialize()
|
|
|
|
|
|
|
|
|
|
log := l.CreateLogger("i2_bot")
|
|
|
|
|
log.CreateOutput("stdout", os.Stdout, true, "debug")
|
|
|
|
|
|
|
|
|
|
logFile, err := os.OpenFile(c.Cfg.Logs.LogPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug")
|
|
|
|
|
|
|
|
|
|
c.Log = log
|
|
|
|
|
|
|
|
|
|
c.Bot = connections.BotInit(c.Cfg, c.Log)
|
|
|
|
|
c.Db = connections.DBInit(c.Cfg, c.Log)
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RegisterRouterInterface registering router interface in application
|
2017-10-04 17:56:18 +04:00
|
|
|
|
func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) {
|
|
|
|
|
c.Router = ri
|
|
|
|
|
c.Router.Init()
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RegisterMigrationsInterface registering migrations interface in application
|
2017-10-06 02:56:06 +04:00
|
|
|
|
func (c *Context) RegisterMigrationsInterface(mi migrationsinterface.MigrationsInterface) {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Migrations = mi
|
|
|
|
|
c.Migrations.Init()
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RegisterParsersInterface registering parsers interface in application
|
2017-10-06 02:56:06 +04:00
|
|
|
|
func (c *Context) RegisterParsersInterface(pi parsersinterface.ParsersInterface) {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Parsers = pi
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RegisterTalkersInterface registering talkers interface in application
|
2017-10-07 02:23:25 +04:00
|
|
|
|
func (c *Context) RegisterTalkersInterface(ti talkersinterface.TalkersInterface) {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Talkers = ti
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Talkers.Init()
|
2017-10-07 02:23:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RegisterGettersInterface registering getters interface in application
|
2017-10-13 00:31:12 +04:00
|
|
|
|
func (c *Context) RegisterGettersInterface(gi gettersinterface.GettersInterface) {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Getters = gi
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Getters.Init()
|
2017-10-13 00:31:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 22:51:50 +04:00
|
|
|
|
// RegisterWelcomerInterface registering welcomer interface in application
|
|
|
|
|
func (c *Context) RegisterWelcomerInterface(wi welcomerinterface.WelcomerInterface) {
|
|
|
|
|
c.Welcomer = wi
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Welcomer.Init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterPinnerInterface registering pinner interface in application
|
|
|
|
|
func (c *Context) RegisterPinnerInterface(pi pinnerinterface.PinnerInterface) {
|
|
|
|
|
c.Pinner = pi
|
|
|
|
|
c.Pinner.Init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterForwarderInterface registers forwarder interface in application
|
|
|
|
|
func (c *Context) RegisterForwarderInterface(fi forwarderinterface.ForwarderInterface) {
|
|
|
|
|
c.Forwarder = fi
|
|
|
|
|
c.Forwarder.Init()
|
2017-11-13 22:51:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 22:16:11 +04:00
|
|
|
|
// RegisterChatterInterface registers chatter interface in application
|
|
|
|
|
func (c *Context) RegisterChatterInterface(ci chatterinterface.ChatterInterface) {
|
|
|
|
|
c.Chatter = ci
|
|
|
|
|
c.Chatter.Init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterSquaderInterface registers squader interface in application
|
|
|
|
|
func (c *Context) RegisterSquaderInterface(si squaderinterface.SquaderInterface) {
|
|
|
|
|
c.Squader = si
|
|
|
|
|
c.Squader.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RunDatabaseMigrations applies migrations on bot's startup
|
2017-10-06 02:56:06 +04:00
|
|
|
|
func (c *Context) RunDatabaseMigrations() {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
c.Migrations.SetDialect("mysql")
|
|
|
|
|
c.Migrations.Migrate()
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|