Files
open-im-server/pkg/common/config/parse.go
T

108 lines
3.5 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-06-30 09:45:02 +08:00
package config
import (
2023-07-03 14:12:29 +08:00
"os"
"path/filepath"
"gopkg.in/yaml.v3"
2023-11-02 12:27:59 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/field"
2023-06-30 09:45:02 +08:00
)
const (
2024-12-25 18:08:08 +08:00
DefaultFolderPath = "../config/"
2023-06-30 09:45:02 +08:00
)
// return absolude path join ../config/, this is k8s container config path.
2024-04-19 22:23:08 +08:00
func GetDefaultConfigPath() (string, error) {
executablePath, err := os.Executable()
2023-11-07 14:36:56 +08:00
if err != nil {
2024-04-19 22:23:08 +08:00
return "", errs.WrapMsg(err, "failed to get executable path")
2023-11-07 14:36:56 +08:00
}
2024-04-19 22:23:08 +08:00
configPath, err := field.OutDir(filepath.Join(filepath.Dir(executablePath), "../config/"))
if err != nil {
2024-04-19 22:23:08 +08:00
return "", errs.WrapMsg(err, "failed to get output directory", "outDir", filepath.Join(filepath.Dir(executablePath), "../config/"))
}
2024-04-19 22:23:08 +08:00
return configPath, nil
2023-11-07 14:36:56 +08:00
}
// getProjectRoot returns the absolute path of the project root directory.
2024-04-19 22:23:08 +08:00
func GetProjectRoot() (string, error) {
executablePath, err := os.Executable()
if err != nil {
return "", errs.Wrap(err)
}
projectRoot, err := field.OutDir(filepath.Join(filepath.Dir(executablePath), "../../../../.."))
if err != nil {
2024-04-19 22:23:08 +08:00
return "", errs.Wrap(err)
}
2024-04-19 22:23:08 +08:00
return projectRoot, nil
}
func GetOptionsByNotification(cfg NotificationConfig, sendNotification *bool) msgprocessor.Options {
opts := msgprocessor.NewOptions()
if sendNotification != nil {
cfg.IsSendMsg = *sendNotification
}
if cfg.IsSendMsg {
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
2023-06-30 09:45:02 +08:00
}
if cfg.OfflinePush.Enable {
opts = msgprocessor.WithOptions(opts, msgprocessor.WithOfflinePush(true))
2023-06-30 09:45:02 +08:00
}
switch cfg.ReliabilityLevel {
case constant.UnreliableNotification:
case constant.ReliableNotificationNoMsg:
opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent())
2023-06-30 09:45:02 +08:00
}
opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg))
2023-06-30 09:45:02 +08:00
return opts
}
// initConfig loads configuration from a specified path into the provided config structure.
// 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 {
configFolderPath = filepath.Join(configFolderPath, configName)
_, err := os.Stat(configFolderPath)
2023-06-30 09:45:02 +08:00
if err != nil {
if !os.IsNotExist(err) {
2024-04-19 22:23:08 +08:00
return errs.WrapMsg(err, "stat config path error", "config Folder Path", configFolderPath)
}
path, err := GetProjectRoot()
if err != nil {
return err
}
2024-04-19 22:23:08 +08:00
configFolderPath = filepath.Join(path, "config", configName)
2023-06-30 09:45:02 +08:00
}
data, err := os.ReadFile(configFolderPath)
2023-06-30 09:45:02 +08:00
if err != nil {
2024-04-19 22:23:08 +08:00
return errs.WrapMsg(err, "read file error", "config Folder Path", configFolderPath)
2023-06-30 09:45:02 +08:00
}
if err = yaml.Unmarshal(data, config); err != nil {
2024-04-19 22:23:08 +08:00
return errs.WrapMsg(err, "unmarshal yaml error", "config Folder Path", configFolderPath)
}
return nil
2023-06-30 09:45:02 +08:00
}