diff options
Diffstat (limited to 'config/config.go')
-rw-r--r-- | config/config.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..135df95 --- /dev/null +++ b/config/config.go @@ -0,0 +1,43 @@ +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"` +} + +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) + } +} |