bbs-backend/config/config.go

38 lines
649 B
Go
Raw Normal View History

2024-11-19 21:08:28 +08:00
// file name: config.go
package config
import (
"github.com/spf13/viper"
"log"
)
type Config struct {
Database struct {
Type string `mapstructure:"type"`
File string `mapstructure:"file"`
} `mapstructure:"database"`
Server struct {
Port int `mapstructure:"port"`
} `mapstructure:"server"`
}
var cfg *Config
func LoadConfig(path string) {
viper.SetConfigFile(path)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
cfg = &Config{}
err = viper.Unmarshal(cfg)
if err != nil {
log.Fatalf("Failed to unmarshal config file: %v", err)
}
}
func GetConfig() *Config {
return cfg
}