feat(config): 添加配置文件

This commit is contained in:
2024-08-22 10:51:23 +08:00
parent 7a898920a8
commit 167c2c873e
5 changed files with 106 additions and 9 deletions

30
config/config.go Normal file
View File

@ -0,0 +1,30 @@
package config
import "github.com/spf13/viper"
type Config struct {
Server ServerConfig `mapstructure:"server"`
}
type ServerConfig struct {
Port int `mapstructure:"port"`
Host string `mapstructure:"host"`
}
var AppConfig Config
func LoadConfig() error {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
return err
}
if err := viper.Unmarshal(&AppConfig); err != nil {
return err
}
return nil
}