38 lines
649 B
Go
38 lines
649 B
Go
|
// 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
|
||
|
}
|