Skip to content

Instantly share code, notes, and snippets.

@mehmetcemyucel
Created April 28, 2022 10:48
Show Gist options
  • Save mehmetcemyucel/6fc66883e4be37ebdcff5383b2bd106c to your computer and use it in GitHub Desktop.
Save mehmetcemyucel/6fc66883e4be37ebdcff5383b2bd106c to your computer and use it in GitHub Desktop.
package config
import (
"fmt"
"github.com/spf13/viper"
"helloWorld/pkg/util"
)
const (
defaultConfigPath = "config"
defaultConfigFileName = "application"
defaultConfigFileType = "yaml"
ymlTagName = "mapstructure"
)
type (
LogConfig struct {
KafkaLogConfig KafkaLogConfig `mapstructure:"kafka"`
FileLogConfig FileLogConfig `mapstructure:"file"`
}
KafkaLogConfig struct {
KafkaCluster string `mapstructure:"kafka-cluster"`
Topic string `mapstructure:"topic"`
}
FileLogConfig struct {
LogPath string `mapstructure:"log-path"`
}
PostgreConfig struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Name string `mapstructure:"name"`
TablePrefix string `mapstructure:"table-prefix"`
Ssl bool `mapstructure:"ssl"`
}
ServerConfig struct {
Port string `mapstructure:"port"`
Cors []string `mapstructure:"cors"`
}
ExampleConfig struct {
ExampleParam1 string `mapstructure:"example-param1"`
ExampleParam2 string `mapstructure:"example-param2"`
}
Config struct {
Postgre PostgreConfig `mapstructure:"postgre"`
Server ServerConfig `mapstructure:"app-server"`
Example ExampleConfig `mapstructure:"example"`
Log LogConfig `mapstructure:"log"`
}
)
func NewConfig() *Config {
return ReadConfig(&Config{}, util.ReadEnv())
}
var ReadConfig = func(c *Config, env string) *Config {
fmt.Println("Configuration read initiated...")
v := viper.New()
switch {
case env == "ENV":
v = readFromEnv(v)
case env == "REMOTE":
v = readFromConfigServer(v)
case env == "LOCAL":
v = readFromLocalAppYml(v)
default:
v = readFromLocalAppYml(v)
}
if err := v.Unmarshal(c); err != nil {
panic("Configuration unmarshalling occured an error, terminating" + err.Error())
}
return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment