12
This commit is contained in:
44
internal/config/config.go
Normal file
44
internal/config/config.go
Normal file
@ -0,0 +1,44 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user