fixup! wip: refactor: API
This commit is contained in:
parent
5aa1be2066
commit
7a3f1335c1
4
go.mod
4
go.mod
@ -27,6 +27,7 @@ require (
|
||||
github.com/containerd/log v0.1.0 // indirect
|
||||
github.com/containerd/platforms v0.2.1 // indirect
|
||||
github.com/cpuguy83/dockercfg v0.3.2 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
@ -68,6 +69,7 @@ require (
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/rs/zerolog v1.33.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.7.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
|
||||
@ -83,7 +85,9 @@ require (
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.12 // indirect
|
||||
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||
github.com/urfave/cli/v2 v2.27.6 // indirect
|
||||
github.com/vektra/mockery/v2 v2.52.2 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.3 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
|
||||
|
8
go.sum
8
go.sum
@ -18,6 +18,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
|
||||
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
|
||||
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
|
||||
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@ -142,6 +144,8 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
|
||||
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
|
||||
@ -187,8 +191,12 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA
|
||||
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
|
||||
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
|
||||
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
|
||||
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
|
||||
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/vektra/mockery/v2 v2.52.2 h1:8QfPKUIrq8P3Cs7G79Iu4Byd5wdhGCE0quIS27x7rQo=
|
||||
github.com/vektra/mockery/v2 v2.52.2/go.mod h1:zGDY/f6bip0Yh13GQ5j7xa43fuEoYBa4ICHEaihisHw=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
|
220
main.go
Normal file
220
main.go
Normal file
@ -0,0 +1,220 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"syscall"
|
||||
|
||||
"git.netflux.io/rob/octoplex/internal/client"
|
||||
"git.netflux.io/rob/octoplex/internal/config"
|
||||
"git.netflux.io/rob/octoplex/internal/domain"
|
||||
"git.netflux.io/rob/octoplex/internal/server"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.design/x/clipboard"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
var (
|
||||
// version is the version of the application.
|
||||
version string
|
||||
// commit is the commit hash of the application.
|
||||
commit string
|
||||
// date is the date of the build.
|
||||
date string
|
||||
)
|
||||
|
||||
var errShutdown = errors.New("shutdown")
|
||||
|
||||
func main() {
|
||||
// when server is running:
|
||||
// server log goes to wherever config defined it
|
||||
// client log does not exist
|
||||
// when client is running:
|
||||
// server log does not exist
|
||||
// client log goes to ??? but for now octoplex.log
|
||||
// when both are running:
|
||||
// server log goes to wherever config defined it
|
||||
// client logs goes to ??? but for now octoplex.log
|
||||
app := &cli.App{
|
||||
Name: "Octoplex",
|
||||
Usage: "Octoplex is a live video restreamer for Docker.",
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "client",
|
||||
Usage: "Run the client",
|
||||
Flags: []cli.Flag{ /* client flags */ },
|
||||
Action: func(c *cli.Context) error {
|
||||
return runClient(c)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "server",
|
||||
Usage: "Run the server",
|
||||
Flags: []cli.Flag{ /* server flags */ },
|
||||
Action: func(c *cli.Context) error {
|
||||
return runServer(c, true)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "run",
|
||||
Usage: "Run server and client together (testing)",
|
||||
Flags: []cli.Flag{ /* optional combined flags */ },
|
||||
Action: func(c *cli.Context) error {
|
||||
return runClientAndServer(c)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runClient(_ *cli.Context) error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// TODO: logger from config
|
||||
fptr, err := os.OpenFile("octoplex.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open log file: %w", err)
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(fptr, nil))
|
||||
|
||||
var clipboardAvailable bool
|
||||
if err = clipboard.Init(); err != nil {
|
||||
logger.Warn("Clipboard not available", "err", err)
|
||||
} else {
|
||||
clipboardAvailable = true
|
||||
}
|
||||
|
||||
buildInfo, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return fmt.Errorf("read build info: %w", err)
|
||||
}
|
||||
|
||||
app := client.New(client.NewParams{
|
||||
ClipboardAvailable: clipboardAvailable,
|
||||
BuildInfo: domain.BuildInfo{
|
||||
GoVersion: buildInfo.GoVersion,
|
||||
Version: version,
|
||||
Commit: commit,
|
||||
Date: date,
|
||||
},
|
||||
Logger: logger,
|
||||
})
|
||||
if err := app.Run(ctx); err != nil {
|
||||
return fmt.Errorf("run app: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runServer(_ *cli.Context, stderrAvailable bool) error {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
defer cancel(nil)
|
||||
|
||||
configService, err := config.NewDefaultService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("build config service: %w", err)
|
||||
}
|
||||
|
||||
cfg, err := configService.ReadOrCreateConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read or create config: %w", err)
|
||||
}
|
||||
|
||||
// TODO: improve logger API
|
||||
// Currently it's a bit complicated because we can only use stdout - the
|
||||
// preferred destination - if the client is not running. Otherwise we
|
||||
// fallback to the legacy configuration but this should be bought more
|
||||
// in-line with the client/server split.
|
||||
var w io.Writer
|
||||
if stderrAvailable {
|
||||
w = os.Stdout
|
||||
} else if !cfg.LogFile.Enabled {
|
||||
w = io.Discard
|
||||
} else {
|
||||
w, err = os.OpenFile(cfg.LogFile.GetPath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening log file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var handlerOpts slog.HandlerOptions
|
||||
if os.Getenv("OCTO_DEBUG") != "" {
|
||||
handlerOpts.Level = slog.LevelDebug
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(w, &handlerOpts))
|
||||
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-ch
|
||||
logger.Info("Received interrupt signal, exiting")
|
||||
signal.Stop(ch)
|
||||
cancel(errShutdown)
|
||||
}()
|
||||
|
||||
dockerClient, err := dockerclient.NewClientWithOpts(
|
||||
dockerclient.FromEnv,
|
||||
dockerclient.WithAPIVersionNegotiation(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("new docker client: %w", err)
|
||||
}
|
||||
|
||||
app := server.New(server.Params{
|
||||
ConfigService: configService,
|
||||
DockerClient: dockerClient,
|
||||
ConfigFilePath: configService.Path(),
|
||||
Logger: logger,
|
||||
})
|
||||
|
||||
logger.Info(
|
||||
"Starting server",
|
||||
"version",
|
||||
cmp.Or(version, "devel"),
|
||||
"commit",
|
||||
cmp.Or(commit, "unknown"),
|
||||
"date",
|
||||
cmp.Or(date, "unknown"),
|
||||
"go_version",
|
||||
runtime.Version(),
|
||||
)
|
||||
|
||||
if err := app.Run(ctx); err != nil {
|
||||
if errors.Is(err, context.Canceled) && context.Cause(ctx) == errShutdown {
|
||||
return errShutdown
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runClientAndServer(c *cli.Context) error {
|
||||
g, _ := errgroup.WithContext(c.Context)
|
||||
|
||||
g.Go(func() error {
|
||||
return runClient(c)
|
||||
})
|
||||
|
||||
g.Go(func() error {
|
||||
return runServer(c, false)
|
||||
})
|
||||
|
||||
return g.Wait()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user