From 621753ceb21b903ff889a163e90576f9044c04e3 Mon Sep 17 00:00:00 2001 From: Fabricio Date: Sat, 18 Nov 2017 09:05:41 -0200 Subject: [PATCH] simplifies flag parsing call --- README.md | 4 ++-- args.go | 24 +++++++----------------- main.go | 12 +++++------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index f4381ad..971f961 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ For ready-to-use executables (no need to build it yourself) for *Windows* and *L | param | description | |-----------------|-------------| | `-url` | **Required.** Set the base url you want to capture | -| `-port` | Set the port you want to capture. Default: *9000* | +| `-port` | Set the proxy port. Default: *9000* | | `-dashboard` | Set the dashboard name. Default: *dashboard* | -| `-max-captures` | Set the max number of captures. Default: *16* | +| `-max-captures` | Set the max number of captures to show in the dashboard. Default: *16* | | `-h` | Show help | diff --git a/args.go b/args.go index 85c74e8..503631d 100644 --- a/args.go +++ b/args.go @@ -2,26 +2,16 @@ package main import ( "flag" + "net/url" "strconv" ) -type Args struct { - url string - port string - dashboard string - maxCaptures int -} - -func parseArgs() Args { - proxyURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture") - proxyPort := flag.Int("port", 9000, "Set the port you want to capture") - maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures") +func parseFlags() (*url.URL, string, string, int) { + target := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture") + proxyPort := flag.Int("port", 9000, "Set the proxy port") dashboard := flag.String("dashboard", "dashboard", "Set the dashboard name") + maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard") flag.Parse() - return Args{ - url: *proxyURL, - port: strconv.Itoa(*proxyPort), - dashboard: *dashboard, - maxCaptures: *maxCaptures, - } + targetURL, _ := url.Parse(*target) + return targetURL, strconv.Itoa(*proxyPort), *dashboard, *maxCaptures } diff --git a/main.go b/main.go index 74bae58..64394d1 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "net/http" "net/http/httputil" - "net/url" ) type Capture map[string]interface{} @@ -20,17 +19,16 @@ type Transport struct { } func main() { - args := parseArgs() - maxCaptures = args.maxCaptures + targetURL, proxyPort, dashboard, maxCaptrs := parseFlags() + maxCaptures = maxCaptrs - URL, _ := url.Parse(args.url) - proxy := httputil.NewSingleHostReverseProxy(URL) + proxy := httputil.NewSingleHostReverseProxy(targetURL) proxy.Transport = Transport{http.DefaultTransport} http.Handle("/", getProxyHandler(proxy)) http.Handle("/socket.io/", getSocketHandler()) - http.Handle("/"+args.dashboard+"/", getDashboardHandler()) - http.ListenAndServe(":"+args.port, nil) + http.Handle("/"+dashboard+"/", getDashboardHandler()) + http.ListenAndServe(":"+proxyPort, nil) } func getProxyHandler(handler http.Handler) http.Handler {