diff --git a/cmd/root.go b/cmd/root.go index 7448cb256..a4b31a4eb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,7 +54,7 @@ Build Time: ` + BuildTime + "\n", }, } rc.PersistentFlags().Bool("dry-run", false, "stop before executing") - rc.PersistentFlags().MarkHidden("dry-run") + _ = rc.PersistentFlags().MarkHidden("dry-run") rc.PersistentFlags().StringP("config", "c", "", "Configuration file to read from.") for _, subcomFn := range subcommandFns { rc.AddCommand(subcomFn(stdin, stdout, stderr)) diff --git a/cmd/server.go b/cmd/server.go index 1eb5dbb59..55b695cf3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -50,7 +50,7 @@ on the configured port.`, // Execute the program. if err := Server.Run(); err != nil { - return err + return fmt.Errorf("error running server: %v", err) } // First SIGKILL causes server to shut down gracefully. diff --git a/config.go b/config.go index 182ea03c3..e664c0771 100644 --- a/config.go +++ b/config.go @@ -4,7 +4,8 @@ import "time" const ( // DefaultHost is the default hostname and port to use. - DefaultHost = "localhost:15000" + DefaultHost = "localhost" + DefaultPort = "10101" ) // Config represents the configuration for the command. @@ -30,7 +31,7 @@ type Config struct { // NewConfig returns an instance of Config with default options. func NewConfig() *Config { c := &Config{ - Host: DefaultHost, + Host: DefaultHost + ":" + DefaultPort, } c.Cluster.ReplicaN = DefaultReplicaN c.Cluster.PollingInterval = Duration(DefaultPollingInterval) diff --git a/server.go b/server.go index 57ec0e2b5..55702acf9 100644 --- a/server.go +++ b/server.go @@ -1,7 +1,6 @@ package pilosa import ( - "errors" "fmt" "io" "io/ioutil" @@ -74,7 +73,7 @@ func (s *Server) Open() error { if err != nil { return err } else if port == "" { - return errors.New("port must be specified in config host") + port = DefaultPort } // Open HTTP listener to determine port (if specified as :0). diff --git a/server/server.go b/server/server.go index ec70e6479..37ee1f7c3 100644 --- a/server/server.go +++ b/server/server.go @@ -60,7 +60,7 @@ func NewCommand() *Command { } // Run executes the pilosa server. -func (m *Command) Run(args ...string) error { +func (m *Command) Run(args ...string) (err error) { defer close(m.Started) prefix := "~" + string(filepath.Separator) if strings.HasPrefix(m.Config.DataDir, prefix) { @@ -80,20 +80,36 @@ func (m *Command) Run(args ...string) error { m.Server.Index.Stats = pilosa.NewExpvarStatsClient() // Build cluster from config file. - m.Server.Host = m.Config.Host + m.Server.Host, err = normalizeHost(m.Config.Host) + if err != nil { + return err + } m.Server.Cluster = m.Config.PilosaCluster() // Set configuration options. m.Server.AntiEntropyInterval = time.Duration(m.Config.AntiEntropy.Interval) // Initialize server. - if err := m.Server.Open(); err != nil { - return err + if err = m.Server.Open(); err != nil { + return fmt.Errorf("server.Open: %v", err) } fmt.Fprintf(m.Stderr, "Listening as http://%s\n", m.Server.Host) return nil } +func normalizeHost(host string) (string, error) { + if !strings.Contains(host, ":") { + host = host + ":" + } else if strings.Contains(host, "://") { + if strings.HasPrefix(host, "http://") { + host = host[7:] + } else { + return "", fmt.Errorf("invalid scheme or host: '%s'. use the format [http://]:", host) + } + } + return host, nil +} + // Close shuts down the server. func (m *Command) Close() error { err := m.Server.Close()