Skip to content

Commit

Permalink
Created a more generic way to read configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Feb 15, 2024
1 parent 00303ae commit 254f4ca
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 42 deletions.
51 changes: 9 additions & 42 deletions backend/src/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"errors"
"github.com/spf13/viper"
"log"
"os"
"time"
Expand Down Expand Up @@ -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"
Expand Down
53 changes: 53 additions & 0 deletions backend/src/config/file_reader.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 254f4ca

Please sign in to comment.