package main import ( "context" "crypto/tls" "os" "path/filepath" "forge.cadoles.com/arcad/arcast" "forge.cadoles.com/arcad/arcast/pkg/browser/gioui" "forge.cadoles.com/arcad/arcast/pkg/config" "forge.cadoles.com/arcad/arcast/pkg/server" "gioui.org/app" "gioui.org/io/system" "gioui.org/layout" "gioui.org/op" "github.com/gioui-plugins/gio-plugins/plugin" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) const packageName = "com.cadoles.arcast_player" func main() { ctx := context.Background() window := app.NewWindow( app.Fullscreen.Option(), app.AnyOrientation.Option(), ) browser := gioui.NewBrowser(window) go func() { ops := new(op.Ops) for { evt := window.NextEvent() plugin.Install(window, evt) switch evt := evt.(type) { case system.DestroyEvent: os.Exit(0) return case system.FrameEvent: gtx := layout.NewContext(ops, evt) browser.Layout(gtx) evt.Frame(ops) } } }() go func() { for { ctx, cancel := context.WithCancel(ctx) defer cancel() conf := config.DefaultConfig() configFiles := getConfigFiles(ctx) for _, f := range configFiles { logger.Info(ctx, "loading or creating configuration file", logger.F("filename", f)) if err := config.LoadOrCreate(ctx, f, conf, config.DefaultTransforms...); err != nil { logger.Error(ctx, "could not load configuration file", logger.CapturedE(errors.WithStack(err))) continue } break } cert, err := tls.X509KeyPair(conf.HTTPS.Cert, conf.HTTPS.Key) if err != nil { logger.Fatal(ctx, "could not parse x509 certificate", logger.CapturedE(errors.WithStack(err))) } server := server.New( browser, server.WithInstanceID(conf.InstanceID), server.WithAppsEnabled(conf.Apps.Enabled), server.WithDefaultApp(conf.Apps.DefaultApp), server.WithApps(arcast.DefaultApps...), server.WithTLSCertificate(&cert), server.WithAddress(conf.HTTP.Address), server.WithTLSAddress(conf.HTTPS.Address), server.WithAllowedOrigins(conf.AllowedOrigins...), ) if err := server.Start(); err != nil { logger.Fatal(ctx, "could not start server", logger.CapturedE(errors.WithStack(err))) } defer func() { if err := server.Stop(); err != nil { logger.Error(ctx, "could not stop server", logger.CapturedE(errors.WithStack(err))) } }() if err := server.Wait(); err != nil { logger.Error(ctx, "could not wait for server", logger.CapturedE(errors.WithStack(err))) } } }() app.Main() } func getConfigFiles(ctx context.Context) []string { configFiles := make([]string, 0) sharedStorageConfigFile := filepath.Join("/storage/emulated/0/Android/data", packageName, "files/config.json") configFiles = append(configFiles, sharedStorageConfigFile) dataDir, err := app.DataDir() if err != nil { logger.Error(ctx, "could not retrieve app data dir", logger.CapturedE(errors.WithStack(err))) } else { appDataConfigFile := filepath.Join(dataDir, "config.json") configFiles = append(configFiles, appDataConfigFile) } return configFiles }