diff --git a/gnatsd.go b/gnatsd.go index 9c45d2fd..b9c6ff80 100644 --- a/gnatsd.go +++ b/gnatsd.go @@ -4,6 +4,9 @@ package main import ( "flag" + "fmt" + "net" + "net/url" "os" "strings" @@ -53,6 +56,7 @@ func main() { flag.BoolVar(&showVersion, "v", false, "Print version information.") flag.IntVar(&opts.ProfPort, "profile", 0, "Profiling HTTP port") flag.StringVar(&opts.RoutesStr, "routes", "", "Routes to actively solicit a connection.") + flag.StringVar(&opts.ClusterListenStr, "cluster_listen", "", "Cluster url from which members can solicit routes.") flag.BoolVar(&showTlsHelp, "help_tls", false, "TLS help.") flag.BoolVar(&opts.TLS, "tls", false, "Enable TLS.") @@ -113,6 +117,12 @@ func main() { // Configure TLS based on any present flags configureTLS(&opts) + // Configure cluster opts if explicitly set via flags. + err = configureClusterOpts(&opts) + if err != nil { + server.PrintAndDie(err.Error()) + } + // Create the server with appropriate options. s := server.New(&opts) @@ -189,3 +199,33 @@ func configureTLS(opts *server.Options) { server.PrintAndDie(err.Error()) } } + +func configureClusterOpts(opts *server.Options) error { + if opts.ClusterListenStr == "" { + return nil + } + + clusterUrl, err := url.Parse(opts.ClusterListenStr) + h, p, err := net.SplitHostPort(clusterUrl.Host) + if err != nil { + return err + } + opts.ClusterHost = h + _, err = fmt.Sscan(p, &opts.ClusterPort) + if err != nil { + return err + } + + if clusterUrl.User != nil { + pass, hasPassword := clusterUrl.User.Password() + if !hasPassword { + return fmt.Errorf("Expected cluster password to be set.") + } + opts.ClusterPassword = pass + + user := clusterUrl.User.Username() + opts.ClusterUsername = user + } + + return nil +} diff --git a/server/opts.go b/server/opts.go index 2d6368ef..caa0774e 100644 --- a/server/opts.go +++ b/server/opts.go @@ -9,12 +9,12 @@ import ( "io/ioutil" "net" "net/url" + "os" "strconv" "strings" "time" "github.com/nats-io/gnatsd/conf" - "os" ) // Options block for gnatsd server. @@ -44,6 +44,7 @@ type Options struct { ClusterAuthTimeout float64 `json:"auth_timeout"` ClusterTLSTimeout float64 `json:"-"` ClusterTLSConfig *tls.Config `json:"-"` + ClusterListenStr string `json:"-"` ProfPort int `json:"-"` PidFile string `json:"-"` LogFile string `json:"-"`