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 |
|-----------------|-------------|
| `-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 |

24
args.go
View file

@ -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
}

12
main.go
View file

@ -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 {