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:
+20
-30
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user