From 2983d16a064b739a978200b6dc16f92d195c4298 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 16 Aug 2022 13:31:45 -0400 Subject: [PATCH] 'zrok http -s' for 'service mode' (no tui chrome) --- cmd/zrok/http.go | 115 ++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 52 deletions(-) diff --git a/cmd/zrok/http.go b/cmd/zrok/http.go index 3964e2eb..968b5f07 100644 --- a/cmd/zrok/http.go +++ b/cmd/zrok/http.go @@ -28,6 +28,7 @@ func init() { } type httpCommand struct { + service bool basicAuth []string cmd *cobra.Command } @@ -39,17 +40,20 @@ func newHttpCommand() *httpCommand { Args: cobra.ExactArgs(1), } command := &httpCommand{cmd: cmd} + cmd.Flags().BoolVarP(&command.service, "service", "s", false, "Disable TUI 'chrome' for service operation") cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (,...") cmd.Run = command.run return command } func (self *httpCommand) run(_ *cobra.Command, args []string) { - if err := ui.Init(); err != nil { - panic(err) + if !self.service { + if err := ui.Init(); err != nil { + panic(err) + } + defer ui.Close() + tb.SetInputMode(tb.InputEsc) } - defer ui.Close() - tb.SetInputMode(tb.InputEsc) idCfg, err := zrokdir.IdentityConfigFile() if err != nil { @@ -113,63 +117,70 @@ func (self *httpCommand) run(_ *cobra.Command, args []string) { } }() - ui.Clear() - w, h := ui.TerminalDimensions() + if !self.service { + ui.Clear() + w, h := ui.TerminalDimensions() - p := widgets.NewParagraph() - p.Border = true - p.Title = " access your zrok service " - p.Text = fmt.Sprintf("%v%v", strings.Repeat(" ", (((w-12)-len(resp.Payload.ProxyEndpoint))/2)-1), resp.Payload.ProxyEndpoint) - p.TextStyle = ui.Style{Fg: ui.ColorWhite} - p.PaddingTop = 1 - p.SetRect(5, 5, w-10, 10) + p := widgets.NewParagraph() + p.Border = true + p.Title = " access your zrok service " + p.Text = fmt.Sprintf("%v%v", strings.Repeat(" ", (((w-12)-len(resp.Payload.ProxyEndpoint))/2)-1), resp.Payload.ProxyEndpoint) + p.TextStyle = ui.Style{Fg: ui.ColorWhite} + p.PaddingTop = 1 + p.SetRect(5, 5, w-10, 10) - lastRequests := float64(0) - var requestData []float64 - spk := widgets.NewSparkline() - spk.Title = " requests " - spk.Data = requestData - spk.LineColor = ui.ColorCyan + lastRequests := float64(0) + var requestData []float64 + spk := widgets.NewSparkline() + spk.Title = " requests " + spk.Data = requestData + spk.LineColor = ui.ColorCyan - slg := widgets.NewSparklineGroup(spk) - slg.SetRect(5, 11, w-10, h-5) + slg := widgets.NewSparklineGroup(spk) + slg.SetRect(5, 11, w-10, h-5) - ui.Render(p, slg) + ui.Render(p, slg) - ticker := time.NewTicker(time.Second).C - uiEvents := ui.PollEvents() - for { - select { - case e := <-uiEvents: - switch e.Type { - case ui.ResizeEvent: - ui.Clear() - w, h = ui.TerminalDimensions() - p.SetRect(5, 5, w-10, 10) - slg.SetRect(5, 11, w-10, h-5) - ui.Render(p, slg) + ticker := time.NewTicker(time.Second).C + uiEvents := ui.PollEvents() + for { + select { + case e := <-uiEvents: + switch e.Type { + case ui.ResizeEvent: + ui.Clear() + w, h = ui.TerminalDimensions() + p.SetRect(5, 5, w-10, 10) + slg.SetRect(5, 11, w-10, h-5) + ui.Render(p, slg) - case ui.KeyboardEvent: - switch e.ID { - case "q", "": - ui.Close() - cleanupHttp(id, cfg, zrok, auth) - os.Exit(0) + case ui.KeyboardEvent: + switch e.ID { + case "q", "": + ui.Close() + cleanupHttp(id, cfg, zrok, auth) + os.Exit(0) + } } - } - case <-ticker: - currentRequests := float64(httpProxy.Requests()) - deltaRequests := currentRequests - lastRequests - requestData = append(requestData, deltaRequests) - lastRequests = currentRequests - requestData = append(requestData, deltaRequests) - for len(requestData) > w-17 { - requestData = requestData[1:] + case <-ticker: + currentRequests := float64(httpProxy.Requests()) + deltaRequests := currentRequests - lastRequests + requestData = append(requestData, deltaRequests) + lastRequests = currentRequests + requestData = append(requestData, deltaRequests) + for len(requestData) > w-17 { + requestData = requestData[1:] + } + spk.Title = fmt.Sprintf(" requests (%d) ", int(currentRequests)) + spk.Data = requestData + ui.Render(p, slg) } - spk.Title = fmt.Sprintf(" requests (%d) ", int(currentRequests)) - spk.Data = requestData - ui.Render(p, slg) + } + } else { + for { + logrus.Infof("access your zrok service: %v", resp.Payload.ProxyEndpoint) + time.Sleep(30 * time.Second) } } }