package android import ( "context" "crypto/tls" "net" "path/filepath" "forge.cadoles.com/arcad/arcast" "forge.cadoles.com/arcad/arcast/pkg/browser/proxy" "forge.cadoles.com/arcad/arcast/pkg/config" "forge.cadoles.com/arcad/arcast/pkg/server" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) var ( browser = proxy.NewBrowser() ) func SetBridge(bridge Bridge) { browser.SetLoadURL(bridge.LoadURL) browser.SetGetTitle(bridge.GetTitle) browser.SetGetURL(bridge.GetURL) } func StartServer(dataDir string) string { ctx := context.Background() conf := config.DefaultConfig() filename := filepath.Join(dataDir, "config.json") logger.Info(ctx, "loading or creating configuration file", logger.F("filename", filename)) if err := config.LoadOrCreate(ctx, filename, conf, config.DefaultTransforms...); err != nil { logger.Error(ctx, "could not load configuration file", logger.CapturedE(errors.WithStack(err))) } go func() { for { runServer(ctx, conf) } }() _, port, err := net.SplitHostPort(conf.HTTP.Address) if err != nil { panic(errors.Wrap(err, "could not parse server listening address")) } return port } func runServer(ctx context.Context, conf *config.Config) { ctx, cancel := context.WithCancel(ctx) defer cancel() 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))) } }