@@ -5,89 +5,87 @@ import (
5
5
"os"
6
6
"sort"
7
7
"strings"
8
+ "sync"
8
9
"time"
9
10
10
- "github.com/aaronvb/logrequest "
11
+ "github.com/aaronvb/request_hole/pkg/protocol "
11
12
"github.com/pterm/pterm"
12
13
)
13
14
14
15
// Logger outputs to a log file.
15
16
type Logger struct {
16
- // File points to the file that we write to.
17
- File string
17
+ // FilePathis the path to the log file which we write to.
18
+ // Default is blank unless passed as a flag to the CLI.
19
+ FilePath string
18
20
19
- // Fields for startText
20
- Port int
21
+ // Details will log the headers with the request. Default is false unless the
22
+ // flag is passed.
23
+ Details bool
24
+
25
+ // Address and Port are used for the start text
21
26
Addr string
27
+ Port int
28
+
29
+ // LogFile is the open log file
30
+ logFile * os.File
22
31
}
23
32
24
33
// Start writes the initial server start to the log file.
25
- func (l * Logger ) Start () {
26
- if l .File == "" {
27
- return
28
- }
29
-
30
- f , err := os .OpenFile (l .File , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
34
+ func (l * Logger ) Start (wg * sync.WaitGroup , rp chan protocol.RequestPayload , q chan int ) {
35
+ f , err := os .OpenFile (l .FilePath , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
31
36
if err != nil {
32
- l . Fatal (err )
37
+ fatal (err )
33
38
}
34
39
35
40
defer f .Close ()
41
+ defer wg .Done ()
36
42
37
43
str := fmt .Sprintf ("%s: %s\n " , time .Now ().Format ("2006/02/01 15:04:05" ), l .startText ())
38
44
f .WriteString (str )
45
+
46
+ // Set logFile to open file
47
+ l .logFile = f
48
+
49
+ // Receive incoming requests on RequestPayload channel or
50
+ // exit blocking select if quit is received from protocol
51
+ for {
52
+ select {
53
+ case r := <- rp :
54
+ l .incomingRequest (r )
55
+ case <- q :
56
+ return
57
+ }
58
+ }
39
59
}
40
60
61
+ // startText returns the starting log string
41
62
func (l * Logger ) startText () string {
42
63
return fmt .Sprintf ("Listening on http://%s:%d" , l .Addr , l .Port )
43
64
}
44
65
45
- // Fatal will use the Error prefix to render the error and then exit the CLI.
46
- func ( l * Logger ) Fatal (err error ) {
66
+ // fatal will use the Error prefix to render the error and then exit the CLI.
67
+ func fatal (err error ) {
47
68
pterm .Error .WithShowLineNumber (false ).Println (err )
48
69
os .Exit (1 )
49
70
}
50
71
51
- // IncomingRequest writes the incoming requests to the log file.
52
- func (l * Logger ) IncomingRequest (fields logrequest.RequestFields , params string ) {
53
- if l .File == "" {
54
- return
55
- }
56
-
57
- f , err := os .OpenFile (l .File , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
58
- if err != nil {
59
- l .Fatal (err )
72
+ // incomingRequest handles the log output for incoming requests to the protocol..
73
+ func (l * Logger ) incomingRequest (r protocol.RequestPayload ) {
74
+ str := fmt .Sprintf ("%s: %s\n " , time .Now ().Format ("2006/02/01 15:04:05" ), l .incomingRequestText (r ))
75
+ l .logFile .WriteString (str )
76
+
77
+ if l .Details {
78
+ headersWithJoinedValues , keys := l .incomingRequestHeaders (r .Headers )
79
+ for _ , key := range keys {
80
+ str := fmt .Sprintf ("%s: %s: %s\n " , time .Now ().Format ("2006/02/01 15:04:05" ), key , headersWithJoinedValues [key ])
81
+ l .logFile .WriteString (str )
82
+ }
60
83
}
61
-
62
- defer f .Close ()
63
-
64
- str := fmt .Sprintf ("%s: %s\n " , time .Now ().Format ("2006/02/01 15:04:05" ), l .incomingRequestText (fields , params ))
65
- f .WriteString (str )
66
- }
67
-
68
- func (l * Logger ) incomingRequestText (fields logrequest.RequestFields , params string ) string {
69
- return fmt .Sprintf ("%s %s %s" , fields .Method , fields .Url , params )
70
84
}
71
85
72
- // IncomingRequestHeaders writes the incoming request headers to the log file
73
- func (l * Logger ) IncomingRequestHeaders (headers map [string ][]string ) {
74
- if l .File == "" {
75
- return
76
- }
77
-
78
- f , err := os .OpenFile (l .File , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
79
- if err != nil {
80
- l .Fatal (err )
81
- }
82
-
83
- defer f .Close ()
84
-
85
- headersWithJoinedValues , keys := l .incomingRequestHeaders (headers )
86
-
87
- for _ , key := range keys {
88
- str := fmt .Sprintf ("%s: %s: %s\n " , time .Now ().Format ("2006/02/01 15:04:05" ), key , headersWithJoinedValues [key ])
89
- f .WriteString (str )
90
- }
86
+ // incomingRequestText converts the RequestPayload into a printable string.
87
+ func (l * Logger ) incomingRequestText (r protocol.RequestPayload ) string {
88
+ return fmt .Sprintf ("%s %s %s" , r .Fields .Method , r .Fields .Url , r .Params )
91
89
}
92
90
93
91
// incomingRequestHeaders takes the headers from the request, sorts them alphabetically,
0 commit comments