implement tui for reserved sharing (#56)

This commit is contained in:
Michael Quigley 2023-01-10 17:21:15 -05:00
parent 0516f28b72
commit 72cf0b54ad
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/endpoints" "github.com/openziti-test-kitchen/zrok/endpoints"
"github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend" "github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend"
@ -13,7 +15,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"time"
) )
func init() { func init() {
@ -22,6 +23,7 @@ func init() {
type shareReservedCommand struct { type shareReservedCommand struct {
overrideEndpoint string overrideEndpoint string
headless bool
cmd *cobra.Command cmd *cobra.Command
} }
@ -32,6 +34,7 @@ func newShareReservedCommand() *shareReservedCommand {
} }
command := &shareReservedCommand{cmd: cmd} command := &shareReservedCommand{cmd: cmd}
cmd.Flags().StringVar(&command.overrideEndpoint, "override-endpoint", "", "Override the stored target endpoint with a replacement") cmd.Flags().StringVar(&command.overrideEndpoint, "override-endpoint", "", "Override the stored target endpoint with a replacement")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Run = command.run cmd.Run = command.run
return command return command
} }
@ -100,12 +103,14 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
logrus.Infof("using existing backend proxy endpoint: %v", target) logrus.Infof("using existing backend proxy endpoint: %v", target)
} }
requestsChan := make(chan *endpoints.BackendRequest, 1024)
switch resp.Payload.BackendMode { switch resp.Payload.BackendMode {
case "proxy": case "proxy":
cfg := &proxyBackend.Config{ cfg := &proxyBackend.Config{
IdentityPath: zif, IdentityPath: zif,
EndpointAddress: target, EndpointAddress: target,
ShrToken: shrToken, ShrToken: shrToken,
RequestsChan: requestsChan,
} }
_, err := cmd.proxyBackendMode(cfg) _, err := cmd.proxyBackendMode(cfg)
if err != nil { if err != nil {
@ -120,6 +125,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
IdentityPath: zif, IdentityPath: zif,
WebRoot: target, WebRoot: target,
ShrToken: shrToken, ShrToken: shrToken,
RequestsChan: requestsChan,
} }
_, err := cmd.webBackendMode(cfg) _, err := cmd.webBackendMode(cfg)
if err != nil { if err != nil {
@ -133,16 +139,46 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
tui.Error("invalid backend mode", nil) tui.Error("invalid backend mode", nil)
} }
switch resp.Payload.ShareMode { if cmd.headless {
case "public": switch resp.Payload.ShareMode {
logrus.Infof("access your zrok share: %v", resp.Payload.FrontendEndpoint) case "public":
logrus.Infof("access your zrok share: %v", resp.Payload.FrontendEndpoint)
case "private": case "private":
logrus.Infof("use this command to access your zrok share: 'zrok access private %v'", shrToken) logrus.Infof("use this command to access your zrok share: 'zrok access private %v'", shrToken)
} }
for {
select {
case req := <-requestsChan:
logrus.Infof("%v -> %v %v", req.RemoteAddr, req.Method, req.Path)
}
}
} else {
var shareDescription string
switch resp.Payload.ShareMode {
case "public":
shareDescription = resp.Payload.FrontendEndpoint
case "private":
shareDescription = fmt.Sprintf("access your share with: %v", tui.CodeStyle.Render(fmt.Sprintf("zrok access private %v", shrToken)))
}
for { mdl := newShareModel(shrToken, []string{shareDescription}, resp.Payload.ShareMode, resp.Payload.BackendMode)
time.Sleep(30 * time.Second) prg := tea.NewProgram(mdl, tea.WithAltScreen())
go func() {
for {
select {
case req := <-requestsChan:
prg.Send(req)
}
}
}()
if _, err := prg.Run(); err != nil {
tui.Error("An error occurred", err)
}
close(requestsChan)
} }
} }