|
|
@ -21,30 +21,33 @@ import (
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/protocol/constant"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
|
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/protocol/constant"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
//go:embed version
|
|
|
|
//go:embed version
|
|
|
|
var Version string
|
|
|
|
var Version string
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
_, b, _, _ = runtime.Caller(0)
|
|
|
|
|
|
|
|
// Root folder of this project.
|
|
|
|
|
|
|
|
Root = filepath.Join(filepath.Dir(b), "../../..")
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
FileName = "config.yaml"
|
|
|
|
FileName = "config.yaml"
|
|
|
|
NotificationFileName = "notification.yaml"
|
|
|
|
NotificationFileName = "notification.yaml"
|
|
|
|
DefaultFolderPath = "../config/"
|
|
|
|
DefaultFolderPath = "../config/"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// getProjectRoot returns the absolute path of the project root directory
|
|
|
|
|
|
|
|
func getProjectRoot() string {
|
|
|
|
|
|
|
|
// Program counter (PC): This represents the address of the function.
|
|
|
|
|
|
|
|
// File path: The full path to the source file from which the function was called.
|
|
|
|
|
|
|
|
// Line number: The line number in the source file from which the function was called.
|
|
|
|
|
|
|
|
// Success flag: it will be true if the information was successfully fetched, false otherwise.
|
|
|
|
|
|
|
|
_, b, _, _ := runtime.Caller(0) // pkg/common/config/parse.go
|
|
|
|
|
|
|
|
return filepath.Join(filepath.Dir(b), "../../..")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
|
|
|
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
|
|
|
opts := msgprocessor.NewOptions()
|
|
|
|
opts := msgprocessor.NewOptions()
|
|
|
|
|
|
|
|
|
|
|
|
if cfg.UnreadCount {
|
|
|
|
if cfg.UnreadCount {
|
|
|
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
|
|
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -61,40 +64,31 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func initConfig(config interface{}, configName, configFolderPath string) error {
|
|
|
|
func initConfig(config interface{}, configName, configFolderPath string) error {
|
|
|
|
if configFolderPath == "" {
|
|
|
|
configFolderPath = filepath.Join(configFolderPath, configName)
|
|
|
|
configFolderPath = DefaultFolderPath
|
|
|
|
_, err := os.Stat(configFolderPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := filepath.Join(configFolderPath, configName)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
fmt.Println("use config", configPath)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err := os.Stat(configPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
return fmt.Errorf("stat config path error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configPath = filepath.Join(Root, "config", configName)
|
|
|
|
configFolderPath = filepath.Join(getProjectRoot(), "config", configName)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Root = filepath.Dir(configPath)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
|
|
data, err := os.ReadFile(configFolderPath)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return fmt.Errorf("read file error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = yaml.Unmarshal(data, config); err != nil {
|
|
|
|
if err = yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return err
|
|
|
|
return fmt.Errorf("unmarshal yaml error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("use config", configFolderPath)
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func InitConfig(configFolderPath string) error {
|
|
|
|
func InitConfig(configFolderPath string) error {
|
|
|
|
err := initConfig(&Config, FileName, configFolderPath)
|
|
|
|
if configFolderPath == "" {
|
|
|
|
if err != nil {
|
|
|
|
configFolderPath = DefaultFolderPath
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = initConfig(&Config.Notification, NotificationFileName, configFolderPath)
|
|
|
|
if err := initConfig(&Config, FileName, configFolderPath); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return initConfig(&Config.Notification, NotificationFileName, configFolderPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|