This commit is contained in:
2026-04-24 16:50:46 +08:00
commit 6d1611d08c
8 changed files with 435 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
package config
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
Database struct {
User string
Password string
Host string
Port int
Dbname string
Charset string
}
Server struct {
Port int
}
}
var AppConfig *Config
func InitConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".") // 在当前目录查找 config.yaml
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("无法读取配置文件: %w", err))
}
if err := viper.Unmarshal(&AppConfig); err != nil {
panic(fmt.Errorf("无法解析配置结构体: %w", err))
}
}