XinYeYouKu/config/config.go

152 lines
3.8 KiB
Go
Raw Normal View History

2024-06-07 18:24:56 +08:00
package config
import (
"github.com/BurntSushi/toml"
"github.com/qit-team/snow-core/config"
2024-06-11 14:57:08 +08:00
"os"
2024-06-07 18:24:56 +08:00
)
const (
ProdEnv = "production" //线上环境
BetaEnv = "beta" //beta环境
DevEnv = "develop" //开发环境
LocalEnv = "local" //本地环境
)
var srvConf *Config
// ------------------------配置文件解析
type Config struct {
ServiceName string `toml:"ServiceName"`
Env string `toml:"Env"`
Debug bool `toml:"Debug"`
PrometheusCollectEnable bool `toml:"PrometheusCollectEnable"`
SkyWalkingOapServer string `toml:"SkyWalkingOapServer"`
Log config.LogConfig `toml:"Log"`
Redis config.RedisConfig `toml:"Redis"`
Mns config.MnsConfig `toml:"AliMns"`
Db config.DbConfig `toml:"Db"`
Api config.ApiConfig `toml:"Api"`
Admin config.ApiConfig `toml:"Admin"`
Nacos Nacos `toml:"Nacas"`
Rpc Rpc `toml:"Rpc"`
AppKey string `toml:"AppKey"`
2024-06-11 14:57:08 +08:00
KafkaUrl []string `toml:"KafkaUrl"`
AliSms AliSms `toml:"AliSms"`
AdminGate string `toml:"AdminGate"`
PluginDir string `toml:"PLUGIN_DIR"`
ImgDir string `toml:"IMG_DIR"`
OtherDir string `toml:"OTHER_DIR"`
AliOss AliOss `toml:"AliOss"`
TemplateUrl string `toml:"TemplateUrl"`
Message Message `toml:"Message"`
OpenApi OpenApi `toml:"OpenApi"`
OpenApiMarketConfig MarketConfig `toml:"MarketConfig"`
RocketMq []string `toml:"RocketMq"`
Sm2 Sm2 `toml:"Sm2"`
}
type Sm2 struct {
PublicKey string
PrivateKey string
}
type OpenApi struct {
MerchantId string
SecretKey string
IsProd bool
NotifyUrl string
TimeOut int
}
type Message struct {
ClientKey string
ClientSecret string
ServerIndex string
TempIndex string
Url string
}
type AliOss struct {
AccessKey string
AccessKeySecret string
EndPoint string
BucKet string
Domain string
Dir string
}
type AliSms struct {
Url string
AccessKeyId string
AccessKeySecret string
SignName string
TemplateSmsCode string
Nums int
2024-06-07 18:24:56 +08:00
}
type Rpc struct {
2024-06-11 14:57:08 +08:00
Port string
Servers []string
2024-06-07 18:24:56 +08:00
}
type Nacos struct {
2024-06-11 14:57:08 +08:00
Url string
Port int64
ServiceName string
GroupName string
ClusterName string
ServicePort int64
}
type MarketConfig struct {
AppId string `json:"app_id"` //APP ID
Sign string `json:"sign"` //签名
ReqCode string `json:"req_code"` //固定值voucher.create
MemId string `json:"mem_id"` //商户号
PosId string `json:"pos_id"` //商户方平台号
Host string `json:"-"`
SecretKey string
2024-06-07 18:24:56 +08:00
}
func newConfig() *Config {
return new(Config)
}
// ------------------------ 加载配置 ------------------------//
func Load(path string) (*Config, error) {
_, err := os.Stat(path)
if err != nil {
return nil, err
}
conf := newConfig()
if _, err := toml.DecodeFile(path, conf); err != nil {
return nil, err
}
srvConf = conf
return conf, nil
}
// 当前配置
func GetConf() *Config {
return srvConf
}
// 是否调试模式
func IsDebug() bool {
return srvConf.Debug
}
// 当前环境,默认本地开发
func GetEnv() string {
if srvConf.Env == "" {
return LocalEnv
}
return srvConf.Env
}
// 是否当前环境
func IsEnvEqual(env string) bool {
return GetEnv() == env
}