mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-14 05:56: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:
+33
-60
@@ -21,10 +21,10 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
//go:embed version
|
||||
@@ -36,38 +36,32 @@ const (
|
||||
DefaultFolderPath = "../config/"
|
||||
)
|
||||
|
||||
// GetDefaultConfigPath returns the absolute path to the default configuration directory
|
||||
// relative to the executable's location. It is intended for use in Kubernetes container configurations.
|
||||
// Errors are returned to the caller to allow for flexible error handling.
|
||||
func GetDefaultConfigPath() (string, error) {
|
||||
// return absolude path join ../config/, this is k8s container config path.
|
||||
func GetDefaultConfigPath() string {
|
||||
executablePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", errs.Wrap(err, "failed to get executable path")
|
||||
fmt.Println("GetDefaultConfigPath error:", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
// Calculate the config path as a directory relative to the executable's location
|
||||
configPath, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../config/"))
|
||||
if err != nil {
|
||||
return "", errs.Wrap(err, "failed to get output directory")
|
||||
fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return configPath, nil
|
||||
return configPath
|
||||
}
|
||||
|
||||
// GetProjectRoot returns the absolute path of the project root directory by navigating up from the directory
|
||||
// containing the executable. It provides a detailed error if the path cannot be determined.
|
||||
func GetProjectRoot() (string, error) {
|
||||
executablePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", errs.Wrap(err, "failed to retrieve executable path")
|
||||
}
|
||||
// getProjectRoot returns the absolute path of the project root directory.
|
||||
func GetProjectRoot() string {
|
||||
executablePath, _ := os.Executable()
|
||||
|
||||
// Attempt to compute the project root by navigating up from the executable's directory
|
||||
projectRoot, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../../../../.."))
|
||||
if err != nil {
|
||||
return "", err
|
||||
fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return projectRoot, nil
|
||||
return projectRoot
|
||||
}
|
||||
|
||||
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
||||
@@ -93,62 +87,41 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
||||
// If the specified config file does not exist, it attempts to load from the project's default "config" directory.
|
||||
// It logs informative messages regarding the configuration path being used.
|
||||
func initConfig(config any, configName, configFolderPath string) error {
|
||||
configFilePath := filepath.Join(configFolderPath, configName)
|
||||
_, err := os.Stat(configFilePath)
|
||||
configFolderPath = filepath.Join(configFolderPath, configName)
|
||||
_, err := os.Stat(configFolderPath)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return errs.Wrap(err, fmt.Sprintf("failed to check existence of config file at path: %s", configFilePath))
|
||||
fmt.Println("stat config path error:", err.Error())
|
||||
return fmt.Errorf("stat config path error: %w", err)
|
||||
}
|
||||
var projectRoot string
|
||||
projectRoot, err = GetProjectRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
configFilePath = filepath.Join(projectRoot, "config", configName)
|
||||
fmt.Printf("Configuration file not found at specified path. Falling back to project path: %s\n", configFilePath)
|
||||
configFolderPath = filepath.Join(GetProjectRoot(), "config", configName)
|
||||
fmt.Println("flag's path,enviment's path,default path all is not exist,using project path:", configFolderPath)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configFilePath)
|
||||
data, err := os.ReadFile(configFolderPath)
|
||||
if err != nil {
|
||||
// Wrap and return the error if reading the configuration file fails.
|
||||
return errs.Wrap(err, fmt.Sprintf("failed to read configuration file at path: %s", configFilePath))
|
||||
return fmt.Errorf("read file error: %w", err)
|
||||
}
|
||||
|
||||
if err = yaml.Unmarshal(data, config); err != nil {
|
||||
// Wrap and return the error if unmarshalling the YAML configuration fails.
|
||||
return errs.Wrap(err, "failed to unmarshal YAML configuration")
|
||||
return fmt.Errorf("unmarshal yaml error: %w", err)
|
||||
}
|
||||
fmt.Println("The path of the configuration file to start the process:", configFolderPath)
|
||||
|
||||
fmt.Printf("Configuration file loaded successfully from path: %s\n", configFilePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitConfig initializes the application configuration by loading it from a specified folder path.
|
||||
// If the folder path is not provided, it attempts to use the OPENIMCONFIG environment variable,
|
||||
// and as a fallback, it uses the default configuration path. It loads both the main configuration
|
||||
// and notification configuration, wrapping errors for better context.
|
||||
func InitConfig(configFolderPath string) error {
|
||||
// Use the provided config folder path, or fallback to environment variable or default path
|
||||
func InitConfig(config *GlobalConfig, configFolderPath string) error {
|
||||
if configFolderPath == "" {
|
||||
configFolderPath = os.Getenv("OPENIMCONFIG")
|
||||
if configFolderPath == "" {
|
||||
var err error
|
||||
configFolderPath, err = GetDefaultConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
envConfigPath := os.Getenv("OPENIMCONFIG")
|
||||
if envConfigPath != "" {
|
||||
configFolderPath = envConfigPath
|
||||
} else {
|
||||
configFolderPath = GetDefaultConfigPath()
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the main configuration
|
||||
if err := initConfig(&Config, FileName, configFolderPath); err != nil {
|
||||
if err := initConfig(config, FileName, configFolderPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Initialize the notification configuration
|
||||
if err := initConfig(&Config.Notification, NotificationFileName, configFolderPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return initConfig(&config.Notification, NotificationFileName, configFolderPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user