2025-02-08 18:57:05 +08:00
package config
import (
"github.com/spf13/viper"
2025-02-10 17:30:29 +08:00
_ "github.com/spf13/viper/remote"
2025-02-08 18:57:05 +08:00
"github.com/zeromicro/go-zero/zrpc"
)
const (
TypeKey = "CONFIG_TYPE" // 配置文件类型
TypeEtcd = "etcd"
TypeEnv = "env"
EtcdPrefix = "/youtu/"
EtcdAddrKey = "ETCD_ADDR"
)
type Redis struct {
Host string ` json:"host" `
Password string ` json:"password" `
}
type AppAccount struct {
AppId string ` json:"appId" `
Secret string ` json:"secret" `
Type string ` json:"type" `
}
type Config struct {
RpcServerConf zrpc . RpcServerConf
Mode string
Mysql string
Redis [ ] Redis
}
func init ( ) {
2025-02-10 17:30:29 +08:00
viper . SetDefault ( EtcdAddrKey , "192.168.0.47:2379" )
2025-02-08 18:57:05 +08:00
viper . SetDefault ( TypeKey , TypeEtcd )
}
func GetConfig ( c * Config , serverName string ) ( err error ) {
err = viper . AddRemoteProvider ( "etcd3" , viper . GetString ( EtcdAddrKey ) , EtcdPrefix + serverName + ".rpc" )
if err != nil {
return
}
viper . SetConfigType ( "json" ) // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
err = viper . ReadRemoteConfig ( )
if err != nil {
return
}
2025-02-11 16:54:00 +08:00
err = viper . Unmarshal ( & c )
if err != nil {
return
}
c . RpcServerConf . Name = serverName + ".rpc"
c . RpcServerConf . ServiceConf . Mode = c . Mode
return
2025-02-08 18:57:05 +08:00
}