fixup! wip: refactor: API

This commit is contained in:
Rob Watson 2025-05-11 07:55:06 +02:00
parent d0d96dd1d9
commit c5724dfe59
3 changed files with 36 additions and 51 deletions

View File

@ -10,7 +10,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"runtime/debug" "runtime"
"syscall" "syscall"
"git.netflux.io/rob/octoplex/internal/app" "git.netflux.io/rob/octoplex/internal/app"
@ -88,7 +88,6 @@ func run() error {
return fmt.Errorf("build logger: %w", err) return fmt.Errorf("build logger: %w", err)
} }
// When running in headless mode tview doesn't handle SIGINT for us.
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
@ -107,24 +106,25 @@ func run() error {
return fmt.Errorf("new docker client: %w", err) return fmt.Errorf("new docker client: %w", err)
} }
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Errorf("read build info: %w", err)
}
app := app.New(app.Params{ app := app.New(app.Params{
ConfigService: configService, ConfigService: configService,
DockerClient: dockerClient, DockerClient: dockerClient,
ConfigFilePath: configService.Path(), ConfigFilePath: configService.Path(),
BuildInfo: domain.BuildInfo{ Logger: logger,
GoVersion: buildInfo.GoVersion,
Version: version,
Commit: commit,
Date: date,
},
Logger: logger,
}) })
logger.Info(
"Starting application",
"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 err := app.Run(ctx); err != nil {
if errors.Is(err, context.Canceled) && context.Cause(ctx) == errShutdown { if errors.Is(err, context.Canceled) && context.Cause(ctx) == errShutdown {
return errShutdown return errShutdown

View File

@ -19,34 +19,27 @@ import (
"git.netflux.io/rob/octoplex/internal/mediaserver" "git.netflux.io/rob/octoplex/internal/mediaserver"
"git.netflux.io/rob/octoplex/internal/replicator" "git.netflux.io/rob/octoplex/internal/replicator"
"git.netflux.io/rob/octoplex/internal/server" "git.netflux.io/rob/octoplex/internal/server"
"git.netflux.io/rob/octoplex/internal/terminal"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
// App is an instance of the app. // App is an instance of the app.
type App struct { type App struct {
cfg config.Config cfg config.Config
configService *config.Service configService *config.Service
eventBus *event.Bus eventBus *event.Bus
dispatchC chan event.Command dispatchC chan event.Command
dockerClient container.DockerClient dockerClient container.DockerClient
screen *terminal.Screen // Screen may be nil. logger *slog.Logger
clipboardAvailable bool
configFilePath string
buildInfo domain.BuildInfo
logger *slog.Logger
} }
// Params holds the parameters for running the application. // Params holds the parameters for running the application.
type Params struct { type Params struct {
ConfigService *config.Service ConfigService *config.Service
DockerClient container.DockerClient DockerClient container.DockerClient
ChanSize int ChanSize int
ClipboardAvailable bool ConfigFilePath string
ConfigFilePath string Logger *slog.Logger
BuildInfo domain.BuildInfo
Logger *slog.Logger
} }
// defaultChanSize is the default size of the dispatch channel. // defaultChanSize is the default size of the dispatch channel.
@ -55,15 +48,12 @@ const defaultChanSize = 64
// New creates a new application instance. // New creates a new application instance.
func New(params Params) *App { func New(params Params) *App {
return &App{ return &App{
cfg: params.ConfigService.Current(), cfg: params.ConfigService.Current(),
configService: params.ConfigService, configService: params.ConfigService,
eventBus: event.NewBus(params.Logger.With("component", "event_bus")), eventBus: event.NewBus(params.Logger.With("component", "event_bus")),
dispatchC: make(chan event.Command, cmp.Or(params.ChanSize, defaultChanSize)), dispatchC: make(chan event.Command, cmp.Or(params.ChanSize, defaultChanSize)),
dockerClient: params.DockerClient, dockerClient: params.DockerClient,
clipboardAvailable: params.ClipboardAvailable, logger: params.Logger,
configFilePath: params.ConfigFilePath,
buildInfo: params.BuildInfo,
logger: params.Logger,
} }
} }
@ -78,8 +68,7 @@ func (a *App) Run(ctx context.Context) error {
return errors.New("config: either sources.mediaServer.rtmp.enabled or sources.mediaServer.rtmps.enabled must be set") return errors.New("config: either sources.mediaServer.rtmp.enabled or sources.mediaServer.rtmps.enabled must be set")
} }
// doFatalError publishes a fatal error to the event bus, waiting for the // doFatalError publishes a fatal error to the event bus.
// user to acknowledge it if not in headless mode.
doFatalError := func(msg string) { doFatalError := func(msg string) {
a.eventBus.Send(event.FatalErrorOccurredEvent{Message: msg}) a.eventBus.Send(event.FatalErrorOccurredEvent{Message: msg})
} }

View File

@ -33,10 +33,8 @@ func buildClientServer(
screenCaptureC chan<- terminal.ScreenCapture, screenCaptureC chan<- terminal.ScreenCapture,
logger *slog.Logger, logger *slog.Logger,
) (*client.App, *app.App) { ) (*client.App, *app.App) {
buildInfo := domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"}
client := client.New(client.NewParams{ client := client.New(client.NewParams{
BuildInfo: buildInfo, BuildInfo: domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"},
Screen: &terminal.Screen{ Screen: &terminal.Screen{
Screen: screen, Screen: screen,
Width: 160, Width: 160,
@ -47,11 +45,9 @@ func buildClientServer(
}) })
server := app.New(app.Params{ server := app.New(app.Params{
ConfigService: configService, ConfigService: configService,
DockerClient: dockerClient, DockerClient: dockerClient,
ClipboardAvailable: false, Logger: logger,
BuildInfo: buildInfo,
Logger: logger,
}) })
return client, server return client, server