simplifies flag parsing call

This commit is contained in:
Fabricio 2017-11-18 09:05:41 -02:00
parent bfa5e78a26
commit 621753ceb2
3 changed files with 14 additions and 26 deletions

View file

@ -18,9 +18,9 @@ For ready-to-use executables (no need to build it yourself) for *Windows* and *L
| param | description | | param | description |
|-----------------|-------------| |-----------------|-------------|
| `-url` | **Required.** Set the base url you want to capture | | `-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* | | `-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 | | `-h` | Show help |

24
args.go
View file

@ -2,26 +2,16 @@ package main
import ( import (
"flag" "flag"
"net/url"
"strconv" "strconv"
) )
type Args struct { func parseFlags() (*url.URL, string, string, int) {
url string target := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture")
port string proxyPort := flag.Int("port", 9000, "Set the proxy port")
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")
dashboard := flag.String("dashboard", "dashboard", "Set the dashboard name") 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() flag.Parse()
return Args{ targetURL, _ := url.Parse(*target)
url: *proxyURL, return targetURL, strconv.Itoa(*proxyPort), *dashboard, *maxCaptures
port: strconv.Itoa(*proxyPort),
dashboard: *dashboard,
maxCaptures: *maxCaptures,
}
} }

12
main.go
View file

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url"
) )
type Capture map[string]interface{} type Capture map[string]interface{}
@ -20,17 +19,16 @@ type Transport struct {
} }
func main() { func main() {
args := parseArgs() targetURL, proxyPort, dashboard, maxCaptrs := parseFlags()
maxCaptures = args.maxCaptures maxCaptures = maxCaptrs
URL, _ := url.Parse(args.url) proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy := httputil.NewSingleHostReverseProxy(URL)
proxy.Transport = Transport{http.DefaultTransport} proxy.Transport = Transport{http.DefaultTransport}
http.Handle("/", getProxyHandler(proxy)) http.Handle("/", getProxyHandler(proxy))
http.Handle("/socket.io/", getSocketHandler()) http.Handle("/socket.io/", getSocketHandler())
http.Handle("/"+args.dashboard+"/", getDashboardHandler()) http.Handle("/"+dashboard+"/", getDashboardHandler())
http.ListenAndServe(":"+args.port, nil) http.ListenAndServe(":"+proxyPort, nil)
} }
func getProxyHandler(handler http.Handler) http.Handler { func getProxyHandler(handler http.Handler) http.Handler {