@ -2,42 +2,17 @@ package config
import (
import (
_ "embed"
_ "embed"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gopkg.in/yaml.v3"
)
)
//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 (
FileName = "config.yaml"
NotificationFileName = "notification.yaml"
ENV = "CONFIG_NAME"
DefaultFolderPath = "../config/"
ConfKey = "conf"
)
var Config config
var Config config
type CallBackConfig struct {
type CallBackConfig struct {
Enable bool ` yaml:"enable" `
Enable bool ` yaml:"enable" `
CallbackTimeOut int ` yaml:" callbackTimeO ut"`
CallbackTimeOut int ` yaml:"timeout" `
CallbackFailedContinue * bool ` yaml:" callbackF ailedContinue"`
CallbackFailedContinue * bool ` yaml:"failedContinue" `
}
}
type NotificationConf struct {
type NotificationConf struct {
@ -55,22 +30,74 @@ type POfflinePush struct {
}
}
type config struct {
type config struct {
ServerIP string ` yaml:"serverip" `
Zookeeper struct {
Schema string ` yaml:"schema" `
ZkAddr [ ] string ` yaml:"address" `
Username string ` yaml:"username" `
Password string ` yaml:"password" `
} ` yaml:"zookeeper" `
Mysql struct {
Address [ ] string ` yaml:"address" `
Username string ` yaml:"username" `
Password string ` yaml:"password" `
Database string ` yaml:"database" `
MaxOpenConn int ` yaml:"maxOpenConn" `
MaxIdleConn int ` yaml:"maxIdleConn" `
MaxLifeTime int ` yaml:"maxLifeTime" `
LogLevel int ` yaml:"logLevel" `
SlowThreshold int ` yaml:"slowThreshold" `
} ` yaml:"mysql" `
Mongo struct {
Uri string ` yaml:"uri" `
Address [ ] string ` yaml:"address" `
Database string ` yaml:"database" `
Username string ` yaml:"username" `
Password string ` yaml:"password" `
MaxPoolSize int ` yaml:"maxPoolSize" `
} ` yaml:"mongo" `
Redis struct {
Address [ ] string ` yaml:"address" `
Username string ` yaml:"username" `
Password string ` yaml:"password" `
} ` yaml:"redis" `
Kafka struct {
Username string ` yaml:"username" `
Password string ` yaml:"password" `
Addr [ ] string ` yaml:"addr" `
LatestMsgToRedis struct {
Topic string ` yaml:"topic" `
} ` yaml:"latestMsgToRedis" `
MsgToMongo struct {
Topic string ` yaml:"topic" `
} ` yaml:"offlineMsgToMongo" `
MsgToPush struct {
Topic string ` yaml:"topic" `
} ` yaml:"msgToPush" `
MsgToModify struct {
Topic string ` yaml:"topic" `
} ` yaml:"msgToModify" `
ConsumerGroupID struct {
MsgToRedis string ` yaml:"msgToRedis" `
MsgToMongo string ` yaml:"msgToMongo" `
MsgToMySql string ` yaml:"msgToMySql" `
MsgToPush string ` yaml:"msgToPush" `
MsgToModify string ` yaml:"msgToModify" `
} ` yaml:"consumerGroupID" `
} ` yaml:"kafka" `
RpcRegisterIP string ` yaml:"rpcRegisterIP" `
Rpc struct {
RegisterIP string ` yaml:"registerIP" `
ListenIP string ` yaml:"listenIP" `
ListenIP string ` yaml:"listenIP" `
} ` yaml:"rpc" `
ServerVersion string ` yaml:"serverversion" `
Api struct {
Api struct {
GinPort [ ] int ` yaml:"openImApiPort" `
OpenImApiPort [ ] int ` yaml:"openImApiPort" `
ListenIP string ` yaml:"listenIP" `
ListenIP string ` yaml:"listenIP" `
}
} ` yaml:"api" `
Sdk struct {
WsPort [ ] int ` yaml:"openImSdkWsPort" `
DataDir [ ] string ` yaml:"dataDir" `
}
Credential struct {
}
Object struct {
Object struct {
Enable string ` yaml:"enable" `
Enable string ` yaml:"enable" `
@ -90,7 +117,7 @@ type config struct {
Bucket string ` yaml:"bucket" `
Bucket string ` yaml:"bucket" `
SecretID string ` yaml:"secretID" `
SecretID string ` yaml:"secretID" `
SecretKey string ` yaml:"secretKey" `
SecretKey string ` yaml:"secretKey" `
}
} ` yaml:"tencent" `
Ali struct {
Ali struct {
RegionID string ` yaml:"regionID" `
RegionID string ` yaml:"regionID" `
AccessKeyID string ` yaml:"accessKeyID" `
AccessKeyID string ` yaml:"accessKeyID" `
@ -101,7 +128,7 @@ type config struct {
FinalHost string ` yaml:"finalHost" `
FinalHost string ` yaml:"finalHost" `
StsDurationSeconds int64 ` yaml:"stsDurationSeconds" `
StsDurationSeconds int64 ` yaml:"stsDurationSeconds" `
OssRoleArn string ` yaml:"OssRoleArn" `
OssRoleArn string ` yaml:"OssRoleArn" `
}
} ` yaml:"ali" `
Aws struct {
Aws struct {
AccessKeyID string ` yaml:"accessKeyID" `
AccessKeyID string ` yaml:"accessKeyID" `
AccessKeySecret string ` yaml:"accessKeySecret" `
AccessKeySecret string ` yaml:"accessKeySecret" `
@ -112,43 +139,8 @@ type config struct {
ExternalId string ` yaml:"externalId" `
ExternalId string ` yaml:"externalId" `
RoleSessionName string ` yaml:"roleSessionName" `
RoleSessionName string ` yaml:"roleSessionName" `
} ` yaml:"aws" `
} ` yaml:"aws" `
}
} ` yaml:"object" `
Mysql struct {
DBAddress [ ] string ` yaml:"dbMysqlAddress" `
DBUserName string ` yaml:"dbMysqlUserName" `
DBPassword string ` yaml:"dbMysqlPassword" `
DBDatabaseName string ` yaml:"dbMysqlDatabaseName" `
DBTableName string ` yaml:"DBTableName" `
DBMsgTableNum int ` yaml:"dbMsgTableNum" `
DBMaxOpenConns int ` yaml:"dbMaxOpenConns" `
DBMaxIdleConns int ` yaml:"dbMaxIdleConns" `
DBMaxLifeTime int ` yaml:"dbMaxLifeTime" `
LogLevel int ` yaml:"logLevel" `
SlowThreshold int ` yaml:"slowThreshold" `
}
Mongo struct {
DBUri string ` yaml:"dbUri" `
DBAddress [ ] string ` yaml:"dbAddress" `
DBDirect bool ` yaml:"dbDirect" `
DBTimeout int ` yaml:"dbTimeout" `
DBDatabase string ` yaml:"dbDatabase" `
DBSource string ` yaml:"dbSource" `
DBUserName string ` yaml:"dbUserName" `
DBPassword string ` yaml:"dbPassword" `
DBMaxPoolSize int ` yaml:"dbMaxPoolSize" `
DBRetainChatRecords int ` yaml:"dbRetainChatRecords" `
ChatRecordsClearTime string ` yaml:"chatRecordsClearTime" `
}
Redis struct {
DBAddress [ ] string ` yaml:"dbAddress" `
DBMaxIdle int ` yaml:"dbMaxIdle" `
DBMaxActive int ` yaml:"dbMaxActive" `
DBIdleTimeout int ` yaml:"dbIdleTimeout" `
DBUserName string ` yaml:"dbUserName" `
DBPassWord string ` yaml:"dbPassWord" `
EnableCluster bool ` yaml:"enableCluster" `
}
RpcPort struct {
RpcPort struct {
OpenImUserPort [ ] int ` yaml:"openImUserPort" `
OpenImUserPort [ ] int ` yaml:"openImUserPort" `
OpenImFriendPort [ ] int ` yaml:"openImFriendPort" `
OpenImFriendPort [ ] int ` yaml:"openImFriendPort" `
@ -158,10 +150,10 @@ type config struct {
OpenImAuthPort [ ] int ` yaml:"openImAuthPort" `
OpenImAuthPort [ ] int ` yaml:"openImAuthPort" `
OpenImPushPort [ ] int ` yaml:"openImPushPort" `
OpenImPushPort [ ] int ` yaml:"openImPushPort" `
OpenImConversationPort [ ] int ` yaml:"openImConversationPort" `
OpenImConversationPort [ ] int ` yaml:"openImConversationPort" `
OpenImCachePort [ ] int ` yaml:"openImCachePort" `
OpenImRtcPort [ ] int ` yaml:"openImRtcPort" `
OpenImRtcPort [ ] int ` yaml:"openImRtcPort" `
OpenImThirdPort [ ] int ` yaml:"openImThirdPort" `
OpenImThirdPort [ ] int ` yaml:"openImThirdPort" `
}
} ` yaml:"rpcPort" `
RpcRegisterName struct {
RpcRegisterName struct {
OpenImUserName string ` yaml:"openImUserName" `
OpenImUserName string ` yaml:"openImUserName" `
OpenImFriendName string ` yaml:"openImFriendName" `
OpenImFriendName string ` yaml:"openImFriendName" `
@ -171,128 +163,89 @@ type config struct {
OpenImGroupName string ` yaml:"openImGroupName" `
OpenImGroupName string ` yaml:"openImGroupName" `
OpenImAuthName string ` yaml:"openImAuthName" `
OpenImAuthName string ` yaml:"openImAuthName" `
OpenImConversationName string ` yaml:"openImConversationName" `
OpenImConversationName string ` yaml:"openImConversationName" `
OpenImRtcName string ` yaml:"openImRtcName" `
OpenImThirdName string ` yaml:"openImThirdName" `
OpenImThirdName string ` yaml:"openImThirdName" `
}
} ` yaml:"rpcRegisterName" `
Zookeeper struct {
Schema string ` yaml:"schema" `
ZkAddr [ ] string ` yaml:"zkAddr" `
UserName string ` yaml:"userName" `
Password string ` yaml:"password" `
} ` yaml:"zookeeper" `
Log struct {
Log struct {
StorageLocation string ` yaml:"storageLocation" `
StorageLocation string ` yaml:"storageLocation" `
RotationTime int ` yaml:"rotationTime" `
RotationTime int ` yaml:"rotationTime" `
RemainRotationCount uint ` yaml:"remainRotationCount" `
RemainRotationCount uint ` yaml:"remainRotationCount" `
RemainLogLevel int ` yaml:"remainLogLevel" `
RemainLogLevel int ` yaml:"remainLogLevel" `
IsStdout bool ` yaml:"isStdout" `
IsStdout bool ` yaml:"isStdout" `
WithStack bool ` yaml:"withStack" `
IsJson bool ` yaml:"isJson" `
IsJson bool ` yaml:"isJson" `
}
WithStack bool ` yaml:"withStack" `
ModuleName struct {
} ` yaml:"log" `
LongConnSvrName string ` yaml:"longConnSvrName" `
MsgTransferName string ` yaml:"msgTransferName" `
PushName string ` yaml:"pushName" `
}
LongConnSvr struct {
LongConnSvr struct {
WebsocketPort [ ] int ` yaml:"openImWsPort" `
OpenImWsPort [ ] int ` yaml:"openImWsPort" `
WebsocketMaxConnNum int ` yaml:"websocketMaxConnNum" `
WebsocketMaxConnNum int ` yaml:"websocketMaxConnNum" `
WebsocketMaxMsgLen int ` yaml:"websocketMaxMsgLen" `
WebsocketMaxMsgLen int ` yaml:"websocketMaxMsgLen" `
WebsocketTime Out int ` yaml:"websocketTimeO ut"`
WebsocketTime out int ` yaml:"websocketTimeo ut"`
}
} ` yaml:"longConnSvr" `
Push struct {
Push struct {
Jpns struct {
Enable string ` yaml:"enable" `
AppKey string ` yaml:"appKey" `
GeTui struct {
MasterSecret string ` yaml:"masterSecret" `
PushUrl string ` yaml:"pushUrl" `
PushIntent string ` yaml:"pushIntent" `
Enable bool ` yaml:"enable" `
}
Getui struct {
PushUrl string ` yaml:"pushUrl" `
PushUrl string ` yaml:"pushUrl" `
AppKey string ` yaml:"appKey" `
AppKey string ` yaml:"appKey" `
Enable bool ` yaml:"enable" `
Intent string ` yaml:"intent" `
Intent string ` yaml:"intent" `
MasterSecret string ` yaml:"masterSecret" `
MasterSecret string ` yaml:"masterSecret" `
ChannelID string ` yaml:"channelID" `
ChannelID string ` yaml:"channelID" `
ChannelName string ` yaml:"channelName" `
ChannelName string ` yaml:"channelName" `
}
} ` yaml:"geTui" `
Fcm struct {
Fcm struct {
ServiceAccount string ` yaml:"serviceAccount" `
ServiceAccount string ` yaml:"serviceAccount" `
Enable bool ` yaml:"enable" `
} ` yaml:"fcm" `
}
Jpns struct {
AppKey string ` yaml:"appKey" `
MasterSecret string ` yaml:"masterSecret" `
PushUrl string ` yaml:"pushUrl" `
PushIntent string ` yaml:"pushIntent" `
} ` yaml:"jpns" `
}
}
Manager struct {
Manager struct {
AppManagerUid [ ] string ` yaml:"appManagerUid" `
UserID [ ] string ` yaml:"userID "`
Nickname [ ] string ` yaml:"nickname" `
Nickname [ ] string ` yaml:"nickname" `
}
} ` yaml:"manager" `
Kafka struct {
MultiLoginPolicy int ` yaml:"multiLoginPolicy" `
SASLUserName string ` yaml:"SASLUserName" `
ChatPersistenceMysql bool ` yaml:"chatPersistenceMysql" `
SASLPassword string ` yaml:"SASLPassword" `
Ws2mschat struct {
Addr [ ] string ` yaml:"addr" `
Topic string ` yaml:"topic" `
}
MsgToMongo struct {
Addr [ ] string ` yaml:"addr" `
Topic string ` yaml:"topic" `
}
Ms2pschat struct {
Addr [ ] string ` yaml:"addr" `
Topic string ` yaml:"topic" `
}
MsgToModify struct {
Addr [ ] string ` yaml:"addr" `
Topic string ` yaml:"topic" `
}
ConsumerGroupID struct {
MsgToRedis string ` yaml:"msgToTransfer" `
MsgToMongo string ` yaml:"msgToMongo" `
MsgToMySql string ` yaml:"msgToMySql" `
MsgToPush string ` yaml:"msgToPush" `
MsgToModify string ` yaml:"msgToModify" `
}
}
Secret string ` yaml:"secret" `
MultiLoginPolicy int ` yaml:"multiloginpolicy" `
ChatPersistenceMysql bool ` yaml:"chatpersistencemysql" `
MsgCacheTimeout int ` yaml:"msgCacheTimeout" `
MsgCacheTimeout int ` yaml:"msgCacheTimeout" `
GroupMessageHasReadReceiptEnable bool ` yaml:"groupMessageHasReadReceiptEnable" `
GroupMessageHasReadReceiptEnable bool ` yaml:"groupMessageHasReadReceiptEnable" `
SingleMessageHasReadReceiptEnable bool ` yaml:"singleMessageHasReadReceiptEnable" `
SingleMessageHasReadReceiptEnable bool ` yaml:"singleMessageHasReadReceiptEnable" `
RetainChatRecords int ` yaml:"retainChatRecords" `
ChatRecordsClearTime string ` yaml:"chatRecordsClearTime" `
TokenPolicy struct {
TokenPolicy struct {
AccessSecret string ` yaml:"accessSecret" `
AccessSecret string ` yaml:"accessSecret" `
AccessExpire int64 ` yaml:"accessExpire" `
AccessExpire int64 ` yaml:"accessExpire" `
}
} ` yaml:"tokenPolicy" `
MessageVerify struct {
MessageVerify struct {
FriendVerify * bool ` yaml:"friendVerify" `
FriendVerify * bool ` yaml:"friendVerify" `
}
} ` yaml:"messageVerify" `
IOSPush struct {
IOSPush struct {
PushSound string ` yaml:"pushSound" `
PushSound string ` yaml:"pushSound" `
BadgeCount bool ` yaml:"badgeCount" `
BadgeCount bool ` yaml:"badgeCount" `
Production bool ` yaml:"production" `
Production bool ` yaml:"production" `
}
} ` yaml:"iosPush" `
Callback struct {
Callback struct {
CallbackUrl string ` yaml:" callbackU rl"`
CallbackUrl string ` yaml:"url" `
CallbackBeforeSendSingleMsg CallBackConfig ` yaml:" call backB eforeSendSingleMsg"`
CallbackBeforeSendSingleMsg CallBackConfig ` yaml:"beforeSendSingleMsg" `
CallbackAfterSendSingleMsg CallBackConfig ` yaml:" c allbackA fterSendSingleMsg"`
CallbackAfterSendSingleMsg CallBackConfig ` yaml:"afterSendSingleMsg" `
CallbackBeforeSendGroupMsg CallBackConfig ` yaml:" call backB eforeSendGroupMsg"`
CallbackBeforeSendGroupMsg CallBackConfig ` yaml:"beforeSendGroupMsg" `
CallbackAfterSendGroupMsg CallBackConfig ` yaml:" c allbackA fterSendGroupMsg"`
CallbackAfterSendGroupMsg CallBackConfig ` yaml:"afterSendGroupMsg" `
CallbackMsgModify CallBackConfig ` yaml:" callbackM sgModify"`
CallbackMsgModify CallBackConfig ` yaml:"msgModify" `
CallbackUserOnline CallBackConfig ` yaml:" callbackU serOnline"`
CallbackUserOnline CallBackConfig ` yaml:"userOnline" `
CallbackUserOffline CallBackConfig ` yaml:" callbackU serOffline"`
CallbackUserOffline CallBackConfig ` yaml:"userOffline" `
CallbackUserKickOff CallBackConfig ` yaml:" callbackU serKickOff"`
CallbackUserKickOff CallBackConfig ` yaml:"userKickOff" `
CallbackOfflinePush CallBackConfig ` yaml:" callbackO fflinePush"`
CallbackOfflinePush CallBackConfig ` yaml:" o fflinePush"`
CallbackOnlinePush CallBackConfig ` yaml:" callbackO nlinePush"`
CallbackOnlinePush CallBackConfig ` yaml:" o nlinePush"`
CallbackBeforeSuperGroupOnlinePush CallBackConfig ` yaml:" callbackS uperGroupOnlinePush"`
CallbackBeforeSuperGroupOnlinePush CallBackConfig ` yaml:" s uperGroupOnlinePush"`
CallbackBeforeAddFriend CallBackConfig ` yaml:" call backB eforeAddFriend"`
CallbackBeforeAddFriend CallBackConfig ` yaml:" beforeAddFriend"`
CallbackBeforeCreateGroup CallBackConfig ` yaml:" call backB eforeCreateGroup"`
CallbackBeforeCreateGroup CallBackConfig ` yaml:" beforeCreateGroup"`
CallbackBeforeMemberJoinGroup CallBackConfig ` yaml:" call backB eforeMemberJoinGroup"`
CallbackBeforeMemberJoinGroup CallBackConfig ` yaml:" beforeMemberJoinGroup"`
CallbackBeforeSetGroupMemberInfo CallBackConfig ` yaml:" call backB eforeSetGroupMemberInfo"`
CallbackBeforeSetGroupMemberInfo CallBackConfig ` yaml:" beforeSetGroupMemberInfo"`
} ` yaml:"callback" `
} ` yaml:"callback" `
Notification Notification ` yaml:"notification" `
Prometheus struct {
Prometheus struct {
Enable bool ` yaml:"enable" `
Enable bool ` yaml:"enable" `
@ -308,9 +261,10 @@ type config struct {
MessageTransferPrometheusPort [ ] int ` yaml:"messageTransferPrometheusPort" `
MessageTransferPrometheusPort [ ] int ` yaml:"messageTransferPrometheusPort" `
ThirdPrometheusPort [ ] int ` yaml:"thirdPrometheusPort" `
ThirdPrometheusPort [ ] int ` yaml:"thirdPrometheusPort" `
} ` yaml:"prometheus" `
} ` yaml:"prometheus" `
Notification notification ` yaml:"notification" `
}
}
type N otification struct {
type n otification struct {
GroupCreated NotificationConf ` yaml:"groupCreated" `
GroupCreated NotificationConf ` yaml:"groupCreated" `
GroupInfoSet NotificationConf ` yaml:"groupInfoSet" `
GroupInfoSet NotificationConf ` yaml:"groupInfoSet" `
JoinGroupApplication NotificationConf ` yaml:"joinGroupApplication" `
JoinGroupApplication NotificationConf ` yaml:"joinGroupApplication" `
@ -347,82 +301,3 @@ type Notification struct {
ConversationChanged NotificationConf ` yaml:"conversationChanged" `
ConversationChanged NotificationConf ` yaml:"conversationChanged" `
ConversationSetPrivate NotificationConf ` yaml:"conversationSetPrivate" `
ConversationSetPrivate NotificationConf ` yaml:"conversationSetPrivate" `
}
}
func GetServiceNames ( ) [ ] string {
return [ ] string { Config . RpcRegisterName . OpenImUserName , Config . RpcRegisterName . OpenImFriendName , Config . RpcRegisterName . OpenImMsgName ,
Config . RpcRegisterName . OpenImPushName , Config . RpcRegisterName . OpenImMessageGatewayName , Config . RpcRegisterName . OpenImGroupName ,
Config . RpcRegisterName . OpenImAuthName , Config . RpcRegisterName . OpenImConversationName , Config . RpcRegisterName . OpenImRtcName ,
Config . RpcRegisterName . OpenImThirdName }
}
func GetOptionsByNotification ( cfg 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 ( c * config ) unmarshalConfig ( config interface { } , configPath string ) error {
bytes , err := ioutil . ReadFile ( configPath )
if err != nil {
return err
}
if err = yaml . Unmarshal ( bytes , config ) ; err != nil {
return err
}
return nil
}
func ( c * config ) 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 )
}
return c . unmarshalConfig ( config , configPath )
}
func ( c * config ) RegisterConf2Registry ( registry discoveryregistry . SvcDiscoveryRegistry ) error {
bytes , err := yaml . Marshal ( Config )
if err != nil {
return err
}
return registry . RegisterConf2Registry ( ConfKey , bytes )
}
func ( c * config ) GetConfFromRegistry ( registry discoveryregistry . SvcDiscoveryRegistry ) ( [ ] byte , error ) {
return registry . GetConfFromRegistry ( ConfKey )
}
func InitConfig ( configFolderPath string ) error {
err := Config . initConfig ( & Config , FileName , configFolderPath )
if err != nil {
return err
}
err = Config . initConfig ( & Config . Notification , NotificationFileName , configFolderPath )
if err != nil {
return err
}
return nil
}