复制项目
This commit is contained in:
247
pkg/common/config/config.go
Normal file
247
pkg/common/config/config.go
Normal file
@@ -0,0 +1,247 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/openimsdk/tools/db/mongoutil"
|
||||
"github.com/openimsdk/tools/db/redisutil"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed version
|
||||
Version string
|
||||
//go:embed template.xlsx
|
||||
ImportTemplate []byte
|
||||
)
|
||||
|
||||
type Share struct {
|
||||
OpenIM struct {
|
||||
ApiURL string `mapstructure:"apiURL"`
|
||||
Secret string `mapstructure:"secret"`
|
||||
AdminUserID string `mapstructure:"adminUserID"`
|
||||
TokenRefreshInterval int `mapstructure:"tokenRefreshInterval"`
|
||||
} `mapstructure:"openIM"`
|
||||
ChatAdmin []string `mapstructure:"chatAdmin"`
|
||||
ProxyHeader string `mapstructure:"proxyHeader"`
|
||||
}
|
||||
|
||||
type RpcService struct {
|
||||
Chat string `mapstructure:"chat"`
|
||||
Admin string `mapstructure:"admin"`
|
||||
Bot string `mapstructure:"bot"`
|
||||
}
|
||||
|
||||
func (r *RpcService) GetServiceNames() []string {
|
||||
return []string{
|
||||
r.Chat,
|
||||
r.Admin,
|
||||
}
|
||||
}
|
||||
|
||||
type API struct {
|
||||
Api struct {
|
||||
ListenIP string `mapstructure:"listenIP"`
|
||||
Ports []int `mapstructure:"ports"`
|
||||
} `mapstructure:"api"`
|
||||
}
|
||||
|
||||
type APIBot struct {
|
||||
Api struct {
|
||||
ListenIP string `mapstructure:"listenIP"`
|
||||
Ports []int `mapstructure:"ports"`
|
||||
} `mapstructure:"api"`
|
||||
}
|
||||
|
||||
type Mongo struct {
|
||||
URI string `mapstructure:"uri"`
|
||||
Address []string `mapstructure:"address"`
|
||||
Database string `mapstructure:"database"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
AuthSource string `mapstructure:"authSource"`
|
||||
MaxPoolSize int `mapstructure:"maxPoolSize"`
|
||||
MaxRetry int `mapstructure:"maxRetry"`
|
||||
}
|
||||
|
||||
func (m *Mongo) Build() *mongoutil.Config {
|
||||
return &mongoutil.Config{
|
||||
Uri: m.URI,
|
||||
Address: m.Address,
|
||||
Database: m.Database,
|
||||
Username: m.Username,
|
||||
Password: m.Password,
|
||||
AuthSource: m.AuthSource,
|
||||
MaxPoolSize: m.MaxPoolSize,
|
||||
MaxRetry: m.MaxRetry,
|
||||
}
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
Address []string `mapstructure:"address"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
EnablePipeline bool `mapstructure:"enablePipeline"`
|
||||
ClusterMode bool `mapstructure:"clusterMode"`
|
||||
DB int `mapstructure:"db"`
|
||||
MaxRetry int `mapstructure:"MaxRetry"`
|
||||
}
|
||||
|
||||
func (r *Redis) Build() *redisutil.Config {
|
||||
return &redisutil.Config{
|
||||
ClusterMode: r.ClusterMode,
|
||||
Address: r.Address,
|
||||
Username: r.Username,
|
||||
Password: r.Password,
|
||||
DB: r.DB,
|
||||
MaxRetry: r.MaxRetry,
|
||||
}
|
||||
}
|
||||
|
||||
type Discovery struct {
|
||||
Enable string `mapstructure:"enable"`
|
||||
Etcd Etcd `mapstructure:"etcd"`
|
||||
Kubernetes Kubernetes `mapstructure:"kubernetes"`
|
||||
RpcService RpcService `mapstructure:"rpcService"`
|
||||
}
|
||||
|
||||
type Kubernetes struct {
|
||||
Namespace string `mapstructure:"namespace"`
|
||||
}
|
||||
|
||||
type Etcd struct {
|
||||
RootDirectory string `mapstructure:"rootDirectory"`
|
||||
Address []string `mapstructure:"address"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
RPC struct {
|
||||
RegisterIP string `mapstructure:"registerIP"`
|
||||
ListenIP string `mapstructure:"listenIP"`
|
||||
Ports []int `mapstructure:"ports"`
|
||||
} `mapstructure:"rpc"`
|
||||
VerifyCode VerifyCode `mapstructure:"verifyCode"`
|
||||
LiveKit struct {
|
||||
URL string `mapstructure:"url"`
|
||||
Key string `mapstructure:"key"`
|
||||
Secret string `mapstructure:"secret"`
|
||||
} `mapstructure:"liveKit"`
|
||||
AllowRegister bool `mapstructure:"allowRegister"`
|
||||
}
|
||||
|
||||
type Bot struct {
|
||||
RPC struct {
|
||||
RegisterIP string `mapstructure:"registerIP"`
|
||||
ListenIP string `mapstructure:"listenIP"`
|
||||
Ports []int `mapstructure:"ports"`
|
||||
} `mapstructure:"rpc"`
|
||||
Timeout int `mapstructure:"timeout"`
|
||||
}
|
||||
type VerifyCode struct {
|
||||
ValidTime int `mapstructure:"validTime"`
|
||||
ValidCount int `mapstructure:"validCount"`
|
||||
UintTime int `mapstructure:"uintTime"`
|
||||
MaxCount int `mapstructure:"maxCount"`
|
||||
SuperCode string `mapstructure:"superCode"`
|
||||
Len int `mapstructure:"len"`
|
||||
Phone struct {
|
||||
Use string `mapstructure:"use"`
|
||||
Ali struct {
|
||||
Endpoint string `mapstructure:"endpoint"`
|
||||
AccessKeyID string `mapstructure:"accessKeyId"`
|
||||
AccessKeySecret string `mapstructure:"accessKeySecret"`
|
||||
SignName string `mapstructure:"signName"`
|
||||
VerificationCodeTemplateCode string `mapstructure:"verificationCodeTemplateCode"`
|
||||
} `mapstructure:"ali"`
|
||||
Bao struct {
|
||||
Endpoint string `mapstructure:"endpoint"`
|
||||
AccessKeyID string `mapstructure:"accessKeyId"`
|
||||
AccessKeySecret string `mapstructure:"accessKeySecret"`
|
||||
SignName string `mapstructure:"signName"`
|
||||
VerificationCodeTemplateCode string `mapstructure:"verificationCodeTemplateCode"`
|
||||
} `mapstructure:"bao"`
|
||||
} `mapstructure:"phone"`
|
||||
Mail struct {
|
||||
Use string `mapstructure:"use"`
|
||||
Title string `mapstructure:"title"`
|
||||
SenderMail string `mapstructure:"senderMail"`
|
||||
SenderAuthorizationCode string `mapstructure:"senderAuthorizationCode"`
|
||||
SMTPAddr string `mapstructure:"smtpAddr"`
|
||||
SMTPPort int `mapstructure:"smtpPort"`
|
||||
} `mapstructure:"mail"`
|
||||
}
|
||||
|
||||
type Admin struct {
|
||||
RPC struct {
|
||||
RegisterIP string `mapstructure:"registerIP"`
|
||||
ListenIP string `mapstructure:"listenIP"`
|
||||
Ports []int `mapstructure:"ports"`
|
||||
} `mapstructure:"rpc"`
|
||||
TokenPolicy struct {
|
||||
Expire int `mapstructure:"expire"`
|
||||
} `mapstructure:"tokenPolicy"`
|
||||
Secret string `mapstructure:"secret"`
|
||||
}
|
||||
|
||||
type Log struct {
|
||||
StorageLocation string `mapstructure:"storageLocation"`
|
||||
RotationTime uint `mapstructure:"rotationTime"`
|
||||
RemainRotationCount uint `mapstructure:"remainRotationCount"`
|
||||
RemainLogLevel int `mapstructure:"remainLogLevel"`
|
||||
IsStdout bool `mapstructure:"isStdout"`
|
||||
IsJson bool `mapstructure:"isJson"`
|
||||
IsSimplify bool `mapstructure:"isSimplify"`
|
||||
WithStack bool `mapstructure:"withStack"`
|
||||
}
|
||||
|
||||
type AllConfig struct {
|
||||
AdminAPI API
|
||||
ChatAPI API
|
||||
Admin Admin
|
||||
Chat Chat
|
||||
Discovery Discovery
|
||||
Log Log
|
||||
Mongo Mongo
|
||||
Redis Redis
|
||||
Share Share
|
||||
}
|
||||
|
||||
func (a *AllConfig) Name2Config(name string) any {
|
||||
switch name {
|
||||
case ChatAPIAdminCfgFileName:
|
||||
return a.AdminAPI
|
||||
case ChatAPIChatCfgFileName:
|
||||
return a.ChatAPI
|
||||
case ChatRPCAdminCfgFileName:
|
||||
return a.Admin
|
||||
case ChatRPCChatCfgFileName:
|
||||
return a.Chat
|
||||
case DiscoveryConfigFileName:
|
||||
return a.Discovery
|
||||
case LogConfigFileName:
|
||||
return a.Log
|
||||
case MongodbConfigFileName:
|
||||
return a.Mongo
|
||||
case RedisConfigFileName:
|
||||
return a.Redis
|
||||
case ShareFileName:
|
||||
return a.Share
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AllConfig) GetConfigNames() []string {
|
||||
return []string{
|
||||
ShareFileName,
|
||||
RedisConfigFileName,
|
||||
DiscoveryConfigFileName,
|
||||
MongodbConfigFileName,
|
||||
LogConfigFileName,
|
||||
ChatAPIAdminCfgFileName,
|
||||
ChatAPIChatCfgFileName,
|
||||
ChatRPCAdminCfgFileName,
|
||||
ChatRPCChatCfgFileName,
|
||||
}
|
||||
}
|
||||
48
pkg/common/config/env.go
Normal file
48
pkg/common/config/env.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ShareFileName = "share.yml"
|
||||
RedisConfigFileName = "redis.yml"
|
||||
DiscoveryConfigFileName = "discovery.yml"
|
||||
MongodbConfigFileName = "mongodb.yml"
|
||||
LogConfigFileName = "log.yml"
|
||||
ChatAPIAdminCfgFileName = "chat-api-admin.yml"
|
||||
ChatAPIChatCfgFileName = "chat-api-chat.yml"
|
||||
ChatAPIBotCfgFileName = "chat-api-bot.yml"
|
||||
ChatRPCAdminCfgFileName = "chat-rpc-admin.yml"
|
||||
ChatRPCChatCfgFileName = "chat-rpc-chat.yml"
|
||||
ChatRPCBotCfgFileName = "chat-rpc-bot.yml"
|
||||
)
|
||||
|
||||
var EnvPrefixMap map[string]string
|
||||
|
||||
func init() {
|
||||
EnvPrefixMap = make(map[string]string)
|
||||
fileNames := []string{
|
||||
ShareFileName,
|
||||
RedisConfigFileName,
|
||||
DiscoveryConfigFileName,
|
||||
MongodbConfigFileName,
|
||||
LogConfigFileName,
|
||||
ChatAPIAdminCfgFileName,
|
||||
ChatAPIChatCfgFileName,
|
||||
ChatRPCAdminCfgFileName,
|
||||
ChatRPCChatCfgFileName,
|
||||
}
|
||||
|
||||
for _, fileName := range fileNames {
|
||||
envKey := strings.TrimSuffix(strings.TrimSuffix(fileName, ".yml"), ".yaml")
|
||||
envKey = "CHATENV_" + envKey
|
||||
envKey = strings.ToUpper(strings.ReplaceAll(envKey, "-", "_"))
|
||||
EnvPrefixMap[fileName] = envKey
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
FlagConf = "config_folder_path"
|
||||
FlagTransferIndex = "index"
|
||||
)
|
||||
45
pkg/common/config/load.go
Normal file
45
pkg/common/config/load.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.imall.cloud/openim/chat/pkg/common/constant"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func Load(configDirectory string, configFileName string, envPrefix string, runtimeEnv string, config any) error {
|
||||
if runtimeEnv == constant.KUBERNETES {
|
||||
mountPath := os.Getenv(constant.MountConfigFilePath)
|
||||
if mountPath == "" {
|
||||
return errs.ErrArgs.WrapMsg(constant.MountConfigFilePath + " env is empty")
|
||||
}
|
||||
|
||||
return loadConfig(filepath.Join(mountPath, configFileName), envPrefix, config)
|
||||
}
|
||||
|
||||
return loadConfig(filepath.Join(configDirectory, configFileName), envPrefix, config)
|
||||
}
|
||||
|
||||
func loadConfig(path string, envPrefix string, config any) error {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetEnvPrefix(envPrefix)
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return errs.WrapMsg(err, "failed to read config file", "path", path, "envPrefix", envPrefix)
|
||||
}
|
||||
|
||||
if err := v.Unmarshal(config, func(config *mapstructure.DecoderConfig) {
|
||||
config.TagName = "mapstructure"
|
||||
}); err != nil {
|
||||
return errs.WrapMsg(err, "failed to unmarshal config", "path", path, "envPrefix", envPrefix)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
BIN
pkg/common/config/template.xlsx
Normal file
BIN
pkg/common/config/template.xlsx
Normal file
Binary file not shown.
1
pkg/common/config/version
Normal file
1
pkg/common/config/version
Normal file
@@ -0,0 +1 @@
|
||||
v1.8.0
|
||||
Reference in New Issue
Block a user