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:
OpenIM-Gordon
2024-03-05 17:53:22 +08:00
committed by GitHub
parent efb8310531
commit 383758782e
114 changed files with 1743 additions and 1802 deletions
+20 -30
View File
@@ -15,54 +15,44 @@
package cmd
import (
"errors"
"fmt"
"github.com/OpenIMSDK/protocol/constant"
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/internal/api"
"github.com/spf13/cobra"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
)
type ApiCmd struct {
*RootCmd
initFunc func(config *config.GlobalConfig, port int, promPort int) error
}
func NewApiCmd() *ApiCmd {
ret := &ApiCmd{NewRootCmd("api")}
ret := &ApiCmd{RootCmd: NewRootCmd("api"), initFunc: api.Start}
ret.SetRootCmdPt(ret)
ret.addPreRun()
ret.addRunE()
return ret
}
// AddApi configures the API command to run with specified ports for the API and Prometheus monitoring.
// It ensures error handling for port retrieval and only proceeds if both port numbers are successfully obtained.
func (a *ApiCmd) AddApi(f func(port int, promPort int) error) {
func (a *ApiCmd) addPreRun() {
a.Command.PreRun = func(cmd *cobra.Command, args []string) {
a.port = a.getPortFlag(cmd)
a.prometheusPort = a.getPrometheusPortFlag(cmd)
}
}
func (a *ApiCmd) addRunE() {
a.Command.RunE = func(cmd *cobra.Command, args []string) error {
port, err := a.getPortFlag(cmd)
if err != nil {
return err
}
promPort, err := a.getPrometheusPortFlag(cmd)
if err != nil {
return err
}
return f(port, promPort)
return a.initFunc(a.config, a.port, a.prometheusPort)
}
}
func (a *ApiCmd) GetPortFromConfig(portType string) (int, error) {
func (a *ApiCmd) GetPortFromConfig(portType string) int {
if portType == constant.FlagPort {
if len(config2.Config.Api.OpenImApiPort) > 0 {
return config2.Config.Api.OpenImApiPort[0], nil
}
return 0, errors.New("API port configuration is empty or missing")
return a.config.Api.OpenImApiPort[0]
} else if portType == constant.FlagPrometheusPort {
if len(config2.Config.Prometheus.ApiPrometheusPort) > 0 {
return config2.Config.Prometheus.ApiPrometheusPort[0], nil
}
return 0, errors.New("Prometheus port configuration is empty or missing")
return a.config.Prometheus.ApiPrometheusPort[0]
}
return 0, fmt.Errorf("unknown port type: %s", portType)
return 0
}
+14 -8
View File
@@ -14,29 +14,35 @@
package cmd
import "github.com/spf13/cobra"
import (
"github.com/openimsdk/open-im-server/v3/internal/tools"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/spf13/cobra"
)
type CronTaskCmd struct {
*RootCmd
initFunc func(config *config.GlobalConfig) error
}
func NewCronTaskCmd() *CronTaskCmd {
ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())}
ret := &CronTaskCmd{RootCmd: NewRootCmd("cronTask", WithCronTaskLogName()),
initFunc: tools.StartTask}
ret.addRunE()
ret.SetRootCmdPt(ret)
return ret
}
func (c *CronTaskCmd) addRunE(f func() error) {
func (c *CronTaskCmd) addRunE() {
c.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f()
return c.initFunc(c.config)
}
}
func (c *CronTaskCmd) Exec(f func() error) error {
c.addRunE(f)
func (c *CronTaskCmd) Exec() error {
return c.Execute()
}
func (c *CronTaskCmd) GetPortFromConfig(portType string) (int, error) {
return 0, nil
func (c *CronTaskCmd) GetPortFromConfig(portType string) int {
return 0
}
+16 -43
View File
@@ -15,13 +15,13 @@
package cmd
import (
"errors"
"log"
"github.com/spf13/cobra"
"github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/tools/errs"
"github.com/openimsdk/open-im-server/v3/internal/msggateway"
v3config "github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/spf13/cobra"
)
type MsgGatewayCmd struct {
@@ -30,6 +30,7 @@ type MsgGatewayCmd struct {
func NewMsgGatewayCmd() *MsgGatewayCmd {
ret := &MsgGatewayCmd{NewRootCmd("msgGateway")}
ret.addRunE()
ret.SetRootCmdPt(ret)
return ret
}
@@ -38,67 +39,39 @@ func (m *MsgGatewayCmd) AddWsPortFlag() {
m.Command.Flags().IntP(constant.FlagWsPort, "w", 0, "ws server listen port")
}
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) (int, error) {
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int {
port, err := cmd.Flags().GetInt(constant.FlagWsPort)
if err != nil {
return 0, errs.Wrap(err, "error getting ws port flag")
log.Println("Error getting ws port flag:", err)
}
if port == 0 {
port, _ = m.PortFromConfig(constant.FlagWsPort)
port = m.PortFromConfig(constant.FlagWsPort)
}
return port, nil
return port
}
func (m *MsgGatewayCmd) addRunE() {
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
wsPort, err := m.getWsPortFlag(cmd)
if err != nil {
return errs.Wrap(err, "failed to get WS port flag")
}
port, err := m.getPortFlag(cmd)
if err != nil {
return err
}
prometheusPort, err := m.getPrometheusPortFlag(cmd)
if err != nil {
return err
}
return msggateway.RunWsAndServer(port, wsPort, prometheusPort)
return msggateway.RunWsAndServer(m.config, m.getPortFlag(cmd), m.getWsPortFlag(cmd), m.getPrometheusPortFlag(cmd))
}
}
func (m *MsgGatewayCmd) Exec() error {
m.addRunE()
return m.Execute()
}
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) (int, error) {
var port int
var exists bool
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int {
switch portType {
case constant.FlagWsPort:
if len(v3config.Config.LongConnSvr.OpenImWsPort) > 0 {
port = v3config.Config.LongConnSvr.OpenImWsPort[0]
exists = true
}
return m.config.LongConnSvr.OpenImWsPort[0]
case constant.FlagPort:
if len(v3config.Config.LongConnSvr.OpenImMessageGatewayPort) > 0 {
port = v3config.Config.LongConnSvr.OpenImMessageGatewayPort[0]
exists = true
}
return m.config.LongConnSvr.OpenImMessageGatewayPort[0]
case constant.FlagPrometheusPort:
if len(v3config.Config.Prometheus.MessageGatewayPrometheusPort) > 0 {
port = v3config.Config.Prometheus.MessageGatewayPrometheusPort[0]
exists = true
}
}
return m.config.Prometheus.MessageGatewayPrometheusPort[0]
if !exists {
return 0, errs.Wrap(errors.New("port type '%s' not found in configuration"), portType)
default:
return 0
}
return port, nil
}
+1 -1
View File
@@ -44,7 +44,7 @@ func TestMsgGatewayCmd_GetPortFromConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.portType, func(t *testing.T) {
got, _ := msgGatewayCmd.GetPortFromConfig(tt.portType)
got := msgGatewayCmd.GetPortFromConfig(tt.portType)
assert.Equal(t, tt.want, got)
})
}
+11 -20
View File
@@ -16,11 +16,10 @@ package cmd
import (
"fmt"
"github.com/OpenIMSDK/protocol/constant"
"github.com/openimsdk/open-im-server/v3/internal/msgtransfer"
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/spf13/cobra"
"github.com/openimsdk/open-im-server/v3/internal/msgtransfer"
)
type MsgTransferCmd struct {
@@ -29,37 +28,29 @@ type MsgTransferCmd struct {
func NewMsgTransferCmd() *MsgTransferCmd {
ret := &MsgTransferCmd{NewRootCmd("msgTransfer")}
ret.addRunE()
ret.SetRootCmdPt(ret)
return ret
}
func (m *MsgTransferCmd) addRunE() {
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
prometheusPort, err := m.getPrometheusPortFlag(cmd)
if err != nil {
return err
}
return msgtransfer.StartTransfer(prometheusPort)
return msgtransfer.StartTransfer(m.config, m.getPrometheusPortFlag(cmd))
}
}
func (m *MsgTransferCmd) Exec() error {
m.addRunE()
return m.Execute()
}
func (m *MsgTransferCmd) GetPortFromConfig(portType string) (int, error) {
func (m *MsgTransferCmd) GetPortFromConfig(portType string) int {
if portType == constant.FlagPort {
return 0, nil
return 0
} else if portType == constant.FlagPrometheusPort {
n := m.getTransferProgressFlagValue()
if n < len(config2.Config.Prometheus.MessageTransferPrometheusPort) {
return config2.Config.Prometheus.MessageTransferPrometheusPort[n], nil
}
return 0, fmt.Errorf("index out of range for MessageTransferPrometheusPort with index %d", n)
return m.config.Prometheus.MessageTransferPrometheusPort[n]
}
return 0, fmt.Errorf("unknown port type: %s", portType)
return 0
}
func (m *MsgTransferCmd) AddTransferProgressFlag() {
@@ -67,10 +58,10 @@ func (m *MsgTransferCmd) AddTransferProgressFlag() {
}
func (m *MsgTransferCmd) getTransferProgressFlagValue() int {
nindex, err := m.Command.Flags().GetInt(constant.FlagTransferProgressIndex)
nIndex, err := m.Command.Flags().GetInt(constant.FlagTransferProgressIndex)
if err != nil {
fmt.Println("get transfercmd error,make sure it is k8s env or not")
fmt.Println("get transfer cmd error,make sure it is k8s env or not")
return 0
}
return nindex
return nIndex
}
+2 -1
View File
@@ -22,6 +22,7 @@ import (
type MsgUtilsCmd struct {
cobra.Command
MsgTool *tools.MsgTool
}
func (m *MsgUtilsCmd) AddUserIDFlag() {
@@ -135,7 +136,7 @@ func NewSeqCmd() *SeqCmd {
func (s *SeqCmd) GetSeqCmd() *cobra.Command {
s.Command.Run = func(cmdLines *cobra.Command, args []string) {
_, err := tools.InitMsgTool()
_, err := tools.InitMsgTool(s.MsgTool.Config)
if err != nil {
util.ExitWithError(err)
}
+22 -25
View File
@@ -26,7 +26,7 @@ import (
)
type RootCmdPt interface {
GetPortFromConfig(portType string) (int, error)
GetPortFromConfig(portType string) int
}
type RootCmd struct {
@@ -35,6 +35,11 @@ type RootCmd struct {
port int
prometheusPort int
cmdItf RootCmdPt
config *config.GlobalConfig
}
func (rc *RootCmd) Port() int {
return rc.port
}
type CmdOpts struct {
@@ -54,7 +59,7 @@ func WithLogName(logName string) func(*CmdOpts) {
}
func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd {
rootCmd := &RootCmd{Name: name}
rootCmd := &RootCmd{Name: name, config: config.NewGlobalConfig()}
cmd := cobra.Command{
Use: "Start openIM application",
Short: fmt.Sprintf(`Start %s `, name),
@@ -96,7 +101,7 @@ func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts {
}
func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error {
logConfig := config.Config.Log
logConfig := rc.config.Log
return log.InitFromConfig(
@@ -129,41 +134,36 @@ func (r *RootCmd) AddPortFlag() {
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
}
func (r *RootCmd) getPortFlag(cmd *cobra.Command) (int, error) {
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
port, err := cmd.Flags().GetInt(constant.FlagPort)
if err != nil {
// Wrapping the error with additional context
return 0, errs.Wrap(err, "error getting port flag")
return 0
}
if port == 0 {
port, _ = r.PortFromConfig(constant.FlagPort)
// port, err := r.PortFromConfig(constant.FlagPort)
// if err != nil {
// // Optionally wrap the error if it's an internal error needing context
// return 0, errs.Wrap(err, "error getting port from config")
// }
port = r.PortFromConfig(constant.FlagPort)
}
return port, nil
return port
}
// // GetPortFlag returns the port flag.
func (r *RootCmd) GetPortFlag() (int, error) {
return r.port, nil
func (r *RootCmd) GetPortFlag() int {
return r.port
}
func (r *RootCmd) AddPrometheusPortFlag() {
r.Command.Flags().IntP(constant.FlagPrometheusPort, "", 0, "server prometheus listen port")
}
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) (int, error) {
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
port, err := cmd.Flags().GetInt(constant.FlagPrometheusPort)
if err != nil || port == 0 {
port, err = r.PortFromConfig(constant.FlagPrometheusPort)
port = r.PortFromConfig(constant.FlagPrometheusPort)
if err != nil {
return 0, err
return 0
}
}
return port, nil
return port
}
func (r *RootCmd) GetPrometheusPortFlag() int {
@@ -173,7 +173,7 @@ func (r *RootCmd) GetPrometheusPortFlag() int {
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
fmt.Println("The directory of the configuration file to start the process:", configFolderPath)
return config2.InitConfig(configFolderPath)
return config2.InitConfig(r.config, configFolderPath)
}
func (r *RootCmd) Execute() error {
@@ -184,11 +184,8 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
r.Command.AddCommand(cmds...)
}
func (r *RootCmd) PortFromConfig(portType string) (int, error) {
func (r *RootCmd) PortFromConfig(portType string) int {
// Retrieve the port and cache it
port, err := r.cmdItf.GetPortFromConfig(portType)
if err != nil {
return 0, err
}
return port, nil
port := r.cmdItf.GetPortFromConfig(portType)
return port
}
+117 -73
View File
@@ -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)
}