fixup! wip: refactor: API
This commit is contained in:
parent
888ac7d67d
commit
7706bb363f
@ -7,15 +7,10 @@ import (
|
||||
"os"
|
||||
"runtime/debug"
|
||||
|
||||
"git.netflux.io/rob/octoplex/internal/client"
|
||||
"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"
|
||||
"golang.design/x/clipboard"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -38,8 +33,6 @@ func run() error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
// TODO: logger from config
|
||||
fptr, err := os.OpenFile("octoplex.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
@ -47,8 +40,6 @@ func run() error {
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(fptr, nil))
|
||||
|
||||
bus := event.NewBus(logger)
|
||||
|
||||
var clipboardAvailable bool
|
||||
if err = clipboard.Init(); err != nil {
|
||||
logger.Warn("Clipboard not available", "err", err)
|
||||
@ -61,61 +52,21 @@ func run() error {
|
||||
return fmt.Errorf("read build info: %w", err)
|
||||
}
|
||||
|
||||
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 {
|
||||
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{
|
||||
app := client.NewApp(
|
||||
event.NewBus(logger),
|
||||
clipboardAvailable,
|
||||
domain.BuildInfo{
|
||||
GoVersion: buildInfo.GoVersion,
|
||||
Version: version,
|
||||
Commit: commit,
|
||||
Date: date,
|
||||
},
|
||||
Logger: logger.With("component", "ui"),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("start terminal user interface: %w", err)
|
||||
}
|
||||
defer ui.Close()
|
||||
logger,
|
||||
)
|
||||
|
||||
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)
|
||||
if err := app.Run(ctx); err != nil {
|
||||
return fmt.Errorf("run app: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -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) {
|
||||
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