Skip to content

Commit

Permalink
Update forwarding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sosedoff committed Dec 3, 2019
1 parent fafe6bf commit eddcfc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ machine api.github.com
Usage of github-events:
-endpoint string
Set custom server endpoint
-forward string
Send events to HTTP server running on this port
-only string
Filter events by type
-pretty
Expand Down Expand Up @@ -84,12 +82,12 @@ github-events -only=push
github-events -save -pretty
```

You can also forward event data to a local HTTP server:
You can also forward event data to a local HTTP endpoint:

```bash
# lets say you have an app running on localhost:5000
# forward requests by running this command
github-events -forward=5000
github-events http://locahost:5000/events
```

While the event proxy server is hosted on Heroku, you can run the server locally:
Expand Down
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ func startWebsocketPing(conn *websocket.Conn, done chan bool) {
}
}

func forwardMessage(port string, message Message) {
url := fmt.Sprintf("http://localhost:%v", port)
func forwardMessage(url string, message Message) {
body := bytes.NewReader(message.Payload)

req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
log.Println("Request setup error:", err)
Expand Down Expand Up @@ -159,15 +157,18 @@ func main() {
var pretty bool
var saveFiles bool
var endpoint string
var forwardPort string
var forwardURL string

flag.StringVar(&forwardPort, "forward", "", "Send events to HTTP server running on this port")
flag.StringVar(&filterType, "only", "", "Filter events by type")
flag.BoolVar(&pretty, "pretty", false, "Pretty print JSON")
flag.BoolVar(&saveFiles, "save", false, "Save each event into separate file")
flag.StringVar(&endpoint, "endpoint", "", "Set custom server endpoint")
flag.Parse()

if len(os.Args) > 1 {
forwardURL = os.Args[1]
}

if endpoint != "" {
proxyEndpoint = endpoint
reEventHook = regexp.MustCompile(proxyEndpoint + `/(.*)`)
Expand Down Expand Up @@ -287,8 +288,8 @@ func main() {
}
}

if forwardPort != "" {
go forwardMessage(forwardPort, message)
if forwardURL != "" {
go forwardMessage(forwardURL, message)
}
}
}()
Expand Down

0 comments on commit eddcfc5

Please sign in to comment.