mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-11 12:36:00 +08:00
optimization: change the configuration file from being read globally … (#1935)
* optimization: change the configuration file from being read globally to being read independently. * optimization: change the configuration file from being read globally to being read independently. * optimization: change the configuration file from being read globally to being read independently. * optimization: config file changed to dependency injection. * fix: replace global config with dependency injection * fix: replace global config with dependency injection * fix: import the enough param * fix: import the enough param * fix: import the enough param * fix: fix the component check of path * fix: fix the kafka of tls is nil problem * fix: fix the TLS.CACrt is nil error * fix: fix the valiable shadows problem * fix: fix the comflect * optimization: message remove options. * fix: fix the param pass error * fix: find error * fix: find error * fix: find eror * fix: find error * fix: find error * fix: del the undifined func * fix: find error * fix: fix the error * fix: pass config * fix: find error * fix: find error * fix: find error * fix: find error * fix: find error * fix: fix the config * fix: fix the error * fix: fix the config pass error * fix: fix the eror * fix: fix the error * fix: fix the error * fix: fix the error * fix: find error * fix: fix the error * fix: fix the config * fix: add return err * fix: fix the err2 * fix: err * fix: fix the func * fix: del the chinese comment * fix: fix the func * fix: fix the gateway_test logic * fix: s3 * test * test * fix: not found --------- Co-authored-by: luhaoling <2198702716@qq.com> Co-authored-by: withchao <993506633@qq.com>
This commit is contained in:
+117
-73
@@ -16,100 +16,144 @@ package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/startrpc"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
|
||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/startrpc"
|
||||
)
|
||||
|
||||
type rpcInitFuc func(config *config2.GlobalConfig, disCov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error
|
||||
|
||||
type RpcCmd struct {
|
||||
*RootCmd
|
||||
RpcRegisterName string
|
||||
initFunc rpcInitFuc
|
||||
}
|
||||
|
||||
func NewRpcCmd(name string) *RpcCmd {
|
||||
ret := &RpcCmd{NewRootCmd(name)}
|
||||
func NewRpcCmd(name string, initFunc rpcInitFuc) *RpcCmd {
|
||||
ret := &RpcCmd{RootCmd: NewRootCmd(name), initFunc: initFunc}
|
||||
ret.addPreRun()
|
||||
ret.addRunE()
|
||||
ret.SetRootCmdPt(ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (a *RpcCmd) Exec() error {
|
||||
a.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
portFlag, err := a.getPortFlag(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.port = portFlag
|
||||
|
||||
prometheusPort, err := a.getPrometheusPortFlag(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.prometheusPort = prometheusPort
|
||||
|
||||
return nil
|
||||
func (a *RpcCmd) addPreRun() {
|
||||
a.Command.PreRun = func(cmd *cobra.Command, args []string) {
|
||||
a.port = a.getPortFlag(cmd)
|
||||
a.prometheusPort = a.getPrometheusPortFlag(cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *RpcCmd) addRunE() {
|
||||
a.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
rpcRegisterName, err := a.GetRpcRegisterNameFromConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
return a.StartSvr(rpcRegisterName, a.initFunc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *RpcCmd) Exec() error {
|
||||
return a.Execute()
|
||||
}
|
||||
|
||||
func (a *RpcCmd) StartSvr(name string, rpcFn func(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
||||
portFlag, err := a.GetPortFlag()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
a.port = portFlag
|
||||
func (a *RpcCmd) StartSvr(name string, rpcFn func(config *config2.GlobalConfig, disCov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
||||
if a.GetPortFlag() == 0 {
|
||||
return errs.Wrap(errors.New("port is required"))
|
||||
}
|
||||
|
||||
return startrpc.Start(portFlag, name, a.GetPrometheusPortFlag(), rpcFn)
|
||||
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), a.config, rpcFn)
|
||||
}
|
||||
|
||||
func (a *RpcCmd) GetPortFromConfig(portType string) (int, error) {
|
||||
portConfigMap := map[string]map[string]int{
|
||||
RpcPushServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImPushPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.PushPrometheusPort[0],
|
||||
},
|
||||
RpcAuthServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImAuthPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.AuthPrometheusPort[0],
|
||||
},
|
||||
RpcConversationServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImConversationPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.ConversationPrometheusPort[0],
|
||||
},
|
||||
RpcFriendServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImFriendPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.FriendPrometheusPort[0],
|
||||
},
|
||||
RpcGroupServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImGroupPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.GroupPrometheusPort[0],
|
||||
},
|
||||
RpcMsgServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImMessagePort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.MessagePrometheusPort[0],
|
||||
},
|
||||
RpcThirdServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImThirdPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.ThirdPrometheusPort[0],
|
||||
},
|
||||
RpcUserServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImUserPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.UserPrometheusPort[0],
|
||||
},
|
||||
}
|
||||
|
||||
if portMap, ok := portConfigMap[a.Name]; ok {
|
||||
if port, ok := portMap[portType]; ok {
|
||||
return port, nil
|
||||
} else {
|
||||
return 0, errs.Wrap(errors.New("port type not found"), fmt.Sprintf("Failed to get port for %s", a.Name))
|
||||
func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
||||
switch a.Name {
|
||||
case RpcPushServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImPushPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.PushPrometheusPort[0]
|
||||
}
|
||||
case RpcAuthServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImAuthPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.AuthPrometheusPort[0]
|
||||
}
|
||||
case RpcConversationServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImConversationPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.ConversationPrometheusPort[0]
|
||||
}
|
||||
case RpcFriendServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImFriendPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.FriendPrometheusPort[0]
|
||||
}
|
||||
case RpcGroupServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImGroupPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.GroupPrometheusPort[0]
|
||||
}
|
||||
case RpcMsgServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImMessagePort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.MessagePrometheusPort[0]
|
||||
}
|
||||
case RpcThirdServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImThirdPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.ThirdPrometheusPort[0]
|
||||
}
|
||||
case RpcUserServer:
|
||||
if portType == constant.FlagPort {
|
||||
return a.config.RpcPort.OpenImUserPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return a.config.Prometheus.UserPrometheusPort[0]
|
||||
}
|
||||
}
|
||||
|
||||
return 0, errs.Wrap(fmt.Errorf("server name '%s' not found", a.Name), "Failed to get port configuration")
|
||||
return 0
|
||||
}
|
||||
|
||||
func (a *RpcCmd) GetRpcRegisterNameFromConfig() (string, error) {
|
||||
switch a.Name {
|
||||
case RpcPushServer:
|
||||
return a.config.RpcRegisterName.OpenImPushName, nil
|
||||
case RpcAuthServer:
|
||||
return a.config.RpcRegisterName.OpenImAuthName, nil
|
||||
case RpcConversationServer:
|
||||
return a.config.RpcRegisterName.OpenImConversationName, nil
|
||||
case RpcFriendServer:
|
||||
return a.config.RpcRegisterName.OpenImFriendName, nil
|
||||
case RpcGroupServer:
|
||||
return a.config.RpcRegisterName.OpenImGroupName, nil
|
||||
case RpcMsgServer:
|
||||
return a.config.RpcRegisterName.OpenImMsgName, nil
|
||||
case RpcThirdServer:
|
||||
return a.config.RpcRegisterName.OpenImThirdName, nil
|
||||
case RpcUserServer:
|
||||
return a.config.RpcRegisterName.OpenImUserName, nil
|
||||
}
|
||||
return "", errs.Wrap(errors.New("can not get rpc register name"), a.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user