@@ -2,11 +2,11 @@ package renderer
2
2
3
3
import (
4
4
"fmt"
5
- "os"
6
5
"sort"
7
6
"strings"
7
+ "sync"
8
8
9
- "github.com/aaronvb/logrequest "
9
+ "github.com/aaronvb/request_hole/pkg/protocol "
10
10
"github.com/pterm/pterm"
11
11
)
12
12
@@ -16,102 +16,67 @@ type Printer struct {
16
16
// Spinner is a constant, we set it during the start method and can later stop it when we exit.
17
17
Spinner * pterm.SpinnerPrinter
18
18
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.
32
21
Details bool
33
22
}
34
23
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 ()
45
27
46
28
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\n Listening on http://%s:%d" , primary , version , p .Addr , p .Port )
58
29
59
- if p .Details {
60
- text = fmt .Sprintf ("%s\n Details: %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
+ }
61
39
}
62
- if p .LogFile != "" {
63
- text = fmt .Sprintf ("%s\n Log: %s" , text , p .LogFile )
64
- }
65
-
66
- return text
67
40
}
68
41
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 ) {
71
44
p .Spinner .Stop ()
72
- pterm .Error .WithShowLineNumber (false ).Println (err )
73
- os .Exit (1 )
74
- }
75
45
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 ()
79
46
prefix := pterm.Prefix {
80
- Text : fields .Method ,
47
+ Text : r . Fields .Method ,
81
48
Style : pterm .NewStyle (pterm .BgGray , pterm .FgWhite ),
82
49
}
83
50
84
- text := p .incomingRequestText (fields , params )
51
+ text := p .incomingRequestText (r )
85
52
pterm .Info .WithPrefix (prefix ).Println (text )
86
53
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
+ }
96
60
97
61
p .startSpinner ()
98
62
}
99
63
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 {
101
66
urlWithStyle := pterm .DefaultBasicText .
102
- WithStyle (pterm .NewStyle (pterm .FgWhite )).Sprintf (fields .Url )
67
+ WithStyle (pterm .NewStyle (pterm .FgWhite )).Sprintf (r . Fields .Url )
103
68
paramsWithStyle := pterm .DefaultBasicText .
104
- WithStyle (pterm .NewStyle (pterm .Fuzzy )).Sprintf (params )
69
+ WithStyle (pterm .NewStyle (pterm .Fuzzy )).Sprintf (r . Params )
105
70
106
71
text := fmt .Sprintf ("%s %s" , urlWithStyle , paramsWithStyle )
107
72
return text
108
73
}
109
74
110
- // incomingRequestHeadersTable constructs the headers table string.
75
+ // incomingRequestHeadersTable constructs the headers table string from the RequestPayload .
111
76
// 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 {
115
80
keys = append (keys , key )
116
81
}
117
82
@@ -123,7 +88,7 @@ func (p *Printer) incomingRequestHeadersTable(headers map[string][]string) strin
123
88
headersFormatted = append (headersFormatted , headerRow )
124
89
125
90
for _ , key := range keys {
126
- value := strings .Join (headers [key ], "," )
91
+ value := strings .Join (r . Headers [key ], "," )
127
92
headersRow := []string {key , value }
128
93
headersFormatted = append (headersFormatted , headersRow )
129
94
}
@@ -157,8 +122,3 @@ func (p *Printer) startSpinner() {
157
122
158
123
p .Spinner = spinner
159
124
}
160
-
161
- // clear will clear the terminal, called at the start.
162
- func clear () {
163
- print ("\033 [H\033 [2J" )
164
- }
0 commit comments