2022-11-16 11:23:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-18 19:28:13 +00:00
|
|
|
"strconv"
|
2022-11-18 16:37:30 +00:00
|
|
|
|
2022-11-30 18:15:47 +00:00
|
|
|
"github.com/strimertul/strimertul/twitch"
|
|
|
|
|
|
|
|
"github.com/strimertul/strimertul/loyalty"
|
2022-11-18 16:37:30 +00:00
|
|
|
|
2022-11-24 00:54:56 +00:00
|
|
|
"git.sr.ht/~hamcha/containers"
|
|
|
|
"github.com/nicklaw5/helix/v2"
|
2022-11-30 18:15:47 +00:00
|
|
|
"github.com/strimertul/strimertul/database"
|
|
|
|
"github.com/strimertul/strimertul/http"
|
2022-11-18 16:37:30 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-11-24 00:54:56 +00:00
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
2022-11-16 11:23:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// App struct
|
|
|
|
type App struct {
|
2022-11-18 16:37:30 +00:00
|
|
|
ctx context.Context
|
|
|
|
cliParams *cli.Context
|
2022-11-30 18:15:47 +00:00
|
|
|
driver database.DatabaseDriver
|
2022-11-24 00:54:56 +00:00
|
|
|
ready *containers.RWSync[bool]
|
2022-11-30 18:15:47 +00:00
|
|
|
|
|
|
|
db *database.LocalDBClient
|
|
|
|
twitchClient *twitch.Client
|
|
|
|
httpServer *http.Server
|
|
|
|
loyaltyManager *loyalty.Manager
|
2022-11-16 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewApp creates a new App application struct
|
2022-11-18 16:37:30 +00:00
|
|
|
func NewApp(cliParams *cli.Context) *App {
|
|
|
|
return &App{
|
|
|
|
cliParams: cliParams,
|
2022-11-24 00:54:56 +00:00
|
|
|
ready: containers.NewRWSync(false),
|
2022-11-18 16:37:30 +00:00
|
|
|
}
|
2022-11-16 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 16:37:30 +00:00
|
|
|
// startup is called when the app starts
|
2022-11-16 11:23:54 +00:00
|
|
|
func (a *App) startup(ctx context.Context) {
|
|
|
|
a.ctx = ctx
|
2022-11-18 16:37:30 +00:00
|
|
|
|
|
|
|
// Make KV hub
|
|
|
|
var err error
|
2022-11-30 18:15:47 +00:00
|
|
|
a.driver, err = database.GetDatabaseDriver(a.cliParams)
|
2022-11-18 16:37:30 +00:00
|
|
|
failOnError(err, "error opening database")
|
|
|
|
|
|
|
|
// Start database backup task
|
2022-11-30 18:15:47 +00:00
|
|
|
backupOpts := database.BackupOptions{
|
2022-11-18 16:37:30 +00:00
|
|
|
BackupDir: a.cliParams.String("backup-dir"),
|
|
|
|
BackupInterval: a.cliParams.Int("backup-interval"),
|
|
|
|
MaxBackups: a.cliParams.Int("max-backups"),
|
|
|
|
}
|
|
|
|
if backupOpts.BackupInterval > 0 {
|
|
|
|
go BackupTask(a.driver, backupOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
hub := a.driver.Hub()
|
|
|
|
go hub.Run()
|
|
|
|
|
2022-11-30 18:15:47 +00:00
|
|
|
a.db, err = database.NewLocalClient(hub, logger)
|
2022-11-18 16:37:30 +00:00
|
|
|
failOnError(err, "failed to initialize database module")
|
|
|
|
|
|
|
|
// Set meta keys
|
2022-11-30 18:15:47 +00:00
|
|
|
_ = a.db.PutKey("stul-meta/version", appVersion)
|
2022-11-18 16:37:30 +00:00
|
|
|
|
|
|
|
// Create logger and endpoints
|
2022-11-30 18:15:47 +00:00
|
|
|
a.httpServer, err = http.NewServer(a.db, logger)
|
2022-11-18 16:37:30 +00:00
|
|
|
failOnError(err, "could not initialize http server")
|
2022-11-30 18:15:47 +00:00
|
|
|
|
|
|
|
// Create twitch client
|
|
|
|
a.twitchClient, err = twitch.NewClient(a.db, a.httpServer, logger)
|
|
|
|
failOnError(err, "could not initialize twitch client")
|
|
|
|
|
|
|
|
// Initialize loyalty system
|
|
|
|
a.loyaltyManager, err = loyalty.NewManager(a.db, a.twitchClient, logger)
|
2022-11-18 16:37:30 +00:00
|
|
|
|
2022-11-24 00:54:56 +00:00
|
|
|
a.ready.Set(true)
|
|
|
|
runtime.EventsEmit(ctx, "ready", true)
|
|
|
|
logger.Info("app is ready")
|
|
|
|
|
|
|
|
// Start redirecting logs to UI
|
|
|
|
go func() {
|
|
|
|
for entry := range incomingLogs {
|
|
|
|
runtime.EventsEmit(ctx, "log-event", entry)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-18 16:37:30 +00:00
|
|
|
// Run HTTP server
|
2022-11-30 18:15:47 +00:00
|
|
|
failOnError(a.httpServer.Listen(), "HTTP server stopped")
|
2022-11-18 16:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) stop(context.Context) {
|
2022-11-30 18:15:47 +00:00
|
|
|
if a.loyaltyManager != nil {
|
|
|
|
a.loyaltyManager.Close()
|
|
|
|
}
|
|
|
|
if a.twitchClient != nil {
|
|
|
|
a.twitchClient.Close()
|
2022-11-29 23:52:54 +00:00
|
|
|
}
|
2022-11-30 18:15:47 +00:00
|
|
|
if a.httpServer != nil {
|
|
|
|
a.httpServer.Close()
|
|
|
|
}
|
|
|
|
a.db.Close()
|
2022-11-29 23:52:54 +00:00
|
|
|
|
2022-11-18 16:37:30 +00:00
|
|
|
failOnError(a.driver.Close(), "could not close driver")
|
|
|
|
}
|
|
|
|
|
2022-11-18 19:28:13 +00:00
|
|
|
func (a *App) AuthenticateKVClient(id string) {
|
|
|
|
idInt, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.driver.Hub().SetAuthenticated(idInt, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) IsServerReady() bool {
|
2022-11-30 18:15:47 +00:00
|
|
|
return a.ready.Get()
|
2022-11-18 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetKilovoltBind() string {
|
2022-11-30 18:15:47 +00:00
|
|
|
if a.httpServer == nil {
|
2022-11-23 21:22:49 +00:00
|
|
|
return ""
|
|
|
|
}
|
2022-11-30 18:15:47 +00:00
|
|
|
return a.httpServer.Config.Bind
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetTwitchAuthURL() string {
|
2022-11-30 18:15:47 +00:00
|
|
|
return a.twitchClient.GetAuthorizationURL()
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetTwitchLoggedUser() (helix.User, error) {
|
2022-11-30 18:15:47 +00:00
|
|
|
return a.twitchClient.GetLoggedUser()
|
2022-11-16 11:23:54 +00:00
|
|
|
}
|
2022-11-24 00:54:56 +00:00
|
|
|
|
|
|
|
func (a *App) GetLastLogs() []LogEntry {
|
|
|
|
return lastLogs
|
|
|
|
}
|