Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

traceapp: take a base URL parameter in New #162

Merged
merged 7 commits into from
May 10, 2016
Merged
Prev Previous commit
cmd/appdash: choose better appdash serve --url default value (based…
… on --http addr).

This causes `appdash serve` to always choose a better default for `--url` when not
specified. Prior to this it would choose a hard-coded default which matched the `--http`
default, but that default would not work when changing `--http`.
emidoots committed May 10, 2016
commit cbf71cd120822c52b37b814f36280b6ae532e14c
27 changes: 25 additions & 2 deletions cmd/appdash/serve_cmd.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ func init() {
// ServeCmd is the command for running Appdash in server mode, where a
// collector server and the web UI are hosted.
type ServeCmd struct {
URL string `long:"url" description:"URL which Appdash is being hosted at" default:"http://localhost:7700"`
URL string `long:"url" description:"URL which Appdash is being hosted at (e.g. http://localhost:7700)"`
CollectorAddr string `long:"collector" description:"collector listen address" default:":7701"`
HTTPAddr string `long:"http" description:"HTTP listen address" default:":7700"`
SampleData bool `long:"sample-data" description:"add sample data"`
@@ -96,7 +96,7 @@ func (c *ServeCmd) Execute(args []string) error {
}
}

url, err := url.Parse(c.URL)
url, err := c.urlOrDefault()
if err != nil {
log.Fatal(err)
}
@@ -173,6 +173,29 @@ func (c *ServeCmd) Execute(args []string) error {
return http.ListenAndServe(c.HTTPAddr, h)
}

// urlOrDefault returns c.URL if non-empty, otherwise it returns c.HTTPAddr
// with localhost" as the default host (if not specified in c.HTTPAddr).
func (c *ServeCmd) urlOrDefault() (*url.URL, error) {
// Parse c.URL and return it if non-empty.
u, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
if c.URL != "" {
return u, nil
}

// Parse c.HTTPAddr and use a default host if not specified.
addr, err := url.Parse("http://" + c.HTTPAddr)
if err != nil {
return nil, err
}
if addr.Host == "" {
addr.Host = "localhost"
}
return addr, nil
}

func newBasicAuthHandler(user, passwd string, h http.Handler) http.Handler {
want := "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, passwd)))
return &basicAuthHandler{h, []byte(want)}