1
- package server
1
+ package protocol
2
2
3
3
import (
4
4
"fmt"
5
5
"log"
6
6
"net/http"
7
+ "sync"
7
8
8
9
"github.com/aaronvb/logparams"
9
10
"github.com/aaronvb/logrequest"
10
- "github.com/aaronvb/request_hole/pkg/renderer"
11
11
"github.com/gorilla/mux"
12
12
"github.com/pterm/pterm"
13
13
)
14
14
15
+ // Http is the protocol for accepting http requests.
15
16
type Http struct {
16
- // Port is the port the HTTP server will run on.
17
- Port int
18
-
19
17
// Addr is the address the HTTP server will bind to.
20
18
Addr string
21
19
20
+ // Port is the port the HTTP server will run on.
21
+ Port int
22
+
22
23
// ResponseCode is the response which out endpoint will return.
23
24
// Default is 200 if no response code is passed.
24
25
ResponseCode int
25
26
26
- // Output is the Renderer interface.
27
- Output renderer.Renderer
28
-
29
- // LogOutput
30
- LogOutput renderer.Renderer
31
-
32
- // Determines if header details should be shown with the request
33
- Details bool
27
+ // rendererChannel is the channel which we send a RequestPayload to when
28
+ // receiving an incoming request to the Http protocol.
29
+ rendererChannels []chan RequestPayload
34
30
}
35
31
36
32
// Start will start the HTTP server.
37
- func (s * Http ) Start () {
38
- s .Output .Start ()
39
- s .LogOutput .Start ()
40
-
33
+ //
34
+ // Sets the channel on our struct so that incoming requests can be sent over it.
35
+ //
36
+ // In the case that we cannot start this server, we send a signal to our quit channel
37
+ // to close renderers.
38
+ func (s * Http ) Start (wg * sync.WaitGroup , c []chan RequestPayload , quits []chan int ) {
41
39
addr := fmt .Sprintf ("%s:%d" , s .Addr , s .Port )
42
- errorLog := log .New (& renderer. PrinterLog { Prefix : pterm . Error }, "" , 0 )
40
+ errorLog := log .New (& httpErrorLog { }, "" , 0 )
43
41
44
42
srv := & http.Server {
45
43
Addr : addr ,
46
44
ErrorLog : errorLog ,
47
45
Handler : s .routes (),
48
46
}
49
47
48
+ s .rendererChannels = c
49
+
50
+ defer wg .Done ()
51
+
50
52
err := srv .ListenAndServe ()
51
- s .Output .Fatal (err )
53
+ str := pterm .Error .WithShowLineNumber (false ).Sprintf ("%s\n " , err )
54
+ pterm .Printo (str ) // Overwrite last line
55
+
56
+ for _ , quit := range quits {
57
+ quit <- 1
58
+ }
52
59
}
53
60
54
61
// routes handles the routes for our HTTP server and currently accepts any path.
55
62
func (s * Http ) routes () http.Handler {
56
63
r := mux .NewRouter ()
57
64
r .PathPrefix ("/" ).HandlerFunc (s .defaultHandler )
58
-
59
65
r .Use (s .logRequest )
60
66
61
67
return r
@@ -75,12 +81,24 @@ func (s *Http) logRequest(next http.Handler) http.Handler {
75
81
fields := lr .ToFields ()
76
82
params := logparams.LogParams {Request : r , HidePrefix : true }
77
83
78
- s .Output .IncomingRequest (fields , params .ToString ())
79
- s .LogOutput .IncomingRequest (fields , params .ToString ())
84
+ req := RequestPayload {
85
+ Fields : fields ,
86
+ Params : params .ToString (),
87
+ Headers : r .Header ,
88
+ }
80
89
81
- if s .Details {
82
- s .Output .IncomingRequestHeaders (r .Header )
83
- s .LogOutput .IncomingRequestHeaders (r .Header )
90
+ for _ , rendererChannel := range s .rendererChannels {
91
+ rendererChannel <- req
84
92
}
85
93
})
86
94
}
95
+
96
+ // httpErrorLog implements the logger interface.
97
+ type httpErrorLog struct {}
98
+
99
+ // Write let's us override the logger required for http errors and
100
+ // prints to the terminal using pterm.
101
+ func (e * httpErrorLog ) Write (b []byte ) (n int , err error ) {
102
+ pterm .Error .WithShowLineNumber (false ).Println (string (b ))
103
+ return len (b ), nil
104
+ }
0 commit comments