4
4
"fmt"
5
5
"log"
6
6
"os"
7
+ "sort"
8
+ "strings"
7
9
8
10
"github.com/aaronvb/logrequest"
9
11
"github.com/pterm/pterm"
@@ -21,6 +23,9 @@ type Printer struct {
21
23
22
24
// Contains build info
23
25
BuildInfo map [string ]string
26
+
27
+ // Determines if header details should be shown with the request
28
+ Details bool
24
29
}
25
30
26
31
// Start renders the initial header and the spinner. The Spinner should be consistent during
@@ -63,7 +68,7 @@ func (p *Printer) Fatal(err error) {
63
68
}
64
69
65
70
// IncomingRequest handles the output for incoming requests to the server.
66
- func (p * Printer ) IncomingRequest (fields logrequest.RequestFields , params string ) {
71
+ func (p * Printer ) IncomingRequest (fields logrequest.RequestFields , params string , headers map [ string ][] string ) {
67
72
p .Spinner .Stop ()
68
73
prefix := pterm.Prefix {
69
74
Text : fields .Method ,
@@ -72,6 +77,12 @@ func (p *Printer) IncomingRequest(fields logrequest.RequestFields, params string
72
77
73
78
text := p .incomingRequestText (fields , params )
74
79
pterm .Info .WithPrefix (prefix ).Println (text )
80
+
81
+ if p .Details {
82
+ table := p .incomingRequestHeadersTable (headers )
83
+ pterm .Printf ("%s\n \n " , table )
84
+ }
85
+
75
86
p .startSpinner ()
76
87
}
77
88
@@ -85,6 +96,35 @@ func (p *Printer) incomingRequestText(fields logrequest.RequestFields, params st
85
96
return text
86
97
}
87
98
99
+ // incomingRequestHeadersTable constructs the headers table string.
100
+ // This takes the headers map from the request and sorts it alphabetically by key.
101
+ func (p * Printer ) incomingRequestHeadersTable (headers map [string ][]string ) string {
102
+ keys := make ([]string , 0 , len (headers ))
103
+ for key := range headers {
104
+ keys = append (keys , key )
105
+ }
106
+
107
+ sort .Strings (keys )
108
+
109
+ var headersFormatted [][]string
110
+
111
+ headerRow := []string {"Header" , "Value" }
112
+ headersFormatted = append (headersFormatted , headerRow )
113
+
114
+ for _ , key := range keys {
115
+ value := strings .Join (headers [key ], "," )
116
+ headersRow := []string {key , value }
117
+ headersFormatted = append (headersFormatted , headersRow )
118
+ }
119
+
120
+ headersTable , err := pterm .DefaultTable .WithHasHeader ().WithData (headersFormatted ).Srender ()
121
+ if err != nil {
122
+ pterm .Error .WithShowLineNumber (false ).Println (err )
123
+ }
124
+
125
+ return headersTable
126
+ }
127
+
88
128
// Create the spinner which will be displayed at the bottom.
89
129
func (p * Printer ) startSpinner () {
90
130
listeningText := pterm .DefaultBasicText .
0 commit comments