Skip to content

Commit a27c50b

Browse files
authored
Merge pull request #7 from aaronvb/refactor-to-channels
Refactor to channels
2 parents 251ebe2 + 3b8c326 commit a27c50b

File tree

18 files changed

+573
-412
lines changed

18 files changed

+573
-412
lines changed

.github/workflows/builds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
name: Set up Go
2020
uses: actions/setup-go@v2
2121
with:
22-
go-version: 1.15
22+
go-version: 1.16
2323
-
2424
name: Run GoReleaser
2525
uses: goreleaser/goreleaser-action@v2

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v2
1818
with:
19-
go-version: 1.15
19+
go-version: 1.16
2020

2121
- name: Run tests
22-
run: go test -v ./...
22+
run: go test -race -v ./...

cmd/protocol.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/aaronvb/request_hole/pkg/protocol"
45
"github.com/aaronvb/request_hole/pkg/renderer"
56
"github.com/aaronvb/request_hole/pkg/server"
67
"github.com/spf13/cobra"
@@ -20,25 +21,42 @@ func init() {
2021
}
2122

2223
func http(cmd *cobra.Command, args []string) {
23-
logOutput := &renderer.Logger{
24-
File: LogFile,
25-
Port: Port,
26-
Addr: Address,
24+
renderers := make([]renderer.Renderer, 0)
25+
26+
// Collect flag data into struct to use with renderers
27+
flagData := server.FlagData{
28+
Addr: Address,
29+
BuildInfo: BuildInfo,
30+
Details: Details,
31+
LogFile: LogFile,
32+
Port: Port,
33+
ResponseCode: ResponseCode,
2734
}
28-
output := &renderer.Printer{
29-
Port: Port,
30-
Addr: Address,
31-
BuildInfo: BuildInfo,
32-
LogFile: LogFile,
33-
Details: Details}
34-
35-
httpServer := server.Http{
35+
36+
printer := &renderer.Printer{Details: Details}
37+
renderers = append(renderers, printer)
38+
39+
if LogFile != "" {
40+
logger := &renderer.Logger{
41+
FilePath: LogFile,
42+
Details: Details,
43+
Addr: Address,
44+
Port: Port,
45+
}
46+
renderers = append(renderers, logger)
47+
}
48+
49+
httpServer := &protocol.Http{
3650
Addr: Address,
3751
Port: Port,
3852
ResponseCode: ResponseCode,
39-
Output: output,
40-
LogOutput: logOutput,
41-
Details: Details}
53+
}
54+
55+
srv := server.Server{
56+
FlagData: flagData,
57+
Protocol: httpServer,
58+
Renderers: renderers,
59+
}
4260

43-
httpServer.Start()
61+
srv.Start()
4462
}

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import "github.com/spf13/cobra"
55

66
var (
77
Address string
8-
Port int
9-
ResponseCode int
108
BuildInfo map[string]string
119
Details bool
1210
LogFile string
11+
Port int
12+
ResponseCode int
1313
)
1414

1515
var rootCmd = &cobra.Command{

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/aaronvb/request_hole
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/aaronvb/logparams v1.0.0
Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
1-
package server
1+
package protocol
22

33
import (
44
"fmt"
55
"log"
66
"net/http"
7+
"sync"
78

89
"github.com/aaronvb/logparams"
910
"github.com/aaronvb/logrequest"
10-
"github.com/aaronvb/request_hole/pkg/renderer"
1111
"github.com/gorilla/mux"
1212
"github.com/pterm/pterm"
1313
)
1414

15+
// Http is the protocol for accepting http requests.
1516
type Http struct {
16-
// Port is the port the HTTP server will run on.
17-
Port int
18-
1917
// Addr is the address the HTTP server will bind to.
2018
Addr string
2119

20+
// Port is the port the HTTP server will run on.
21+
Port int
22+
2223
// ResponseCode is the response which out endpoint will return.
2324
// Default is 200 if no response code is passed.
2425
ResponseCode int
2526

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
3430
}
3531

3632
// 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) {
4139
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)
4341

4442
srv := &http.Server{
4543
Addr: addr,
4644
ErrorLog: errorLog,
4745
Handler: s.routes(),
4846
}
4947

48+
s.rendererChannels = c
49+
50+
defer wg.Done()
51+
5052
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+
}
5259
}
5360

5461
// routes handles the routes for our HTTP server and currently accepts any path.
5562
func (s *Http) routes() http.Handler {
5663
r := mux.NewRouter()
5764
r.PathPrefix("/").HandlerFunc(s.defaultHandler)
58-
5965
r.Use(s.logRequest)
6066

6167
return r
@@ -75,12 +81,24 @@ func (s *Http) logRequest(next http.Handler) http.Handler {
7581
fields := lr.ToFields()
7682
params := logparams.LogParams{Request: r, HidePrefix: true}
7783

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+
}
8089

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
8492
}
8593
})
8694
}
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

Comments
 (0)