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
@@ -46,6 +51,11 @@ func (p *Printer) startText() string {
46
51
Sprintf (p .BuildInfo ["version" ])
47
52
48
53
text := fmt .Sprintf ("%s %s\n Listening on http://%s:%d" , primary , version , p .Addr , p .Port )
54
+
55
+ if p .Details {
56
+ text = fmt .Sprintf ("%s\n Details: %t" , text , p .Details )
57
+ }
58
+
49
59
return text
50
60
}
51
61
@@ -63,7 +73,7 @@ func (p *Printer) Fatal(err error) {
63
73
}
64
74
65
75
// IncomingRequest handles the output for incoming requests to the server.
66
- func (p * Printer ) IncomingRequest (fields logrequest.RequestFields , params string ) {
76
+ func (p * Printer ) IncomingRequest (fields logrequest.RequestFields , params string , headers map [ string ][] string ) {
67
77
p .Spinner .Stop ()
68
78
prefix := pterm.Prefix {
69
79
Text : fields .Method ,
@@ -72,6 +82,12 @@ func (p *Printer) IncomingRequest(fields logrequest.RequestFields, params string
72
82
73
83
text := p .incomingRequestText (fields , params )
74
84
pterm .Info .WithPrefix (prefix ).Println (text )
85
+
86
+ if p .Details {
87
+ table := p .incomingRequestHeadersTable (headers )
88
+ pterm .Printf ("%s\n \n " , table )
89
+ }
90
+
75
91
p .startSpinner ()
76
92
}
77
93
@@ -85,6 +101,35 @@ func (p *Printer) incomingRequestText(fields logrequest.RequestFields, params st
85
101
return text
86
102
}
87
103
104
+ // incomingRequestHeadersTable constructs the headers table string.
105
+ // This takes the headers map from the request and sorts it alphabetically by key.
106
+ func (p * Printer ) incomingRequestHeadersTable (headers map [string ][]string ) string {
107
+ keys := make ([]string , 0 , len (headers ))
108
+ for key := range headers {
109
+ keys = append (keys , key )
110
+ }
111
+
112
+ sort .Strings (keys )
113
+
114
+ var headersFormatted [][]string
115
+
116
+ headerRow := []string {"Header" , "Value" }
117
+ headersFormatted = append (headersFormatted , headerRow )
118
+
119
+ for _ , key := range keys {
120
+ value := strings .Join (headers [key ], "," )
121
+ headersRow := []string {key , value }
122
+ headersFormatted = append (headersFormatted , headersRow )
123
+ }
124
+
125
+ headersTable , err := pterm .DefaultTable .WithHasHeader ().WithData (headersFormatted ).Srender ()
126
+ if err != nil {
127
+ pterm .Error .WithShowLineNumber (false ).Println (err )
128
+ }
129
+
130
+ return headersTable
131
+ }
132
+
88
133
// Create the spinner which will be displayed at the bottom.
89
134
func (p * Printer ) startSpinner () {
90
135
listeningText := pterm .DefaultBasicText .
0 commit comments