You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.6 KiB
105 lines
2.6 KiB
// 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.
|
|
|
|
package config
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"github.com/OpenIMSDK/tools/config"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/OpenIMSDK/tools/constant"
|
|
"github.com/OpenIMSDK/tools/utils"
|
|
)
|
|
|
|
//go:embed version
|
|
var version string
|
|
|
|
func init() {
|
|
config.Version = version
|
|
}
|
|
|
|
var (
|
|
_, b, _, _ = runtime.Caller(0)
|
|
// Root folder of this project.
|
|
Root = filepath.Join(filepath.Dir(b), "../../..")
|
|
)
|
|
|
|
const (
|
|
FileName = "config.yaml"
|
|
NotificationFileName = "notification.yaml"
|
|
DefaultFolderPath = "../config/"
|
|
)
|
|
|
|
func GetOptionsByNotification(cfg config.NotificationConf) utils.Options {
|
|
opts := utils.NewOptions()
|
|
if cfg.UnreadCount {
|
|
opts = utils.WithOptions(opts, utils.WithUnreadCount(true))
|
|
}
|
|
if cfg.OfflinePush.Enable {
|
|
opts = utils.WithOptions(opts, utils.WithOfflinePush(true))
|
|
}
|
|
switch cfg.ReliabilityLevel {
|
|
case constant.UnreliableNotification:
|
|
case constant.ReliableNotificationNoMsg:
|
|
opts = utils.WithOptions(opts, utils.WithHistory(true), utils.WithPersistent())
|
|
}
|
|
opts = utils.WithOptions(opts, utils.WithSendMsg(cfg.IsSendMsg))
|
|
return opts
|
|
}
|
|
|
|
func initConfig(config interface{}, configName, configFolderPath string) error {
|
|
if configFolderPath == "" {
|
|
configFolderPath = DefaultFolderPath
|
|
}
|
|
configPath := filepath.Join(configFolderPath, configName)
|
|
defer func() {
|
|
fmt.Println("use config", configPath)
|
|
}()
|
|
_, err := os.Stat(configPath)
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
configPath = filepath.Join(Root, "config", configName)
|
|
} else {
|
|
Root = filepath.Dir(configPath)
|
|
}
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = yaml.Unmarshal(data, config); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func InitConfig(configFolderPath string) error {
|
|
err := initConfig(&config.Config, FileName, configFolderPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = initConfig(&config.Config.Notification, NotificationFileName, configFolderPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|