xy_sh-20260727101126/xy_sh/internal/config/config.go

58 lines
1.4 KiB
Go

package config
import (
"fmt"
"github.com/spf13/viper"
"time"
)
type Config struct {
ServerPort string `mapstructure:"server_port"`
SM3Salt string `mapstructure:"s_m3_salt"`
SM4Key string `mapstructure:"sm4_key"`
NotifyUrl string `mapstructure:"notify_url"`
Ymt Ymt `mapstructure:"ymt"`
DB DB `mapstructure:"db"`
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type Ymt struct {
AppID string `mapstructure:"app_id"`
PrivateKey string `mapstructure:"private_key"`
PublicKey string `mapstructure:"public_key"`
Key string `mapstructure:"key"`
ActivityNo string `mapstructure:"activity_no"`
EncryptType string `mapstructure:"encrypt_type"`
Env string `mapstructure:"env"`
}
type DB struct {
Driver string `mapstructure:"driver"`
Source string `mapstructure:"source"`
MaxIdle int32 `mapstructure:"maxIdle"`
MaxOpen int32 `mapstructure:"maxOpen"`
MaxLifetime int32 `mapstructure:"maxLifetime"`
IsDebug bool `mapstructure:"isDebug"`
}
func LoadConfig(configPath string) (*Config, error) {
viper.SetConfigFile(configPath)
viper.SetConfigType("yaml")
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
// 解析配置
var bc Config
if err := viper.Unmarshal(&bc); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
return &bc, nil
}