mirror of
https://github.com/fhmq/hmq.git
synced 2026-04-26 19:48:34 +00:00
add flag for hmq
This commit is contained in:
45
README.md
45
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
|
||||
~~~
|
||||
{
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user