99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"mylomen_server/common/utils"
|
|
|
|
_ "embed"
|
|
)
|
|
|
|
// FbConsul consul配置
|
|
type FbConsul struct {
|
|
Address string `mapstructure:"address" json:"address" yaml:"address"`
|
|
Datacenter string `mapstructure:"datacenter" json:"datacenter" yaml:"datacenter"`
|
|
Token string `mapstructure:"token" json:"token" yaml:"token"`
|
|
}
|
|
|
|
// Rpc rpc配置
|
|
type Rpc struct {
|
|
ApiGolangAddress string `mapstructure:"api-golang" json:"api-golang" yaml:"api-golang"`
|
|
WssAddress string `mapstructure:"wss" json:"wss" yaml:"wss"`
|
|
}
|
|
|
|
// PgSql mysql配置
|
|
type PgSql struct {
|
|
LogSql bool `mapstructure:"logSql" json:"logSql" yaml:"logSql"`
|
|
Dsn string `mapstructure:"dsn" json:"dsn" yaml:"dsn"`
|
|
MaxIdleConn int `mapstructure:"maxIdleConn" json:"maxIdleConn" yaml:"maxIdleConn"`
|
|
MaxOpenConn int `mapstructure:"maxOpenConn" json:"maxOpenConn" yaml:"maxOpenConn"`
|
|
ConnMaxLifetime int `mapstructure:"connMaxLifetime" json:"connMaxLifetime" yaml:"connMaxLifetime"`
|
|
}
|
|
|
|
// Redis redis配置
|
|
type Redis struct {
|
|
DB int `mapstructure:"db" json:"db" yaml:"db"` // redis的哪个数据库
|
|
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务器地址:端口
|
|
Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
|
|
|
|
ReadTimeOut int64 `mapstructure:"readTimeout" json:"readTimeout" yaml:"readTimeout"`
|
|
WriteTimeout int64 `mapstructure:"writeTimeout" json:"writeTimeout" yaml:"writeTimeout"`
|
|
}
|
|
|
|
type MyServer struct {
|
|
AppName string `mapstructure:"appName" json:"appName" yaml:"appName"`
|
|
JwtSigningKey string `mapstructure:"jwtSigningKey" json:"jwtSigningKey" yaml:"jwtSigningKey"`
|
|
}
|
|
|
|
type Conf struct {
|
|
FbConsul *FbConsul `mapstructure:"fbConsul" json:"fbConsul" yaml:"fbConsul"` // fbConsul 配置
|
|
|
|
Rpc *Rpc `mapstructure:"rpc" json:"rpc" yaml:"rpc"` // rpc 配置
|
|
|
|
PgSql *PgSql `mapstructure:"pgSql" json:"pgSql" yaml:"pgSql"` // pgSql 配置
|
|
|
|
Redis *Redis `mapstructure:"redis" json:"redis" yaml:"redis"` // redis 配置
|
|
|
|
MyServer *MyServer `mapstructure:"myServer" json:"myServer" yaml:"myServer"` // myServer 配置
|
|
}
|
|
|
|
var Instance = initCf()
|
|
|
|
//go:embed *.yaml
|
|
var multiCf embed.FS
|
|
|
|
func initCf() *Conf {
|
|
//获取环境变量
|
|
env := utils.FillValOfKey("API_ENV", "dev")
|
|
|
|
//读取配置文件
|
|
data, err := multiCf.ReadFile("config." + env + ".yaml")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
//读取配置文件
|
|
v := viper.New()
|
|
v.SetConfigType("yaml")
|
|
err = v.ReadConfig(bytes.NewBuffer(data))
|
|
//err = v.ReadInConfig()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
//解析配置文件
|
|
var cf *Conf
|
|
err = v.Unmarshal(&cf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
//根据配置参数更新 consul token
|
|
cf.FbConsul.Token = utils.FillValOfKey("CONSUL_HTTP_TOKEN", cf.FbConsul.Token)
|
|
|
|
//fbl.Log().Sugar().Infof("start in %s", env)
|
|
return cf
|
|
}
|