From 33db3abdadd6687eb16305014c70654e03168fe5 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sun, 17 Mar 2024 13:13:01 +0300 Subject: init --- config/config.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 config/config.go (limited to 'config') 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) + } +} -- cgit v1.2.3