diff --git a/cmd/api/main.go b/cmd/api/main.go index 2073e1ce2..609f372ff 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -4,30 +4,55 @@ import ( "OpenIM/internal/api" "OpenIM/pkg/common/config" "OpenIM/pkg/common/log" - "flag" "fmt" + "github.com/spf13/cobra" + "os" "strconv" "OpenIM/pkg/common/constant" ) -func main() { - if err := config.InitConfig(); err != nil { - panic(err.Error()) +var startCmd = &cobra.Command{ + Use: "start", + Short: "Start the server", + Run: func(cmd *cobra.Command, args []string) { + port, _ := cmd.Flags().GetInt(constant.FlagPort) + configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf) + fmt.Printf("Starting server on port %d with config file at %s\n", port, configFolderPath) + if err := run(configFolderPath, port); err != nil { + panic(err.Error()) + } + }, +} + +func init() { + startCmd.Flags().IntP(constant.FlagPort, "p", 10002, "Port to listen on") + startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") +} + +func run(configFolderPath string, port int) error { + if err := config.InitConfig(configFolderPath); err != nil { + return err } log.NewPrivateLog(constant.LogFileName) router := api.NewGinRouter() - ginPort := flag.Int("port", config.Config.Api.GinPort[0], "get ginServerPort from cmd,default 10002 as port") - flag.Parse() - address := "0.0.0.0:" + strconv.Itoa(*ginPort) + address := constant.LocalHost + ":" + strconv.Itoa(port) if config.Config.Api.ListenIP != "" { - address = config.Config.Api.ListenIP + ":" + strconv.Itoa(*ginPort) + address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port) } fmt.Println("start api server, address: ", address, ", OpenIM version: ", constant.CurrentVersion) err := router.Run(address) if err != nil { log.Error("", "api run failed ", address, err.Error()) - panic("api start failed " + err.Error()) + return err + } + return nil +} + +func main() { + if err := startCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/go.mod b/go.mod index e8a307593..6137d1575 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( github.com/jonboulle/clockwork v0.3.0 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/mattn/go-isatty v0.0.17 // indirect + github.com/spf13/cobra v1.6.1 github.com/ugorji/go/codec v1.2.8 // indirect golang.org/x/crypto v0.5.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect diff --git a/go.sum b/go.sum index a8dd15579..46f64632b 100644 --- a/go.sum +++ b/go.sum @@ -432,6 +432,7 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -624,6 +625,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo= @@ -789,6 +792,7 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= @@ -802,6 +806,10 @@ github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= diff --git a/internal/common/network/ip.go b/internal/common/network/ip.go index 5c8ed4f59..abc7dbe38 100644 --- a/internal/common/network/ip.go +++ b/internal/common/network/ip.go @@ -1,6 +1,9 @@ package network -import utils "github.com/OpenIMSDK/open_utils" +import ( + "OpenIM/pkg/common/constant" + utils "github.com/OpenIMSDK/open_utils" +) func GetRpcRegisterIP(configIP string) (string, error) { registerIP := configIP @@ -16,7 +19,7 @@ func GetRpcRegisterIP(configIP string) (string, error) { func GetListenIP(configIP string) string { if configIP == "" { - return "0.0.0.0" + return constant.LocalHost } else { return configIP } diff --git a/internal/msggateway/relay_rpc_server.go b/internal/msggateway/relay_rpc_server.go index 5f5a04366..0c211521f 100644 --- a/internal/msggateway/relay_rpc_server.go +++ b/internal/msggateway/relay_rpc_server.go @@ -52,7 +52,7 @@ func (r *RPCServer) onInit(rpcPort int) { func (r *RPCServer) run() { listenIP := "" if config.Config.ListenIP == "" { - listenIP = "0.0.0.0" + listenIP = constant.LocalHost } else { listenIP = config.Config.ListenIP } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index f2382ba23..a5571f6fc 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -2,7 +2,7 @@ package config import ( "OpenIM/pkg/discoveryregistry" - "flag" + "fmt" "io/ioutil" "os" "path/filepath" @@ -21,7 +21,7 @@ const ( FileName = "config.yaml" NotificationFileName = "notification.yaml" ENV = "CONFIG_NAME" - DefaultPath = "../config/" + DefaultFolderPath = "../config/" ConfKey = "conf" ) @@ -497,15 +497,18 @@ func (c *config) unmarshalConfig(config interface{}, configPath string) error { return nil } -func (c *config) initConfig(config interface{}, configName, configPath string) error { - if configPath == "" { - if configPath == "" { - configPath = DefaultPath - } +func (c *config) initConfig(config interface{}, configName, configFolderPath string) error { + if configFolderPath == "" { + configFolderPath = DefaultFolderPath } + configPath := filepath.Join(configFolderPath, configName) _, err := os.Stat(configPath) - if os.IsNotExist(err) { + if err != nil { + if !os.IsNotExist(err) { + return err + } configPath = filepath.Join(Root, "config", configName) + fmt.Println(configPath, "not exist, use", configPath) } Root = filepath.Dir(configPath) return c.unmarshalConfig(config, configPath) @@ -523,14 +526,12 @@ func (c *config) GetConfFromRegistry(registry discoveryregistry.SvcDiscoveryRegi return registry.GetConfFromRegistry(ConfKey) } -func InitConfig() error { - configPath := flag.String("config_path", os.Getenv(ENV), "folder for config") - flag.Parse() - err := Config.initConfig(&Config, FileName, *configPath) +func InitConfig(configFolderPath string) error { + err := Config.initConfig(&Config, FileName, configFolderPath) if err != nil { return err } - err = Config.initConfig(&Config.Notification, NotificationFileName, *configPath) + err = Config.initConfig(&Config.Notification, NotificationFileName, configFolderPath) if err != nil { return err } diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 0db87f764..fd20aefcd 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -325,3 +325,11 @@ const BigVersion = "v2" const LogFileName = "OpenIM.log" const CurrentVersion = "v2.3.4-rc0" + +const LocalHost = "0.0.0.0" + +// flag parse +const ( + FlagPort = "port" + FlagConf = "config_folder_path" +) diff --git a/script/path_info.cfg b/script/path_info.cfg index 24443caa2..5b999cbe4 100644 --- a/script/path_info.cfg +++ b/script/path_info.cfg @@ -19,7 +19,7 @@ msg_transfer_source_root="../cmd/msgtransfer/" msg_transfer_service_num=4 -sdk_server_name="sdkws_server" +sdk_server_name="sdk_ws_server" sdk_server_binary_root="../bin/" sdk_server_source_root="../cmd/Open-IM-SDK-Core/"