package config import ( "log/slog" "os" "github.com/spf13/viper" ) type Config struct { ServerConfig ServerConfig `mapstructure:"SERVICE"` BaseURL string `mapstructure:"BASE_URL"` SessionLifetime int `mapstructure:"SESSION_LIFETIME_SECONDS"` DBURI string `mapstructure:"DBURI"` } type ServerConfig struct { Host string `mapstructure:"HOST"` Port string `mapstructure:"PORT"` } // LoadConfig Load server configuration from the yaml file func LoadConfig(viperConf *viper.Viper) Config { logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) var config Config err := viperConf.Unmarshal(&config) if err != nil { logger.Error("Unable to decode configuration file", "error", err) } return config } // OpenConfig open config file func OpenConfig() { logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) viper.SetConfigType("yml") viper.SetConfigName("config") viper.SetEnvPrefix("CFG") err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file logger.Error("Unable to read configuration file", "error", err) } }