Skip to content

Commit b212850

Browse files
authored
Merge pull request #8 from aaronvb/add-web-renderer
Add web renderer
2 parents a27c50b + d8e71fb commit b212850

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+54233
-47
lines changed

.github/workflows/tests.yml renamed to .github/workflows/test_go.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test
1+
name: Test Go
22

33
on:
44
push:
@@ -7,7 +7,6 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
1110
build:
1211
runs-on: ubuntu-latest
1312
steps:

.github/workflows/test_js.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test JS
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
defaults:
13+
run:
14+
working-directory: web
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Setup node 16
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: '16'
22+
23+
- name: Install dependencies
24+
run: yarn install
25+
26+
- name: Run tests
27+
run: yarn test

.gitignore

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,23 @@
22
dist/
33
requests.http
44
*.log
5-
request_hole
5+
request_hole
6+
7+
## web folder
8+
# dependencies
9+
web/node_modules
10+
web/.pnp
11+
web/.pnp.js
12+
13+
# testing
14+
web/coverage
15+
16+
# misc
17+
web/.env.local
18+
web/.env.development.local
19+
web/.env.test.local
20+
web/.env.production.local
21+
22+
web/npm-debug.log*
23+
web/yarn-debug.log*
24+
web/yarn-error.log*

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Request Hole CLI
22
[![go.dev Reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/aaronvb/request_hole)
3-
[![Test](https://github.com/aaronvb/request_hole/workflows/Test/badge.svg)](https://github.com/aaronvb/request_hole/actions/workflows/tests.yml)
3+
[![Go Tests](https://github.com/aaronvb/request_hole/workflows/Test%20Go/badge.svg)](https://github.com/aaronvb/request_hole/actions/workflows/test_go.yml)
4+
[![JS Tests](https://github.com/aaronvb/request_hole/workflows/Test%20JS/badge.svg)](https://github.com/aaronvb/request_hole/actions/workflows/test_js.yml)
45
[![Builds](https://github.com/aaronvb/request_hole/workflows/Builds/badge.svg)](https://github.com/aaronvb/request_hole/actions/workflows/builds.yml)
56

67
`rh` is a CLI tool for creating an ephemeral endpoint for testing and inspecting requests from your application or webhook.
78

9+
<img width="1136" alt="Request Hole CLI web ui" src="https://user-images.githubusercontent.com/100900/125158715-9b866500-e10e-11eb-9438-36d0f8325c60.png">
810
<img width="788" alt="Request Hole CLI" src="https://user-images.githubusercontent.com/100900/120265767-63048900-c23c-11eb-9a20-079ab9822767.png">
911

1012

@@ -48,10 +50,21 @@ Flags:
4850
--log string writes incoming requests to the specified log file (example: --log rh.log)
4951
-p, --port int sets the port for the endpoint (default 8080)
5052
-r, --response_code int sets the response code (default 200)
53+
--web runs a web server to show incoming requests
54+
--web_port int sets the port for the web server (default 8081)
5155
5256
Use "rh [command] --help" for more information about a command.
5357
```
58+
## Using the Web UI
59+
### Create an HTTP endpoint
60+
```
61+
$ rh http --web
62+
```
63+
This option will open a web UI that will display the incoming requests. Incoming requests will render live in the browser when they are received.
5464

65+
<img width="1136" alt="Request Hole CLI web ui" src="https://user-images.githubusercontent.com/100900/125158715-9b866500-e10e-11eb-9438-36d0f8325c60.png">
66+
67+
## Using the CLI
5568
### Creating an HTTP endpoint
5669
To create an http endpoint with default settings (port 8080, return status code 200):
5770
```
@@ -73,9 +86,41 @@ $ rh http --log rh.log
7386
```
7487
<img width="787" alt="Request Hole CLI log" src="https://user-images.githubusercontent.com/100900/120877567-fac2e980-c552-11eb-8ec0-8075bc6c0cd8.png">
7588

76-
### Exposing Request Hole to the internet
89+
## Exposing Request Hole to the internet
7790
Sometimes we need to expose `rh` to the internet to test applications or webhooks from outside of our local dev env. The best way to do this is to use a tunneling service such as [ngrok](https://ngrok.com).
7891
```
7992
$ ngrok http 3001
8093
$ rh http -p 3001
8194
```
95+
96+
## Running Tests and Building
97+
It is recommended to run the JS build first so that the Go build can embed the latest web UI build.
98+
99+
### CLI
100+
```
101+
$ go test -v ./...
102+
$ go build
103+
```
104+
105+
### Web UI
106+
```
107+
$ cd web; yarn test
108+
$ cd web; yarn build
109+
```
110+
#### For Development
111+
```
112+
$ cd web; yarn start
113+
```
114+
Visit `localhost:3000`
115+
116+
## Built With
117+
- Go https://golang.org/
118+
- logparams https://github.com/aaronvb/logparams
119+
- logrequest https://github.com/aaronvb/logrequest
120+
- cobra https://github.com/spf13/cobra
121+
- gqlgen https://github.com/99designs/gqlgen (GraphQL/Websockets)
122+
- gorilla/mux https://github.com/gorilla/mux
123+
- Apollo https://github.com/apollographql/apollo-client (GraphQL frontend)
124+
- create-react-app https://github.com/facebook/create-react-app
125+
- Tailwind https://github.com/tailwindlabs/tailwindcss
126+
- React Testing Library https://github.com/testing-library/react-testing-library

cmd/protocol.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ var httpCmd = &cobra.Command{
1313
Long: `rh: http
1414
Create an endpoint that accepts http connections.
1515
`,
16-
Run: http,
16+
Run: httpCommand,
1717
}
1818

1919
func init() {
2020
rootCmd.AddCommand(httpCmd)
2121
}
2222

23-
func http(cmd *cobra.Command, args []string) {
23+
func httpCommand(cmd *cobra.Command, args []string) {
2424
renderers := make([]renderer.Renderer, 0)
2525

2626
// Collect flag data into struct to use with renderers
@@ -31,10 +31,24 @@ func http(cmd *cobra.Command, args []string) {
3131
LogFile: LogFile,
3232
Port: Port,
3333
ResponseCode: ResponseCode,
34+
Web: Web,
35+
WebPort: WebPort,
3436
}
3537

36-
printer := &renderer.Printer{Details: Details}
37-
renderers = append(renderers, printer)
38+
if Web {
39+
web := &renderer.Web{
40+
Port: WebPort,
41+
StaticFiles: StaticFS,
42+
RequestAddr: Address,
43+
RequestPort: Port,
44+
ResponseCode: ResponseCode,
45+
BuildInfo: BuildInfo,
46+
}
47+
renderers = append(renderers, web)
48+
} else {
49+
printer := &renderer.Printer{Details: Details}
50+
renderers = append(renderers, printer)
51+
}
3852

3953
if LogFile != "" {
4054
logger := &renderer.Logger{

cmd/root.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Package cmd contains all of the CLI commands
22
package cmd
33

4-
import "github.com/spf13/cobra"
4+
import (
5+
"net/http"
6+
7+
"github.com/spf13/cobra"
8+
)
59

610
var (
711
Address string
@@ -10,6 +14,9 @@ var (
1014
LogFile string
1115
Port int
1216
ResponseCode int
17+
Web bool
18+
WebPort int
19+
StaticFS http.FileSystem
1320
)
1421

1522
var rootCmd = &cobra.Command{
@@ -19,8 +26,9 @@ var rootCmd = &cobra.Command{
1926
This CLI tool will let you create a temporary API endpoint for testing purposes.`,
2027
}
2128

22-
func Execute(buildInfo map[string]string) error {
29+
func Execute(buildInfo map[string]string, staticFS http.FileSystem) error {
2330
BuildInfo = buildInfo
31+
StaticFS = staticFS
2432
return rootCmd.Execute()
2533
}
2634

@@ -30,4 +38,8 @@ func init() {
3038
rootCmd.PersistentFlags().IntVarP(&ResponseCode, "response_code", "r", 200, "sets the response code")
3139
rootCmd.PersistentFlags().BoolVar(&Details, "details", false, "shows header details in the request")
3240
rootCmd.PersistentFlags().StringVar(&LogFile, "log", "", "writes incoming requests to the specified log file (example: --log rh.log)")
41+
42+
// 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")
3345
}

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ module github.com/aaronvb/request_hole
33
go 1.16
44

55
require (
6-
github.com/aaronvb/logparams v1.0.0
6+
github.com/99designs/gqlgen v0.13.0
7+
github.com/aaronvb/logparams v1.2.1
78
github.com/aaronvb/logrequest v1.0.0
9+
github.com/google/uuid v1.2.0
810
github.com/gorilla/mux v1.8.0
11+
github.com/gorilla/websocket v1.4.2
912
github.com/pterm/pterm v0.12.18
13+
github.com/rs/cors v1.6.0
1014
github.com/spf13/cobra v1.1.3
15+
github.com/vektah/gqlparser/v2 v2.1.0
1116
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096 // indirect
1217
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
1318
)

0 commit comments

Comments
 (0)