Skip to content

Commit 77bfb4b

Browse files
committed
Move details to http and add new function to iface
Instead of doing the details check in the printer we now handle that in the http server and call an interface function on renderer.
1 parent eaea731 commit 77bfb4b

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

cmd/protocol.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,25 @@ func init() {
2020
}
2121

2222
func http(cmd *cobra.Command, args []string) {
23-
renderer := &renderer.Printer{Port: Port, Addr: Address, BuildInfo: BuildInfo, Details: Details}
24-
httpServer := server.Http{Addr: Address, Port: Port, ResponseCode: ResponseCode, Output: renderer}
23+
logOutput := &renderer.Logger{
24+
File: LogFile,
25+
Port: Port,
26+
Addr: Address,
27+
}
28+
output := &renderer.Printer{
29+
Port: Port,
30+
Addr: Address,
31+
BuildInfo: BuildInfo,
32+
LogFile: LogFile,
33+
Details: Details}
34+
35+
httpServer := server.Http{
36+
Addr: Address,
37+
Port: Port,
38+
ResponseCode: ResponseCode,
39+
Output: output,
40+
LogOutput: logOutput,
41+
Details: Details}
42+
2543
httpServer.Start()
2644
}

pkg/renderer/printer.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ func (p *Printer) IncomingRequest(fields logrequest.RequestFields, params string
8383
text := p.incomingRequestText(fields, params)
8484
pterm.Info.WithPrefix(prefix).Println(text)
8585

86-
if p.Details {
87-
table := p.incomingRequestHeadersTable(headers)
88-
pterm.Printf("%s\n\n", table)
89-
}
86+
p.startSpinner()
87+
}
88+
89+
// IncomingRequestHeader handles the output for incoming requests headers to the server.
90+
func (p *Printer) IncomingRequestHeaders(headers map[string][]string) {
91+
p.Spinner.Stop()
92+
93+
table := p.incomingRequestHeadersTable(headers)
94+
pterm.Printf("%s\n\n", table)
9095

9196
p.startSpinner()
9297
}

pkg/renderer/renderer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ type Renderer interface {
1818
// Fatal is used when we need to display a message and should always exit the CLI.
1919
Fatal(error)
2020

21-
IncomingRequest(logrequest.RequestFields, string, map[string][]string)
21+
// IncomingRequest is called when we receive an incoming request to the server.
22+
IncomingRequest(logrequest.RequestFields, string)
23+
24+
// IncomingRequestHeaders is called when the details flag is passed and we want to
25+
// render the headers.
26+
IncomingRequestHeaders(map[string][]string)
2227
}

pkg/server/http.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Http struct {
2323

2424
// Output is the Renderer interface.
2525
Output renderer.Renderer
26+
// Determines if header details should be shown with the request
27+
Details bool
2628
}
2729

2830
// Start will start the HTTP server.
@@ -64,5 +66,9 @@ func (s *Http) logRequest(next http.Handler) http.Handler {
6466
fields := lr.ToFields()
6567
params := logparams.LogParams{Request: r, HidePrefix: true}
6668
s.Output.IncomingRequest(fields, params.ToString(), r.Header)
69+
if s.Details {
70+
s.Output.IncomingRequestHeaders(r.Header)
71+
s.LogOutput.IncomingRequestHeaders(r.Header)
72+
}
6773
})
6874
}

pkg/server/http_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ func (mp *MockPrinter) Fatal(error) {}
2121
func (mp *MockPrinter) Start() {}
2222
func (mp *MockPrinter) ErrorLogger() *log.Logger { return log.New(os.Stderr, "", 0) }
2323
func (mp *MockPrinter) IncomingRequest(fields logrequest.RequestFields, params string, headers map[string][]string) {
24+
25+
func (mp *MockPrinter) IncomingRequest(fields logrequest.RequestFields, params string) {
2426
mp.fields = fields
2527
mp.params = params
28+
}
29+
30+
func (mp *MockPrinter) IncomingRequestHeaders(headers map[string][]string) {
2631
mp.headers = headers
2732
}
2833

0 commit comments

Comments
 (0)