Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/appcontext/appcontext.go

73 lines
2.0 KiB
Go
Raw Normal View History

2017-10-04 17:56:18 +04:00
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package appcontext
import (
2017-10-18 07:03:34 +04:00
// 3rd-party
"github.com/go-telegram-bot-api/telegram-bot-api"
2017-10-18 07:03:34 +04:00
"github.com/jmoiron/sqlx"
// local
"../config"
"../connections"
2017-10-04 17:56:18 +04:00
// interfaces
2017-10-18 07:03:34 +04:00
"../getters/gettersinterface"
"../migrations/migrationsinterface"
"../parsers/parsersinterface"
"../router/routerinterface"
"../talkers/talkersinterface"
2017-10-04 17:56:18 +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
Bot *tgbotapi.BotAPI
Migrations migrationsinterface.MigrationsInterface
Router routerinterface.RouterInterface
Parsers parsersinterface.ParsersInterface
Db *sqlx.DB
Talkers talkersinterface.TalkersInterface
Getters gettersinterface.GettersInterface
2017-10-04 17:56:18 +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()
c.Bot = connections.BotInit(c.Cfg)
2017-10-04 17:56:18 +04:00
c.Db = connections.DBInit(c.Cfg)
}
// 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()
}
// RegisterMigrationsInterface registering migrations interface in application
func (c *Context) RegisterMigrationsInterface(mi migrationsinterface.MigrationsInterface) {
2017-10-18 07:03:34 +04:00
c.Migrations = mi
c.Migrations.Init()
}
// RegisterParsersInterface registering parsers interface in application
func (c *Context) RegisterParsersInterface(pi parsersinterface.ParsersInterface) {
2017-10-18 07:03:34 +04:00
c.Parsers = pi
}
// RegisterTalkersInterface registering talkers interface in application
func (c *Context) RegisterTalkersInterface(ti talkersinterface.TalkersInterface) {
2017-10-18 07:03:34 +04:00
c.Talkers = ti
}
// 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-10-13 00:31:12 +04:00
}
// RunDatabaseMigrations applies migrations on bot's startup
func (c *Context) RunDatabaseMigrations() {
2017-10-18 07:03:34 +04:00
c.Migrations.SetDialect("mysql")
c.Migrations.Migrate()
}