Skip to content

Commit

Permalink
chore(config): make multi threaded safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Dec 10, 2024
1 parent 94ba0ab commit b8f4639
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ package config
import (
"fmt"
"strings"
"sync"

"github.com/joho/godotenv"
"github.com/spf13/viper"
)

// FIXME: Add mutex for map writes

var mu sync.Mutex

func bindEnv(key string) {
envName := strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
// nolint:errcheck // we do not care if it can get binded

mu.Lock()
defer mu.Unlock()

viper.BindEnv(key, envName)
}

Expand All @@ -20,11 +28,15 @@ func Init() error {
if err := godotenv.Load(); err != nil {
return err
}

mu.Lock()
defer mu.Unlock()

viper.AutomaticEnv()
env := GetDefaultString("app.env", "development")

viper.SetConfigName(fmt.Sprintf("%s.toml", env))
viper.SetConfigType("toml")
viper.SetConfigName(fmt.Sprintf("%s.yaml", strings.ToLower(env)))
viper.SetConfigType("yaml")
viper.AddConfigPath("./config")

return viper.ReadInConfig()
Expand All @@ -33,59 +45,94 @@ func Init() error {
// GetString returns the value of the key in string
func GetString(key string) string {
bindEnv(key)

mu.Lock()
defer mu.Unlock()

return viper.GetString(key)
}

// GetDefaultString returns the value of the key in string or a default value
func GetDefaultString(key, defaultValue string) string {
mu.Lock()
defer mu.Unlock()

viper.SetDefault(key, defaultValue)
return GetString(key)
}

// GetStringSlice returns the value of the key in string slice
func GetStringSlice(key string) []string {
bindEnv(key)

mu.Lock()
defer mu.Unlock()

return viper.GetStringSlice(key)
}

// GetDefaultStringSlice returns the value of the key in string slice or a default value
func GetDefaultStringSlice(key string, defaultValue []string) []string {
mu.Lock()
defer mu.Unlock()

viper.SetDefault(key, defaultValue)
return GetStringSlice(key)
}

// GetInt returns the value of the key in int
func GetInt(key string) int {
bindEnv(key)

mu.Lock()
defer mu.Unlock()

return viper.GetInt(key)
}

// GetDefaultInt returns the value of the key in int or a default value
func GetDefaultInt(key string, defaultVal int) int {
mu.Lock()
defer mu.Unlock()

viper.SetDefault(key, defaultVal)
return GetInt(key)
}

// GetUint16 returns the value of the key in uint16
func GetUint16(key string) uint16 {
bindEnv(key)

mu.Lock()
defer mu.Unlock()

return viper.GetUint16(key)
}

// GetDefaultUint16 returns the value of the key in uint16 or a default value
func GetDefaultUint16(key string, defaultVal uint16) uint16 {
mu.Lock()
defer mu.Unlock()

viper.SetDefault(key, defaultVal)
return GetUint16(key)
}

// GetBool returns the value of the key in bool
func GetBool(key string) bool {
bindEnv(key)

mu.Lock()
defer mu.Unlock()

return viper.GetBool(key)
}

// GetDefaultBool returns the value of the key in bool or a default value
func GetDefaultBool(key string, defaultVal bool) bool {
mu.Lock()
defer mu.Unlock()

viper.SetDefault(key, defaultVal)
return GetBool(key)
}

0 comments on commit b8f4639

Please sign in to comment.