45 lines
662 B
Go
45 lines
662 B
Go
package config
|
|
|
|
import (
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
)
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
}
|
|
type MySQLConfig struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
Database string
|
|
}
|
|
type LogConfig struct {
|
|
Level string
|
|
Format string
|
|
Output string
|
|
}
|
|
|
|
var Conf struct {
|
|
Server ServerConfig
|
|
MySQL MySQLConfig
|
|
Log LogConfig
|
|
}
|
|
|
|
func InitConfig() {
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
log.Fatalf("读取配置失败: %v", err)
|
|
}
|
|
|
|
err = viper.Unmarshal(&Conf)
|
|
if err != nil {
|
|
log.Fatalf("解析配置失败: %v", err)
|
|
}
|
|
}
|