2021-05-02 12:29:43 +00:00
package main
import (
"embed"
"fmt"
2022-01-27 15:49:18 +00:00
"log"
2021-09-18 20:06:22 +00:00
"math/rand"
2022-01-02 10:45:09 +00:00
"os"
2021-05-02 12:29:43 +00:00
"time"
2022-11-23 15:34:49 +00:00
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"
2022-01-27 15:49:18 +00:00
"go.uber.org/zap/zapcore"
2021-05-02 12:29:43 +00:00
"github.com/strimertul/strimertul/modules"
"github.com/strimertul/strimertul/modules/loyalty"
2021-05-14 11:15:38 +00:00
"github.com/strimertul/strimertul/modules/twitch"
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
)
2022-11-23 15:34:49 +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"
2022-11-16 11:23:54 +00:00
var logger * zap . Logger
2021-05-02 12:29:43 +00:00
//go:embed frontend/dist/*
var frontend embed . FS
2021-12-09 10:45:10 +00:00
type ModuleConstructor = func ( manager * modules . Manager ) error
var moduleList = map [ modules . ModuleID ] ModuleConstructor {
modules . ModuleLoyalty : loyalty . Register ,
2022-01-12 11:02:54 +00:00
modules . ModuleTwitch : twitch . Register ,
2021-12-09 10:45:10 +00:00
}
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 {
& cli . BoolFlag { Name : "debug" , Aliases : [ ] string { "d" } , Usage : "print more logs (for debugging)" , Value : false } ,
& cli . BoolFlag { Name : "json-log" , Usage : "print logs in JSON format" , Value : false } ,
& 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
initLogger ( ctx . Bool ( "debug" ) , ctx . Bool ( "json-log" ) )
return nil
} ,
After : func ( ctx * cli . Context ) error {
2022-11-18 16:37:30 +00:00
_ = 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 initLogger ( debug bool , json bool ) {
if debug {
2022-01-27 15:49:18 +00:00
cfg := zap . NewDevelopmentConfig ( )
2022-11-16 11:23:54 +00:00
if json {
2022-01-27 15:49:18 +00:00
cfg . Encoding = "json"
}
logger , _ = cfg . Build ( )
2021-12-07 00:22:45 +00:00
} else {
2022-01-27 15:49:18 +00:00
cfg := zap . NewProductionConfig ( )
2022-11-16 11:23:54 +00:00
if ! json {
2022-01-27 15:49:18 +00:00
cfg . Encoding = "console"
cfg . EncoderConfig . EncodeTime = zapcore . ISO8601TimeEncoder
cfg . EncoderConfig . CallerKey = zapcore . OmitKey
}
logger , _ = cfg . Build ( )
2021-05-02 19:33:37 +00:00
}
2022-11-16 11:23:54 +00:00
}
2021-05-02 19:33:37 +00:00
2022-11-16 11:23:54 +00:00
func cliMain ( ctx * cli . Context ) error {
// Create an instance of the app structure
2022-11-18 16:37:30 +00:00
app := NewApp ( ctx )
2022-11-16 11:23:54 +00:00
// Create application with options
2022-11-18 16:37:30 +00:00
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 ,
2022-11-18 16:37:30 +00:00
OnShutdown : app . stop ,
2022-11-16 11:23:54 +00:00
Bind : [ ] interface { } {
app ,
} ,
} )
if err != nil {
2022-11-18 16:37:30 +00:00
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 {
2022-11-18 16:37:30 +00:00
logger . Fatal ( text , zap . Error ( err ) )
2021-05-02 12:29:43 +00:00
}
}
2022-11-18 16:37:30 +00:00
func fatalError ( err error , text string ) error {
return cli . Exit ( fmt . Errorf ( "%s: %w" , text , err ) , 1 )
}