init
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user