arcast/cmd/mobile/main.go

121 lines
3.0 KiB
Go
Raw Normal View History

2023-12-13 20:07:22 +01:00
package main
import (
"context"
"crypto/tls"
2023-12-13 20:07:22 +01:00
"os"
"path/filepath"
2023-12-13 20:07:22 +01:00
"forge.cadoles.com/arcad/arcast"
2023-12-13 20:07:22 +01:00
"forge.cadoles.com/arcad/arcast/pkg/browser/gioui"
"forge.cadoles.com/arcad/arcast/pkg/config"
2023-12-13 20:07:22 +01:00
"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"
2023-12-13 20:07:22 +01:00
func main() {
ctx := context.Background()
window := app.NewWindow(
app.Fullscreen.Option(),
2024-04-26 09:35:16 +02:00
app.AnyOrientation.Option(),
2023-12-13 20:07:22 +01:00
)
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)
2024-04-26 09:35:16 +02:00
evt.Frame(ops)
2023-12-13 20:07:22 +01:00
}
}
}()
go func() {
2023-12-13 20:07:22 +01:00
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),
2024-04-24 17:32:01 +02:00
server.WithAllowedOrigins(conf.AllowedOrigins...),
)
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()
}
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
}