Skip to content

Commit 6c4ad67

Browse files
authored
Merge pull request #10 from aaronvb/bind-webui-address
Add a CLI flag to allow binding of web UI address
2 parents 3c83d35 + 159f932 commit 6c4ad67

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ Available Commands:
4343
version Print version number of Request Hole
4444
4545
Flags:
46-
-a, --address string sets the address for the endpoint (default "localhost")
47-
--details shows header details in the request
48-
-h, --help help for rh
49-
--log string writes incoming requests to the specified log file (example: --log rh.log)
50-
-p, --port int sets the port for the endpoint (default 8080)
51-
-r, --response_code int sets the response code (default 200)
52-
--web runs a web server to show incoming requests
53-
--web_port int sets the port for the web server (default 8081)
46+
-a, --address string sets the address for the endpoint (default "localhost")
47+
--details shows header details in the request
48+
-h, --help help for rh
49+
--log string writes incoming requests to the specified log file (example: --log rh.log)
50+
-p, --port int sets the port for the endpoint (default 8080)
51+
-r, --response_code int sets the response code (default 200)
52+
--web runs the web UI to show incoming requests
53+
--web_address string sets the address for the web UI (default "localhost")
54+
--web_port int sets the port for the web UI (default 8081)
5455
5556
Use "rh [command] --help" for more information about a command.
5657
```

cmd/protocol.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ func httpCommand(cmd *cobra.Command, args []string) {
3232
Port: Port,
3333
ResponseCode: ResponseCode,
3434
Web: Web,
35+
WebAddress: WebAddress,
3536
WebPort: WebPort,
3637
}
3738

3839
if Web {
3940
web := &renderer.Web{
41+
Address: WebAddress,
4042
Port: WebPort,
4143
StaticFiles: StaticFS,
4244
RequestAddr: Address,

cmd/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var (
1515
Port int
1616
ResponseCode int
1717
Web bool
18+
WebAddress string
1819
WebPort int
1920
StaticFS http.FileSystem
2021
)
@@ -40,6 +41,7 @@ func init() {
4041
rootCmd.PersistentFlags().StringVar(&LogFile, "log", "", "writes incoming requests to the specified log file (example: --log rh.log)")
4142

4243
// Web server renderer
43-
rootCmd.PersistentFlags().BoolVar(&Web, "web", false, "runs a web server to show incoming requests")
44-
rootCmd.PersistentFlags().IntVar(&WebPort, "web_port", 8081, "sets the port for the web server")
44+
rootCmd.PersistentFlags().BoolVar(&Web, "web", false, "runs the web UI to show incoming requests")
45+
rootCmd.PersistentFlags().StringVar(&WebAddress, "web_address", "localhost", "sets the address for the web UI")
46+
rootCmd.PersistentFlags().IntVar(&WebPort, "web_port", 8081, "sets the port for the web UI")
4547
}

pkg/renderer/web.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,31 @@ import (
2424
"github.com/rs/cors"
2525
)
2626

27+
// Web is the renderer for the web UI.
2728
type Web struct {
28-
BuildInfo map[string]string
29-
Port int
30-
ResponseCode int
31-
RequestAddr string
32-
RequestPort int
33-
StaticFiles http.FileSystem
34-
mu sync.Mutex
35-
requests []*protocol.RequestPayload
29+
// Address is the address the web UI server will bind to.
30+
Address string
31+
32+
// BuildInfo contains build information.
33+
BuildInfo map[string]string
34+
35+
// Port is the port the web UI server will run on.
36+
Port int
37+
38+
// These are used for showing information about the request endpoint in the
39+
// web UI.
40+
ResponseCode int
41+
RequestAddr string
42+
RequestPort int
43+
44+
StaticFiles http.FileSystem
45+
46+
mu sync.Mutex
47+
48+
// requests contain the incoming requests.
49+
requests []*protocol.RequestPayload
50+
51+
// subscriptions contain the websocket connections to our graphql subscribers.
3652
subscriptions map[string]chan *protocol.RequestPayload
3753
}
3854

@@ -42,7 +58,7 @@ func (web *Web) Start(wg *sync.WaitGroup, rp chan protocol.RequestPayload, q cha
4258
web.requests = make([]*protocol.RequestPayload, 0)
4359
web.subscriptions = make(map[string]chan *protocol.RequestPayload)
4460

45-
addr := fmt.Sprintf("localhost:%d", web.Port)
61+
addr := fmt.Sprintf("%s:%d", web.Address, web.Port)
4662
errorLog := log.New(&httpErrorLog{}, "", 0)
4763

4864
srv := &http.Server{

pkg/server/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type FlagData struct {
4848
// Web determines if we use the web renderer, otherwise defaults to the printer renderer.
4949
Web bool
5050

51+
// WebAddress is the address the web UI will bind to.
52+
WebAddress string
53+
5154
// WebPort defines which port we host the web renderer at, defaults to 8081.
5255
WebPort int
5356
}
@@ -119,7 +122,7 @@ func (s *Server) startText() string {
119122
text := fmt.Sprintf("%s %s\nListening on http://%s:%d", primary, version, s.FlagData.Addr, s.FlagData.Port)
120123

121124
if s.FlagData.Web {
122-
text = fmt.Sprintf("%s\nWeb running on: http://localhost:%d", text, s.FlagData.WebPort)
125+
text = fmt.Sprintf("%s\nWeb running on: http://%s:%d", text, s.FlagData.WebAddress, s.FlagData.WebPort)
123126
}
124127

125128
if s.FlagData.Details {

pkg/server/server_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,43 @@ func TestStartTextWithLogFile(t *testing.T) {
5555
t.Errorf("Expected %s, got %s", expected, result)
5656
}
5757
}
58+
59+
func TestStartTextWithWebUIDefault(t *testing.T) {
60+
pterm.DisableColor()
61+
flags := FlagData{
62+
Addr: "localhost",
63+
Port: 8080,
64+
BuildInfo: map[string]string{"version": "dev"},
65+
Web: true,
66+
}
67+
server := Server{FlagData: flags}
68+
result := server.startText()
69+
expected := fmt.Sprintf(
70+
"Request Hole %s\nListening on http://%s:%d\nWeb running on: http://%s:%d", "dev",
71+
server.FlagData.Addr, server.FlagData.Port, server.FlagData.WebAddress, server.FlagData.WebPort)
72+
73+
if result != expected {
74+
t.Errorf("Expected %s, got %s", expected, result)
75+
}
76+
}
77+
78+
func TestStartTextWithWebUICustomFlags(t *testing.T) {
79+
pterm.DisableColor()
80+
flags := FlagData{
81+
Addr: "localhost",
82+
Port: 8080,
83+
BuildInfo: map[string]string{"version": "dev"},
84+
Web: true,
85+
WebAddress: "0.0.0.0",
86+
WebPort: 8082,
87+
}
88+
server := Server{FlagData: flags}
89+
result := server.startText()
90+
expected := fmt.Sprintf(
91+
"Request Hole %s\nListening on http://%s:%d\nWeb running on: http://%s:%d", "dev",
92+
server.FlagData.Addr, server.FlagData.Port, server.FlagData.WebAddress, server.FlagData.WebPort)
93+
94+
if result != expected {
95+
t.Errorf("Expected %s, got %s", expected, result)
96+
}
97+
}

0 commit comments

Comments
 (0)