2023-12-13 20:07:22 +01:00
|
|
|
//go:build android
|
|
|
|
// +build android
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2024-01-12 15:15:24 +01:00
|
|
|
"sync"
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/browser/gioui"
|
|
|
|
"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"
|
2024-01-12 15:15:24 +01:00
|
|
|
"github.com/gioui-plugins/gio-plugins/safedata"
|
|
|
|
"github.com/gioui-plugins/gio-plugins/safedata/giosafedata"
|
2023-12-13 20:07:22 +01:00
|
|
|
"github.com/gioui-plugins/gio-plugins/webviewer/webview"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
2024-01-12 15:15:24 +01:00
|
|
|
const instanceIDSecretIdentifier = "instance_id"
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
webview.SetDebug(true)
|
|
|
|
window := app.NewWindow(
|
|
|
|
app.Fullscreen.Option(),
|
|
|
|
)
|
|
|
|
|
|
|
|
browser := gioui.NewBrowser(window)
|
|
|
|
|
2024-01-12 15:15:24 +01:00
|
|
|
var safeDataConfig safedata.Config
|
|
|
|
var safeDataConfigWaigGroup sync.WaitGroup
|
|
|
|
var initSafeDataConfig sync.Once
|
|
|
|
|
|
|
|
safeDataConfigWaigGroup.Add(1)
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
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(gtx.Ops)
|
2024-01-12 15:15:24 +01:00
|
|
|
case app.ViewEvent:
|
|
|
|
initSafeDataConfig.Do(func() {
|
|
|
|
defer safeDataConfigWaigGroup.Done()
|
|
|
|
safeDataConfig = giosafedata.NewConfigFromViewEvent(window, evt, "com.cadoles.arcast_player")
|
|
|
|
})
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2024-01-12 15:15:24 +01:00
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
for {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-01-12 15:15:24 +01:00
|
|
|
safeDataConfigWaigGroup.Wait()
|
|
|
|
safe := safedata.NewSafeData(safeDataConfig)
|
|
|
|
|
|
|
|
instanceID, err := getInstanceIDFromSafeData(safe)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(ctx, "could not retrieve instance id", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
server := server.New(browser, server.WithInstanceID(instanceID))
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2024-01-12 15:15:24 +01:00
|
|
|
|
|
|
|
func getInstanceIDFromSafeData(safe *safedata.SafeData) (string, error) {
|
|
|
|
instanceIDSecret, err := safe.Get(instanceIDSecretIdentifier)
|
|
|
|
if err != nil && err.Error() != "not found" {
|
|
|
|
return "", errors.Wrapf(err, "could not retrieve instance id secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
var instanceID string
|
|
|
|
if len(instanceIDSecret.Data) > 0 {
|
|
|
|
instanceID = string(instanceIDSecret.Data)
|
|
|
|
} else {
|
|
|
|
instanceID = server.NewRandomInstanceID()
|
|
|
|
|
|
|
|
instanceIDSecret.Identifier = instanceIDSecretIdentifier
|
|
|
|
instanceIDSecret.Data = []byte(instanceID)
|
|
|
|
instanceIDSecret.Description = "Arcast player instance identifier"
|
|
|
|
|
|
|
|
if err := safe.Set(instanceIDSecret); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "could not save instance id secret")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return instanceID, nil
|
|
|
|
}
|