From 254f4ca70f0cd6b95b4005f6e4378c1466635496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ovidiu=20=C8=98tefan=20Popescu?= Date: Thu, 15 Feb 2024 21:33:05 +0200 Subject: [PATCH] Created a more generic way to read configuration files --- backend/src/config/config.go | 51 ++++++----------------------- backend/src/config/file_reader.go | 53 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 backend/src/config/file_reader.go diff --git a/backend/src/config/config.go b/backend/src/config/config.go index 9b9cd64..0c21254 100644 --- a/backend/src/config/config.go +++ b/backend/src/config/config.go @@ -1,8 +1,6 @@ package config import ( - "errors" - "github.com/spf13/viper" "log" "os" "time" @@ -65,57 +63,26 @@ type CorsConfig struct { } func GetConfig() *Config { - cfgPath := getConfigPath(os.Getenv("APP_ENV")) - configReader, err := LoadConfig(cfgPath, "yml") - if err != nil { - log.Fatalf("Error while loading config: %v", err) + fileReader := FileReader[Config]{} + configFilePath := getConfigPath(os.Getenv("APP_ENV")) + + config := fileReader.GetContent(configFilePath) + if config == nil { + return nil } - config, err := ParseConfig(configReader) - envPort := os.Getenv("PORT") - if envPort != "" { - config.Server.ExternalPort = envPort + environmentPort := os.Getenv("PORT") + if environmentPort != "" { + config.Server.ExternalPort = environmentPort log.Printf("Set external port from environment -> %s", config.Server.ExternalPort) } else { config.Server.ExternalPort = config.Server.InternalPort log.Printf("Set external port from environment -> %s", config.Server.ExternalPort) } - if err != nil { - log.Fatalf("Error while parsing config: %v", err) - } return config } -func ParseConfig(configReader *viper.Viper) (*Config, error) { - var config Config - err := configReader.Unmarshal(&config) - if err != nil { - log.Printf("Unable to parse config: %v", err) - return nil, err - } - return &config, nil -} - -func LoadConfig(filename string, fileType string) (*viper.Viper, error) { - configReader := viper.New() - configReader.SetConfigType(fileType) - configReader.SetConfigName(filename) - configReader.AddConfigPath(".") - configReader.AutomaticEnv() - - err := configReader.ReadInConfig() - if err != nil { - log.Printf("Unable to read config: %v", err) - var configFileNotFoundError viper.ConfigFileNotFoundError - if errors.As(err, &configFileNotFoundError) { - return nil, errors.New("could not find configuration file") - } - return nil, err - } - return configReader, nil -} - func getConfigPath(env string) string { if env == "docker" { return "/app/config/docker-config" diff --git a/backend/src/config/file_reader.go b/backend/src/config/file_reader.go new file mode 100644 index 0000000..4fc500a --- /dev/null +++ b/backend/src/config/file_reader.go @@ -0,0 +1,53 @@ +package config + +import ( + "errors" + "github.com/spf13/viper" + "log" +) + +type FileReader[T any] struct { +} + +func (reader *FileReader[T]) GetContent(filePath string) *T { + fileReader, err := reader.LoadFileContent(filePath, "yml") + if err != nil { + log.Fatalf("Error while loading file content: %v", err) + } + + fileContent, err := reader.ParseFileContent(fileReader) + if err != nil { + log.Fatalf("Error while parsing file content: %v", err) + } + + return fileContent +} + +func (reader *FileReader[T]) ParseFileContent(fileReader *viper.Viper) (*T, error) { + var fileContent T + err := fileReader.Unmarshal(&fileContent) + if err != nil { + log.Printf("Unable to parse file: %v", err) + return nil, err + } + return &fileContent, nil +} + +func (reader *FileReader[T]) LoadFileContent(fileName string, fileType string) (*viper.Viper, error) { + fileReader := viper.New() + fileReader.SetConfigType(fileType) + fileReader.SetConfigName(fileName) + fileReader.AddConfigPath(".") + fileReader.AutomaticEnv() + + err := fileReader.ReadInConfig() + if err != nil { + log.Printf("Unable to read file content: %v", err) + var fileNotFoundError viper.ConfigFileNotFoundError + if errors.As(err, &fileNotFoundError) { + return nil, errors.New("could not find file") + } + return nil, err + } + return fileReader, nil +}