2023-12-13 20:07:22 +01:00
|
|
|
package player
|
|
|
|
|
|
|
|
import (
|
2024-04-24 10:49:47 +02:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2023-12-13 20:07:22 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-01-16 09:27:04 +01:00
|
|
|
"forge.cadoles.com/arcad/arcast"
|
2023-12-13 20:07:22 +01:00
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/browser/lorca"
|
2024-04-24 10:49:47 +02:00
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/config"
|
2023-12-13 20:07:22 +01:00
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/server"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Run() *cli.Command {
|
|
|
|
defaults := lorca.NewOptions()
|
2024-04-24 10:49:47 +02:00
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
return &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Flags: []cli.Flag{
|
2024-04-24 10:49:47 +02:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_CONFIG"},
|
|
|
|
Value: config.DefaultConfigFile(context.Background()),
|
|
|
|
},
|
2023-12-13 20:07:22 +01:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "additional-chrome-arg",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_ADDITIONAL_CHROME_ARGS"},
|
|
|
|
Value: cli.NewStringSlice("incognito"),
|
|
|
|
},
|
2024-01-12 17:09:19 +01:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "instance-id",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_INSTANCE_ID"},
|
|
|
|
Value: "",
|
|
|
|
},
|
2024-01-16 09:27:04 +01:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "address",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_ADDRESS"},
|
|
|
|
Value: ":",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2024-04-24 10:49:47 +02:00
|
|
|
Name: "https-address",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_HTTPS_ADDRESS"},
|
2024-01-16 09:27:04 +01:00
|
|
|
Value: ":",
|
|
|
|
},
|
2023-12-13 20:07:22 +01:00
|
|
|
&cli.IntFlag{
|
2024-01-12 17:09:19 +01:00
|
|
|
Name: "window-height",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_WINDOW_HEIGHT"},
|
|
|
|
Value: defaults.Height,
|
2023-12-13 20:07:22 +01:00
|
|
|
},
|
2024-01-16 09:27:04 +01:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "apps",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_APPS"},
|
|
|
|
Value: false,
|
|
|
|
},
|
2023-12-13 20:07:22 +01:00
|
|
|
&cli.IntFlag{
|
2024-01-12 17:09:19 +01:00
|
|
|
Name: "window-width",
|
|
|
|
EnvVars: []string{"ARCAST_DESKTOP_WINDOW_WIDTH"},
|
|
|
|
Value: defaults.Width,
|
2023-12-13 20:07:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(ctx *cli.Context) error {
|
2024-04-24 10:49:47 +02:00
|
|
|
configFile := ctx.String("config")
|
2023-12-13 20:07:22 +01:00
|
|
|
windowHeight := ctx.Int("window-height")
|
|
|
|
windowWidth := ctx.Int("window-width")
|
|
|
|
chromeArgs := addFlagsPrefix(ctx.StringSlice("additional-chrome-arg")...)
|
|
|
|
|
|
|
|
browser := lorca.NewBrowser(
|
|
|
|
lorca.WithAdditionalChromeArgs(chromeArgs...),
|
|
|
|
lorca.WithWindowSize(windowWidth, windowHeight),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := browser.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not start browser")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
browser.Wait()
|
|
|
|
logger.Warn(ctx.Context, "browser was closed")
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
logger.Info(ctx.Context, "stopping browser")
|
|
|
|
if err := browser.Stop(); err != nil {
|
|
|
|
logger.Error(ctx.Context, "could not stop browser", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-04-24 10:49:47 +02:00
|
|
|
conf := config.DefaultConfig()
|
|
|
|
|
|
|
|
logger.Info(ctx.Context, "loading or creating configuration file", logger.F("filename", configFile))
|
|
|
|
if err := config.LoadOrCreate(ctx.Context, configFile, conf, config.DefaultTransforms...); err != nil {
|
|
|
|
logger.Error(ctx.Context, "could not load configuration file", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
|
2024-01-12 17:09:19 +01:00
|
|
|
instanceID := ctx.String("instance-id")
|
2024-04-24 10:49:47 +02:00
|
|
|
if instanceID != "" {
|
|
|
|
conf.InstanceID = instanceID
|
2024-01-12 17:09:19 +01:00
|
|
|
}
|
|
|
|
|
2024-04-24 10:49:47 +02:00
|
|
|
cert, err := tls.X509KeyPair(conf.HTTPS.Cert, conf.HTTPS.Key)
|
2024-01-16 09:27:04 +01:00
|
|
|
if err != nil {
|
2024-04-24 10:49:47 +02:00
|
|
|
return errors.Wrap(err, "could not parse tls cert/key pair")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("apps") {
|
|
|
|
conf.Apps.Enabled = ctx.Bool("apps")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("address") {
|
|
|
|
conf.HTTP.Address = ctx.String("address")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("https-address") {
|
|
|
|
conf.HTTPS.Address = ctx.String("tls-address")
|
2024-01-16 09:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
server := server.New(browser,
|
2024-04-24 10:49:47 +02:00
|
|
|
server.WithInstanceID(conf.InstanceID),
|
|
|
|
server.WithAppsEnabled(conf.Apps.Enabled),
|
|
|
|
server.WithDefaultApp(conf.Apps.DefaultApp),
|
2024-01-16 09:27:04 +01:00
|
|
|
server.WithApps(arcast.DefaultApps...),
|
2024-04-24 10:49:47 +02:00
|
|
|
server.WithAddress(conf.HTTP.Address),
|
|
|
|
server.WithTLSAddress(conf.HTTPS.Address),
|
|
|
|
server.WithTLSCertificate(&cert),
|
2024-01-16 09:27:04 +01:00
|
|
|
)
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
if err := server.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not start server")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := server.Stop(); err != nil {
|
|
|
|
logger.Error(ctx.Context, "could not stop server", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := server.Wait(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not wait for server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFlagsPrefix(stripped ...string) []string {
|
|
|
|
flags := make([]string, len(stripped))
|
|
|
|
for i, s := range stripped {
|
|
|
|
flags[i] = fmt.Sprintf("--%s", s)
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|