From dcf2934e1b84350108d26f0ebb2bdfa8e3ec248e Mon Sep 17 00:00:00 2001 From: zhouyuyan Date: Thu, 25 Jan 2018 13:11:45 +0800 Subject: [PATCH] add flag for hmq --- README.md | 45 +++++++++++++++++++++++++++++-- broker/config.go | 70 +++++++++++++++++++++++++++++++++++++++++------- main.go | 4 +-- 3 files changed, 105 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 170870d..e17afb1 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,52 @@ Download: [click here](https://github.com/fhmq/hmq/releases) ## RUNNING ```bash -$ git clone https://github.com/fhmq/hmq.git -$ cd hmq +$ go get https://github.com/fhmq/hmq.git +$ cd $GOPATH/github.com/fhmq/hmq $ go run main.go ``` +## Usage of hmq: +~~~ +Usage of ./hmq: + -w int + worker num to process message, perfer (client num)/10. (default 1024) + -worker int + worker num to process message, perfer (client num)/10. (default 1024) + -h string + Network host to listen on. (default "0.0.0.0") + -host string + Network host to listen on. (default "0.0.0.0") + -p string + Port to listen on. (default "1883") + -port string + Port to listen on. (default "1883") + -c string + config file for hmq + -config string + config file for hmq + -cluster string + Cluster ip from which members can connect. + -cluster_listen string + Cluster ip from which members can connect. + -cluster_port string + Cluster port from which members can connect. + -cp string + Cluster port from which members can connect. + -r string + Router who maintenance cluster info + -router string + Router who maintenance cluster info + -ws_path string + path for ws to listen on + -ws_port string + port for ws to listen on + -wspath string + path for ws to listen on + -wsport string + port for ws to listen on +~~~ + ### hmq.config ~~~ { diff --git a/broker/config.go b/broker/config.go index e73f61c..c15fa76 100644 --- a/broker/config.go +++ b/broker/config.go @@ -5,16 +5,13 @@ import ( "crypto/x509" "encoding/json" "errors" + "flag" "fmt" "io/ioutil" log "github.com/cihub/seelog" ) -const ( - CONFIGFILE = "conf/hmq.config" -) - type Config struct { Worker int `json:"workerNum"` Host string `json:"host"` @@ -43,9 +40,58 @@ type TLSInfo struct { KeyFile string `json:"keyFile"` } -func LoadConfig() (*Config, error) { +var DefaultConfig *Config = &Config{ + Worker: 4096, + Host: "0.0.0.0", + Port: "1883", + Acl: false, +} - content, err := ioutil.ReadFile(CONFIGFILE) +func ConfigureConfig() (*Config, error) { + config := &Config{} + var ( + configFile string + ) + flag.IntVar(&config.Worker, "w", 1024, "worker num to process message, perfer (client num)/10.") + flag.IntVar(&config.Worker, "worker", 1024, "worker num to process message, perfer (client num)/10.") + flag.StringVar(&config.Port, "port", "1883", "Port to listen on.") + flag.StringVar(&config.Port, "p", "1883", "Port to listen on.") + flag.StringVar(&config.Host, "host", "0.0.0.0", "Network host to listen on.") + flag.StringVar(&config.Host, "h", "0.0.0.0", "Network host to listen on.") + flag.StringVar(&config.Cluster.Host, "cluster", "", "Cluster ip from which members can connect.") + flag.StringVar(&config.Cluster.Host, "cluster_listen", "", "Cluster ip from which members can connect.") + flag.StringVar(&config.Cluster.Port, "cp", "", "Cluster port from which members can connect.") + flag.StringVar(&config.Cluster.Port, "cluster_port", "", "Cluster port from which members can connect.") + flag.StringVar(&config.Router, "r", "", "Router who maintenance cluster info") + flag.StringVar(&config.Router, "router", "", "Router who maintenance cluster info") + flag.StringVar(&config.WsPort, "wsport", "", "port for ws to listen on") + flag.StringVar(&config.WsPort, "ws_port", "", "port for ws to listen on") + flag.StringVar(&config.WsPath, "wspath", "", "path for ws to listen on") + flag.StringVar(&config.WsPath, "ws_path", "", "path for ws to listen on") + flag.StringVar(&configFile, "config", "", "config file for hmq") + flag.StringVar(&configFile, "c", "", "config file for hmq") + flag.Parse() + + if configFile != "" { + tmpConfig, e := LoadConfig(configFile) + if e != nil { + return nil, e + } else { + config = tmpConfig + } + } + + if err := config.check(); err != nil { + return nil, err + } + + return config, nil + +} + +func LoadConfig(filename string) (*Config, error) { + + content, err := ioutil.ReadFile(filename) if err != nil { log.Error("Read config file error: ", err) return nil, err @@ -59,6 +105,11 @@ func LoadConfig() (*Config, error) { return nil, err } + return &config, nil +} + +func (config *Config) check() error { + if config.Worker == 0 { config.Worker = 1024 } @@ -78,21 +129,20 @@ func LoadConfig() (*Config, error) { } if config.Router != "" { if config.Cluster.Port == "" { - return nil, errors.New("cluster port is null") + return errors.New("cluster port is null") } } if config.TlsPort != "" { if config.TlsInfo.CertFile == "" || config.TlsInfo.KeyFile == "" { log.Error("tls config error, no cert or key file.") - return nil, err + return errors.New("tls config error, no cert or key file.") } if config.TlsHost == "" { config.TlsHost = "0.0.0.0" } } - - return &config, nil + return nil } func NewTLSConfig(tlsInfo TLSInfo) (*tls.Config, error) { diff --git a/main.go b/main.go index 456d23f..a79d0d1 100644 --- a/main.go +++ b/main.go @@ -30,9 +30,9 @@ func init() { func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - config, er := broker.LoadConfig() + config, er := broker.ConfigureConfig() if er != nil { - log.Error("Load Config file error: ", er) + log.Error("configure broker config error: ", er) return }