1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00
strimertul/main.go

144 lines
3.8 KiB
Go
Raw Normal View History

2021-05-02 12:29:43 +00:00
package main
import (
"embed"
"fmt"
2022-01-27 15:49:18 +00:00
"log"
"math/rand"
2022-01-02 10:45:09 +00:00
"os"
2021-05-02 12:29:43 +00:00
"time"
2022-11-24 12:15:59 +00:00
"go.uber.org/zap/zapcore"
"github.com/strimertul/strimertul/modules"
"github.com/strimertul/strimertul/modules/loyalty"
"github.com/strimertul/strimertul/modules/twitch"
jsoniter "github.com/json-iterator/go"
2022-11-16 11:23:54 +00:00
"github.com/urfave/cli/v2"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"go.uber.org/zap"
2021-05-02 12:29:43 +00:00
2021-06-05 23:18:31 +00:00
_ "net/http/pprof"
2021-05-02 12:29:43 +00:00
)
var json = jsoniter.ConfigFastest
2022-11-16 11:23:54 +00:00
const databaseDefaultDriver = "pebble"
2021-05-02 12:29:43 +00:00
2021-12-06 13:47:38 +00:00
var appVersion = "v0.0.0-UNKNOWN"
2021-05-02 12:29:43 +00:00
//go:embed frontend/dist/*
var frontend embed.FS
type ModuleConstructor = func(manager *modules.Manager) error
var moduleList = map[modules.ModuleID]ModuleConstructor{
modules.ModuleLoyalty: loyalty.Register,
modules.ModuleTwitch: twitch.Register,
}
2021-05-02 12:29:43 +00:00
func main() {
2022-11-16 11:23:54 +00:00
app := &cli.App{
Name: "strimertul",
Usage: "the small broadcasting suite for Twitch",
Version: appVersion,
Action: cliMain,
Flags: []cli.Flag{
2022-11-24 12:15:59 +00:00
&cli.StringFlag{Name: "log-level", Usage: "logging level (debug,info,warn,error)", Value: "info"},
2022-11-16 11:23:54 +00:00
&cli.StringFlag{Name: "driver", Usage: "specify database driver", Value: "auto"},
&cli.StringFlag{Name: "database-dir", Aliases: []string{"db-dir"}, Usage: "specify database directory", Value: "data"},
&cli.StringFlag{Name: "backup-dir", Aliases: []string{"b-dir"}, Usage: "specify backup directory", Value: "backups"},
&cli.IntFlag{Name: "backup-interval", Aliases: []string{"b-i"}, Usage: "specify backup interval (in minutes, 0 to disable)", Value: 60},
&cli.IntFlag{Name: "max-backups", Aliases: []string{"b-max"}, Usage: "maximum number of backups to keep, older ones will be deleted, set to 0 to keep all", Value: 20},
},
Commands: []*cli.Command{
{
Name: "import",
Usage: "import database from JSON file",
ArgsUsage: "[-f input.json]",
Flags: []cli.Flag{
&cli.StringFlag{Name: "file", Aliases: []string{"f"}, Usage: "file to open", DefaultText: "STDIN"},
},
Action: cliImport,
},
{
Name: "export",
Usage: "export database as JSON file",
ArgsUsage: "[-f output.json]",
Flags: []cli.Flag{
&cli.StringFlag{Name: "file", Aliases: []string{"f"}, Usage: "file to save to", DefaultText: "STDOUT"},
},
Action: cliExport,
},
{
Name: "restore",
Usage: "restore database from backup",
ArgsUsage: "[-f backup.db]",
Flags: []cli.Flag{
&cli.StringFlag{Name: "file", Aliases: []string{"f"}, Usage: "backup to open", DefaultText: "STDOUT"},
},
Action: cliRestore,
},
},
Before: func(ctx *cli.Context) error {
// Seed RNG
rand.Seed(time.Now().UnixNano())
// Initialize logger with global flags
2022-11-24 12:15:59 +00:00
level, err := zapcore.ParseLevel(ctx.String("log-level"))
if err != nil {
level = zapcore.InfoLevel
}
initLogger(level)
2022-11-16 11:23:54 +00:00
return nil
},
After: func(ctx *cli.Context) error {
_ = logger.Sync()
2022-11-16 11:23:54 +00:00
zap.RedirectStdLog(logger)()
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func cliMain(ctx *cli.Context) error {
// Create an instance of the app structure
app := NewApp(ctx)
2022-11-16 11:23:54 +00:00
// Create application with options
err := wails.Run(&options.App{
2022-11-16 11:23:54 +00:00
Title: "strimertul",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: frontend,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup,
OnShutdown: app.stop,
2022-11-16 11:23:54 +00:00
Bind: []interface{}{
app,
},
})
if err != nil {
return cli.Exit(fmt.Errorf("%s: %w", "App exited unexpectedly", err), 1)
2022-11-16 11:23:54 +00:00
}
return nil
}
2021-05-02 12:29:43 +00:00
func failOnError(err error, text string) {
if err != nil {
logger.Fatal(text, zap.Error(err))
2021-05-02 12:29:43 +00:00
}
}
func fatalError(err error, text string) error {
return cli.Exit(fmt.Errorf("%s: %w", text, err), 1)
}