fixup! wip: refactor: API
This commit is contained in:
parent
888ac7d67d
commit
7706bb363f
@ -7,15 +7,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
|
"git.netflux.io/rob/octoplex/internal/client"
|
||||||
"git.netflux.io/rob/octoplex/internal/domain"
|
"git.netflux.io/rob/octoplex/internal/domain"
|
||||||
"git.netflux.io/rob/octoplex/internal/event"
|
"git.netflux.io/rob/octoplex/internal/event"
|
||||||
pb "git.netflux.io/rob/octoplex/internal/generated/grpc"
|
|
||||||
"git.netflux.io/rob/octoplex/internal/protocol"
|
|
||||||
"git.netflux.io/rob/octoplex/internal/terminal"
|
|
||||||
"golang.design/x/clipboard"
|
"golang.design/x/clipboard"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -38,8 +33,6 @@ func run() error {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
g, ctx := errgroup.WithContext(ctx)
|
|
||||||
|
|
||||||
// TODO: logger from config
|
// TODO: logger from config
|
||||||
fptr, err := os.OpenFile("octoplex.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
fptr, err := os.OpenFile("octoplex.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,8 +40,6 @@ func run() error {
|
|||||||
}
|
}
|
||||||
logger := slog.New(slog.NewTextHandler(fptr, nil))
|
logger := slog.New(slog.NewTextHandler(fptr, nil))
|
||||||
|
|
||||||
bus := event.NewBus(logger)
|
|
||||||
|
|
||||||
var clipboardAvailable bool
|
var clipboardAvailable bool
|
||||||
if err = clipboard.Init(); err != nil {
|
if err = clipboard.Init(); err != nil {
|
||||||
logger.Warn("Clipboard not available", "err", err)
|
logger.Warn("Clipboard not available", "err", err)
|
||||||
@ -61,61 +52,21 @@ func run() error {
|
|||||||
return fmt.Errorf("read build info: %w", err)
|
return fmt.Errorf("read build info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
app := client.NewApp(
|
||||||
if err != nil {
|
event.NewBus(logger),
|
||||||
return fmt.Errorf("connect to gRPC server: %w", err)
|
clipboardAvailable,
|
||||||
}
|
domain.BuildInfo{
|
||||||
apiClient := pb.NewInternalAPIClient(conn)
|
|
||||||
stream, err := apiClient.Communicate(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("create gRPC stream: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
g.Go(func() error {
|
|
||||||
for {
|
|
||||||
envelope, recErr := stream.Recv()
|
|
||||||
if recErr != nil {
|
|
||||||
return fmt.Errorf("receive envelope: %w", recErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
evt := envelope.GetEvent()
|
|
||||||
if evt == nil {
|
|
||||||
logger.Error("Received envelope without event")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debug("Received event", "type", fmt.Sprintf("%T", evt))
|
|
||||||
bus.Send(protocol.EventFromProto(evt))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ui, err := terminal.NewUI(ctx, terminal.Params{
|
|
||||||
EventBus: bus,
|
|
||||||
Dispatcher: func(cmd event.Command) {
|
|
||||||
logger.Info("Command dispatched", "cmd", cmd.Name())
|
|
||||||
if sendErr := stream.Send(&pb.Envelope{Payload: &pb.Envelope_Command{Command: protocol.CommandToProto(cmd)}}); sendErr != nil {
|
|
||||||
logger.Error("Error sending command to gRPC API", "err", sendErr)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ClipboardAvailable: clipboardAvailable,
|
|
||||||
BuildInfo: domain.BuildInfo{
|
|
||||||
GoVersion: buildInfo.GoVersion,
|
GoVersion: buildInfo.GoVersion,
|
||||||
Version: version,
|
Version: version,
|
||||||
Commit: commit,
|
Commit: commit,
|
||||||
Date: date,
|
Date: date,
|
||||||
},
|
},
|
||||||
Logger: logger.With("component", "ui"),
|
logger,
|
||||||
})
|
)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("start terminal user interface: %w", err)
|
if err := app.Run(ctx); err != nil {
|
||||||
|
return fmt.Errorf("run app: %w", err)
|
||||||
}
|
}
|
||||||
defer ui.Close()
|
|
||||||
|
|
||||||
g.Go(func() error { return ui.Run(ctx) })
|
|
||||||
|
|
||||||
if err := g.Wait(); err == terminal.ErrUserClosed {
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return fmt.Errorf("errgroup.Wait: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,12 @@ func buildAppParams(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildClientServer(
|
||||||
|
t *testing.T,
|
||||||
|
) (*client.App, *app.App) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) {
|
func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
94
internal/client/clientapp.go
Normal file
94
internal/client/clientapp.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.netflux.io/rob/octoplex/internal/domain"
|
||||||
|
"git.netflux.io/rob/octoplex/internal/event"
|
||||||
|
pb "git.netflux.io/rob/octoplex/internal/generated/grpc"
|
||||||
|
"git.netflux.io/rob/octoplex/internal/protocol"
|
||||||
|
"git.netflux.io/rob/octoplex/internal/terminal"
|
||||||
|
"github.com/sagikazarmark/slog-shim"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
bus *event.Bus
|
||||||
|
clipboardAvailable bool
|
||||||
|
buildInfo domain.BuildInfo
|
||||||
|
logger *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApp(
|
||||||
|
bus *event.Bus,
|
||||||
|
clipboardAvailable bool,
|
||||||
|
buildInfo domain.BuildInfo,
|
||||||
|
logger *slog.Logger,
|
||||||
|
) *App {
|
||||||
|
return &App{
|
||||||
|
bus: bus,
|
||||||
|
clipboardAvailable: clipboardAvailable,
|
||||||
|
buildInfo: buildInfo,
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run(ctx context.Context) error {
|
||||||
|
g, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
|
conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect to gRPC server: %w", err)
|
||||||
|
}
|
||||||
|
apiClient := pb.NewInternalAPIClient(conn)
|
||||||
|
stream, err := apiClient.Communicate(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create gRPC stream: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
g.Go(func() error {
|
||||||
|
for {
|
||||||
|
envelope, recErr := stream.Recv()
|
||||||
|
if recErr != nil {
|
||||||
|
return fmt.Errorf("receive envelope: %w", recErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
evt := envelope.GetEvent()
|
||||||
|
if evt == nil {
|
||||||
|
a.logger.Error("Received envelope without event")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
a.logger.Debug("Received event", "type", fmt.Sprintf("%T", evt))
|
||||||
|
a.bus.Send(protocol.EventFromProto(evt))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ui, err := terminal.NewUI(ctx, terminal.Params{
|
||||||
|
EventBus: a.bus,
|
||||||
|
Dispatcher: func(cmd event.Command) {
|
||||||
|
a.logger.Info("Command dispatched", "cmd", cmd.Name())
|
||||||
|
if sendErr := stream.Send(&pb.Envelope{Payload: &pb.Envelope_Command{Command: protocol.CommandToProto(cmd)}}); sendErr != nil {
|
||||||
|
a.logger.Error("Error sending command to gRPC API", "err", sendErr)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ClipboardAvailable: a.clipboardAvailable,
|
||||||
|
BuildInfo: a.buildInfo,
|
||||||
|
Logger: a.logger.With("component", "ui"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("start terminal user interface: %w", err)
|
||||||
|
}
|
||||||
|
defer ui.Close()
|
||||||
|
|
||||||
|
g.Go(func() error { return ui.Run(ctx) })
|
||||||
|
|
||||||
|
if err := g.Wait(); err == terminal.ErrUserClosed {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("errgroup.Wait: %w", err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user