Skip to content

Commit fdeed0a

Browse files
committed
Add channel support to printer
- Removed unnecsary struct fields. Most of these were for printing the start text but that's now handled in the server package. - Added a channel and receive from it. - Move headers back into the incoming request. We now use a struct, RequestPayload, which contains all the incoming request data.
1 parent 55c9ea0 commit fdeed0a

File tree

2 files changed

+40
-122
lines changed

2 files changed

+40
-122
lines changed

pkg/renderer/printer.go

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package renderer
22

33
import (
44
"fmt"
5-
"os"
65
"sort"
76
"strings"
7+
"sync"
88

9-
"github.com/aaronvb/logrequest"
9+
"github.com/aaronvb/request_hole/pkg/protocol"
1010
"github.com/pterm/pterm"
1111
)
1212

@@ -16,102 +16,67 @@ type Printer struct {
1616
// Spinner is a constant, we set it during the start method and can later stop it when we exit.
1717
Spinner *pterm.SpinnerPrinter
1818

19-
// Fields for startText
20-
Port int
21-
Addr string
22-
23-
// Contains build info
24-
BuildInfo map[string]string
25-
26-
// Log file location for the CLI header that shows the user
27-
// the entered log file location. Not used for writing to
28-
LogFile string
29-
30-
// Details used in the header to show the user if they passed
31-
// the flag.
19+
// Details will output the headers with the request. Default is false unless the
20+
// flag is passed.
3221
Details bool
3322
}
3423

35-
// Start renders the initial header and the spinner. The Spinner should be consistent during
36-
// all requests, unless we explicitly tell it to stop.
37-
func (p *Printer) Start() {
38-
// Clear the terminal
39-
clear()
40-
41-
text := p.startText()
42-
pterm.DefaultBox.
43-
WithBoxStyle(pterm.NewStyle(pterm.FgGray)).
44-
Printfln(text)
24+
// Start renders the spinner and starts receive incoming requests from the channel.
25+
func (p *Printer) Start(wg *sync.WaitGroup, rp chan protocol.RequestPayload, q chan int) {
26+
defer wg.Done()
4527

4628
p.startSpinner()
47-
}
48-
49-
func (p *Printer) startText() string {
50-
primary := pterm.DefaultBasicText.
51-
WithStyle(pterm.NewStyle(pterm.Bold)).
52-
Sprintf("Request Hole")
53-
version := pterm.DefaultBasicText.
54-
WithStyle(pterm.NewStyle(pterm.Fuzzy)).
55-
Sprintf(p.BuildInfo["version"])
56-
57-
text := fmt.Sprintf("%s %s\nListening on http://%s:%d", primary, version, p.Addr, p.Port)
5829

59-
if p.Details {
60-
text = fmt.Sprintf("%s\nDetails: %t", text, p.Details)
30+
// Receive incoming requests on RequestPayload channel or
31+
// exit blocking select if quit is received from protocol
32+
for {
33+
select {
34+
case r := <-rp:
35+
p.incomingRequest(r)
36+
case <-q:
37+
return
38+
}
6139
}
62-
if p.LogFile != "" {
63-
text = fmt.Sprintf("%s\nLog: %s", text, p.LogFile)
64-
}
65-
66-
return text
6740
}
6841

69-
// Fatal will use the Error prefix to render the error and then exit the CLI.
70-
func (p *Printer) Fatal(err error) {
42+
// incomingRequest handles the output for incoming requests to the protocol.
43+
func (p *Printer) incomingRequest(r protocol.RequestPayload) {
7144
p.Spinner.Stop()
72-
pterm.Error.WithShowLineNumber(false).Println(err)
73-
os.Exit(1)
74-
}
7545

76-
// IncomingRequest handles the output for incoming requests to the server.
77-
func (p *Printer) IncomingRequest(fields logrequest.RequestFields, params string) {
78-
p.Spinner.Stop()
7946
prefix := pterm.Prefix{
80-
Text: fields.Method,
47+
Text: r.Fields.Method,
8148
Style: pterm.NewStyle(pterm.BgGray, pterm.FgWhite),
8249
}
8350

84-
text := p.incomingRequestText(fields, params)
51+
text := p.incomingRequestText(r)
8552
pterm.Info.WithPrefix(prefix).Println(text)
8653

87-
p.startSpinner()
88-
}
89-
90-
// IncomingRequestHeader handles the output for incoming requests headers to the server.
91-
func (p *Printer) IncomingRequestHeaders(headers map[string][]string) {
92-
p.Spinner.Stop()
93-
94-
table := p.incomingRequestHeadersTable(headers)
95-
pterm.Printf("%s\n\n", table)
54+
// If the details flag is passed we print headers as well,
55+
// default is false if no flag is passed.
56+
if p.Details {
57+
table := p.incomingRequestHeadersTable(r)
58+
pterm.Printf("%s\n", table)
59+
}
9660

9761
p.startSpinner()
9862
}
9963

100-
func (p *Printer) incomingRequestText(fields logrequest.RequestFields, params string) string {
64+
// incomingRequestText converts the RequestPayload into a printable string.
65+
func (p *Printer) incomingRequestText(r protocol.RequestPayload) string {
10166
urlWithStyle := pterm.DefaultBasicText.
102-
WithStyle(pterm.NewStyle(pterm.FgWhite)).Sprintf(fields.Url)
67+
WithStyle(pterm.NewStyle(pterm.FgWhite)).Sprintf(r.Fields.Url)
10368
paramsWithStyle := pterm.DefaultBasicText.
104-
WithStyle(pterm.NewStyle(pterm.Fuzzy)).Sprintf(params)
69+
WithStyle(pterm.NewStyle(pterm.Fuzzy)).Sprintf(r.Params)
10570

10671
text := fmt.Sprintf("%s %s", urlWithStyle, paramsWithStyle)
10772
return text
10873
}
10974

110-
// incomingRequestHeadersTable constructs the headers table string.
75+
// incomingRequestHeadersTable constructs the headers table string from the RequestPayload.
11176
// This takes the headers map from the request and sorts it alphabetically by key.
112-
func (p *Printer) incomingRequestHeadersTable(headers map[string][]string) string {
113-
keys := make([]string, 0, len(headers))
114-
for key := range headers {
77+
func (p *Printer) incomingRequestHeadersTable(r protocol.RequestPayload) string {
78+
keys := make([]string, 0, len(r.Headers))
79+
for key := range r.Headers {
11580
keys = append(keys, key)
11681
}
11782

@@ -123,7 +88,7 @@ func (p *Printer) incomingRequestHeadersTable(headers map[string][]string) strin
12388
headersFormatted = append(headersFormatted, headerRow)
12489

12590
for _, key := range keys {
126-
value := strings.Join(headers[key], ",")
91+
value := strings.Join(r.Headers[key], ",")
12792
headersRow := []string{key, value}
12893
headersFormatted = append(headersFormatted, headersRow)
12994
}
@@ -157,8 +122,3 @@ func (p *Printer) startSpinner() {
157122

158123
p.Spinner = spinner
159124
}
160-
161-
// clear will clear the terminal, called at the start.
162-
func clear() {
163-
print("\033[H\033[2J")
164-
}

pkg/renderer/printer_test.go

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,10 @@ import (
55
"testing"
66

77
"github.com/aaronvb/logrequest"
8+
"github.com/aaronvb/request_hole/pkg/protocol"
89
"github.com/pterm/pterm"
910
)
1011

11-
func TestStartText(t *testing.T) {
12-
pterm.DisableColor()
13-
printer := Printer{Addr: "localhost", Port: 8080, BuildInfo: map[string]string{"version": "dev"}}
14-
result := printer.startText()
15-
expected := fmt.Sprintf("Request Hole %s\nListening on http://%s:%d", "dev", printer.Addr, printer.Port)
16-
17-
if result != expected {
18-
t.Errorf("Expected %s, got %s", expected, result)
19-
}
20-
}
21-
22-
func TestStartTextWithDetails(t *testing.T) {
23-
pterm.DisableColor()
24-
printer := Printer{
25-
Addr: "localhost",
26-
Port: 8080,
27-
BuildInfo: map[string]string{"version": "dev"},
28-
Details: true}
29-
result := printer.startText()
30-
expected := fmt.Sprintf(
31-
"Request Hole %s\nListening on http://%s:%d\nDetails: %t", "dev",
32-
printer.Addr, printer.Port, printer.Details)
33-
34-
if result != expected {
35-
t.Errorf("Expected %s, got %s", expected, result)
36-
}
37-
}
38-
39-
func TestStartTextWithLogFile(t *testing.T) {
40-
pterm.DisableColor()
41-
printer := Printer{
42-
Addr: "localhost",
43-
Port: 8080,
44-
BuildInfo: map[string]string{"version": "dev"},
45-
LogFile: "rh.log"}
46-
result := printer.startText()
47-
expected := fmt.Sprintf(
48-
"Request Hole %s\nListening on http://%s:%d\nLog: %s", "dev",
49-
printer.Addr, printer.Port, printer.LogFile)
50-
51-
if result != expected {
52-
t.Errorf("Expected %s, got %s", expected, result)
53-
}
54-
}
55-
5612
func TestIncomingRequestText(t *testing.T) {
5713
pterm.DisableColor()
5814
printer := Printer{}
@@ -61,7 +17,8 @@ func TestIncomingRequestText(t *testing.T) {
6117
Url: "/foobar",
6218
}
6319
params := "{\"foo\" => \"bar\"}"
64-
result := printer.incomingRequestText(fields, params)
20+
rp := protocol.RequestPayload{Fields: fields, Params: params}
21+
result := printer.incomingRequestText(rp)
6522
expected := fmt.Sprintf("%s %s", fields.Url, params)
6623

6724
if result != expected {
@@ -76,7 +33,8 @@ func TestIncomingRequestHeadersTables(t *testing.T) {
7633
"hello": {"world", "foobar"},
7734
"foo": {"bar"},
7835
}
79-
result := printer.incomingRequestHeadersTable(headers)
36+
rp := protocol.RequestPayload{Headers: headers}
37+
result := printer.incomingRequestHeadersTable(rp)
8038

8139
headersForTable := [][]string{}
8240
headersForTable = append(headersForTable, []string{"Header", "Value"})

0 commit comments

Comments
 (0)