Further app-ifying app.
This commit is contained in:
parent
ab9cfe7393
commit
49fed21990
1 changed files with 33 additions and 4 deletions
|
|
@ -2,22 +2,23 @@ package app
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/urfave/cli/v3"
|
||||
"go.destrealm.net/logging/v3"
|
||||
"go.destrealm.net/vfs"
|
||||
|
||||
"code.destrealm.net/zancarius/justusbot/internal/models"
|
||||
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
bot *bot
|
||||
ctx context.Context
|
||||
db *sqlx.DB
|
||||
db *models.Database
|
||||
fs vfs.VFS
|
||||
logger *logging.Log
|
||||
settings *settings.Settings
|
||||
|
|
@ -25,12 +26,31 @@ type App struct {
|
|||
cron *cron.Cron
|
||||
}
|
||||
|
||||
func NewApp(root vfs.VFS, conf *settings.Settings) *App {
|
||||
return &App{
|
||||
func NewApp(ctx context.Context, root vfs.VFS, conf *settings.Settings) (*App, error) {
|
||||
app := &App{
|
||||
ctx: ctx,
|
||||
db: models.OpenDatabase(ctx, conf),
|
||||
fs: root,
|
||||
logger: logging.Logger(),
|
||||
settings: conf,
|
||||
}
|
||||
|
||||
if err := app.db.Open(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (a *App) loadCron() error {
|
||||
a.cron = cron.New()
|
||||
a.cron.AddFunc(fmt.Sprintf("TZ=%s %s",
|
||||
a.settings.System.TimeZone,
|
||||
a.settings.DailyScripture.Schedule), a.scripture)
|
||||
|
||||
a.cron.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) loadEnv() error {
|
||||
|
|
@ -51,13 +71,22 @@ func (a *App) loadEnv() error {
|
|||
}
|
||||
}
|
||||
|
||||
//if chats, err := a.db.
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) scripture() {
|
||||
|
||||
}
|
||||
|
||||
func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
|
||||
if err := a.loadEnv(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.loadCron(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bot := NewBot(ctx, a.settings)
|
||||
bot.Run()
|
||||
|
|
|
|||
Loading…
Reference in a new issue