Golang Viper: The Unsung Hero of Configuration Wizards


Written by - Tuan Nguyen
Reviewed by - Deepak Prasad

Introduction to Golang Viper

Golang Viper is a comprehensive library used for managing application configurations in Golang projects. It plays a critical role in handling various configuration needs, such as reading from environment variables, configuration files, and remote config systems, allowing for a more dynamic and flexible configuration management process. This makes Viper an excellent tool for developers who want to maintain clean and organized configuration code in their Golang applications.

Overview of Features

Golang Viper comes packed with a multitude of features making it a powerful library for configuration management. Some of its standout features include the ability to:

  • Read configuration data from various sources like files (JSON, YAML, TOML, etc.) and environment variables.
  • Automatically reload configurations when changes are detected.
  • Work harmoniously with remote configuration systems like etcd or Consul.
  • Support for setting defaults, aliases, and the ability to merge configurations from multiple sources.

Comparison with Other Configuration Management Libraries

When comparing Golang Viper with other configuration management libraries, Viper tends to stand out due to its versatility and rich feature set. Unlike some libraries, Viper is not just limited to reading files or environment variables; it provides a unified interface for managing configurations, making the developer’s job more straightforward. Its ability to seamlessly integrate with remote configuration systems and auto-reload configurations makes Golang Viper a robust and reliable choice for modern-day application development.

 

Reading Configuration Files

In this section, we will dive into how to use Golang Viper for reading configuration files. Viper supports various file formats, such as JSON, TOML, and YAML, providing developers with the flexibility to choose a format that best suits their needs. Below are examples and explanations to guide you through this process.

Golang Viper can effortlessly work with various configuration file formats. Let’s look at examples in JSON, TOML, and YAML formats:

1. JSON Example (config.json):

{
    "hostname": "localhost",
    "port": 8080,
    "credentials": {
        "username": "admin",
        "password": "secret"
    }
}

Here is the main.go to load this JSON configuration file:

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
)

func initializeViper() {
	viper.SetConfigName("config")         // name of config file (without extension)
	viper.SetConfigType("json")           // REQUIRED if the config file does not have the extension
	viper.AddConfigPath(".")              // path to look for the config file in
	err := viper.ReadInConfig()           // Find and read the config file

	if err != nil {                       // Handle errors reading the config file
		log.Fatalf("Error while reading config file %s", err)
	}
}

func main() {
	initializeViper()

	hostname := viper.GetString("hostname")
	port := viper.GetInt("port")
	username := viper.GetString("credentials.username")
	password := viper.GetString("credentials.password")

	fmt.Printf("Hostname: %s\n", hostname)
	fmt.Printf("Port: %d\n", port)
	fmt.Printf("Username: %s\n", username)
	fmt.Printf("Password: %s\n", password)
}

Output:

Hostname: localhost
Port: 8080
Username: admin
Password: secret

2. TOML Example (config.toml):

[database]
server = "localhost"
ports = [ 8001, 8002 ]
connection_max = 5000
enabled = true

Here is the main.go to initialize the config.toml file:

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
)

func initializeViper() {
	viper.SetConfigName("config") // name of config file (without extension)
	viper.SetConfigType("toml")   // Set the type of the configuration file
	viper.AddConfigPath(".")      // path to look for the config file
	err := viper.ReadInConfig()

	if err != nil { // Handle errors reading the config file
		log.Fatalf("Error while reading config file %s", err)
	}
}

func main() {
	initializeViper()

	// Getting values from the configuration file
	dbServer := viper.GetString("database.server")
	dbPorts := viper.GetIntSlice("database.ports")
	dbConnMax := viper.GetInt("database.connection_max")
	dbEnabled := viper.GetBool("database.enabled")

	// Printing the values
	fmt.Printf("DB Server: %s\n", dbServer)
	fmt.Printf("DB Ports: %v\n", dbPorts)
	fmt.Printf("DB Connection Max: %v\n", dbConnMax)
	fmt.Printf("DB Enabled: %v\n", dbEnabled)
}

3. YAML Example (config.yaml):

hostname: localhost
port: 8080
credentials:
    username: admin
    password: secret

Here is the main.go to initialize the config.yaml file:

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
)

func initializeViper() {
	// Set the file name of the configurations file
	viper.SetConfigName("config") // name of config file (without extension)

	// Set the type of the configuration file
	viper.SetConfigType("yaml")

	// Set the path to look for the configurations file
	viper.AddConfigPath(".") // path to look for the config file in

	// Find and read the config file
	err := viper.ReadInConfig()

	if err != nil { // Handle errors reading the config file
		log.Fatalf("Error while reading config file %s", err)
	}
}

func main() {
	initializeViper()

	// Getting values from the configuration file
	hostname := viper.GetString("hostname")
	port := viper.GetInt("port")
	username := viper.GetString("credentials.username")
	password := viper.GetString("credentials.password")

	// Printing the values
	fmt.Printf("Hostname: %s\n", hostname)
	fmt.Printf("Port: %d\n", port)
	fmt.Printf("Username: %s\n", username)
	fmt.Printf("Password: %s\n", password)
}

 

Writing Configuration Files

In this section, we will explore how to create, update, and save configuration files using Golang Viper. The ability to manage configuration files programmatically is vital for dynamic and adaptable applications.

Creating and Updating Configuration Files

Firstly, let’s look at how you can create and update configuration files with Golang Viper. We will start by defining some configurations in our application and then write them to a file.

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
)

func main() {
	// Setting up some configurations
	viper.Set("name", "Gopher")
	viper.Set("language", "Go")
	viper.Set("website", "golang.org")

	// Creating and updating a configuration file
	viper.SetConfigName("app_config") // name of config file (without extension)
	viper.SetConfigType("yaml")       // specifying the config type
	viper.AddConfigPath(".")          // path to look for the config file in

	err := viper.SafeWriteConfig()
	if err != nil {
		if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok {
			err = viper.WriteConfig()
			if err != nil {
				log.Fatalf("Error while updating config file %s", err)
			}
		} else {
			log.Fatalf("Error while creating config file %s", err)
		}
	}

	fmt.Println("Configuration file created/updated successfully!")
}

In this example:

  • We set up configurations like name, language, and website.
  • We then attempt to write these configurations to a file named app_config.yaml.
  • If the file already exists, it gets updated; otherwise, a new file is created.

Saving Configurations

Golang Viper automatically handles saving configurations once WriteConfig or SafeWriteConfig methods are invoked, as shown in the previous example.

  • viper.WriteConfig(): Overwrites the existing config file.
  • viper.SafeWriteConfig(): Writes the config file only if it does not already exist.

In the example provided, the configurations get saved to a file named app_config.yaml in the YAML format. You can specify a different file format (like JSON or TOML) by changing the argument passed to viper.SetConfigType().

 

Working with Environment Variables

Environment variables are crucial for configuring applications in various environments. Golang Viper simplifies the process of working with environment variables by providing functionalities to read, set, and prioritize them over configuration files. Let’s delve into how you can effectively manage environment variables using Golang Viper.

 

Reading Environment Variables

Golang Viper allows you to read environment variables directly, and it can also synchronize them with your configuration. Below is a demonstration of how to read environment variables using Golang Viper.

package main

import (
	"fmt"

	"github.com/spf13/viper"
)

func main() {
	// Automatically read environment variables
	viper.AutomaticEnv()

	// Accessing an environment variable
	value := viper.GetString("HOME")
	fmt.Printf("HOME = %s\n", value)
}

In this example, viper.AutomaticEnv() is used to instruct Viper to automatically read environment variables, and then an environment variable (HOME) is accessed using viper.GetString().

 

Setting Environment Variables

Setting environment variables using Golang Viper involves binding an environment variable to a Viper key. Here is how to do it:

package main

import (
	"fmt"
	"os"

	"github.com/spf13/viper"
)

func main() {
	// Setting an environment variable
	os.Setenv("APP_DEBUG", "true")

	// Binding the environment variable to a Viper key
	viper.BindEnv("debug", "APP_DEBUG")

	// Accessing the value of the environment variable through Viper
	debug := viper.GetBool("debug")
	fmt.Printf("Debug Mode = %v\n", debug)
}

Here, the environment variable APP_DEBUG is bound to the Viper key debug, allowing it to be accessed using Viper methods.

 

Priority of Environment Variables over Configuration Files

Golang Viper gives higher priority to environment variables over configuration files. If an environment variable and a configuration file key collide, the environment variable’s value will be used.

package main

import (
	"fmt"
	"os"

	"github.com/spf13/viper"
)

func initializeViper() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	viper.ReadInConfig()
}

func main() {
	initializeViper()

	// Setting an environment variable
	os.Setenv("APP_NAME", "ViperAppEnv")

	// Binding the environment variable to a Viper key
	viper.BindEnv("app.name", "APP_NAME")

	// Accessing the value through Viper
	appName := viper.GetString("app.name")
	fmt.Printf("App Name = %s\n", appName)
}

In this example, even if app.name is defined in the configuration file, the environment variable APP_NAME value will be used when accessing app.name through Viper.

 

Setting and Getting Values

Golang Viper facilitates the setting of default values and the retrieval of values of various data types, enhancing configuration management's flexibility and robustness. Here’s a detailed guide on how to set and get values using Golang Viper.

Defining Default Values

Defining default values is a proactive way to ensure that your application has a value to fall back on if a configuration value is not found. Here’s how you can set default values:

package main

import (
	"fmt"

	"github.com/spf13/viper"
)

func main() {
	// Setting default values
	viper.SetDefault("app.name", "MyApp")
	viper.SetDefault("app.version", "1.0.0")

	// Getting the values
	appName := viper.GetString("app.name")
	appVersion := viper.GetString("app.version")

	fmt.Printf("App Name: %s\n", appName)
	fmt.Printf("App Version: %s\n", appVersion)
}

In this example, default values are set for the keys app.name and app.version. These values will be used if no other value is provided in the environment variables or configuration files.

Retrieving Values of Different Data Types

Golang Viper allows you to retrieve values as various data types like strings, integers, booleans, etc. Here’s how you can retrieve values of different data types:

package main

import (
	"fmt"

	"github.com/spf13/viper"
)

func main() {
	// Setting values
	viper.Set("app.description", "A demo application")
	viper.Set("app.rating", 4.5)
	viper.Set("app.is_published", true)

	// Getting values as different data types
	description := viper.GetString("app.description")
	rating := viper.GetFloat64("app.rating")
	isPublished := viper.GetBool("app.is_published")

	fmt.Printf("Description: %s\n", description)
	fmt.Printf("Rating: %.2f\n", rating)
	fmt.Printf("Is Published: %v\n", isPublished)
}

In this example, values are retrieved as strings, floats, and booleans. Golang Viper provides specific methods like GetString, GetFloat64, and GetBool to facilitate this.

 

Managing Configuration in Different Environments

Managing configurations across different environments such as development, testing, and production is a common requirement in software development. Golang Viper aids in streamlining this process by providing functionalities to set up and switch between different configurations seamlessly. Below is a detailed guide on how to manage configurations across various environments using Golang Viper.

Setting up Configurations for Development, Testing, and Production

You can set up separate configuration files for different environments and instruct Viper to use a specific file based on the current environment. Here’s how you can do it:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/spf13/viper"
)

func initializeViper(env string) {
	viper.SetConfigName(env)        // name of config file (without extension)
	viper.SetConfigType("yaml")     // REQUIRED if the config file does not have the extension
	viper.AddConfigPath(".")        // path to look for the config file in
	err := viper.ReadInConfig()     // Find and read the config file

	if err != nil { // Handle errors reading the config file
		log.Fatalf("Error while reading config file %s", err)
	}
}

func main() {
	env := os.Getenv("APP_ENV")
	if env == "" {
		env = "development"
	}

	initializeViper(env)

	fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
	fmt.Printf("DB Host: %s\n", viper.GetString("db.host"))
}

In this example, based on the APP_ENV environment variable, different configuration files (e.g., development.yaml, testing.yaml, production.yaml) are loaded.

Switching Between Different Configurations

Switching between configurations is as simple as changing the environment variable value. You can set the APP_ENV variable to development, testing, or production to load the respective configurations.

export APP_ENV=development
go run main.go
export APP_ENV=production
go run main.go

By changing the APP_ENV value and running the application, different configurations will be used, allowing for easy switching between various environments.

 

Aliases and Merging Configurations

Golang Viper offers flexibility in configuration management through features like aliases and configuration merging. Aliases allow for multiple keys to refer to the same value, and merging configurations enable combining multiple sources into a unified configuration. Below is a guide on how to use these features effectively with Golang Viper.

Creating Aliases for Keys

Creating aliases can make accessing configuration values more flexible. Here is how you can create aliases for configuration keys using Golang Viper:

package main

import (
	"fmt"

	"github.com/spf13/viper"
)

func main() {
	// Setting a value
	viper.Set("app.name", "ViperDemo")

	// Creating an alias
	viper.RegisterAlias("application_name", "app.name")

	// Getting value using the alias
	appName := viper.GetString("application_name")
	fmt.Printf("App Name: %s\n", appName)
}

In this example, an alias application_name is created for the key app.name, allowing the value to be accessed using either the original key or the alias.

Merging Multiple Configuration Sources

Merging configurations involves combining values from various sources, ensuring a composite configuration. Here’s an example of merging configurations from two different sources:

package main

import (
	"log"

	"github.com/spf13/viper"
)

func loadConfig(name string) {
	viper.SetConfigName(name)
	viper.AddConfigPath(".")
	if err := viper.MergeInConfig(); err != nil {
		log.Fatalf("Error merging config file: %s", err)
	}
}

func main() {
	// Loading the first configuration
	loadConfig("config1")

	// Merging with the second configuration
	loadConfig("config2")

	// Accessing a merged value
	fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
	fmt.Printf("App Version: %s\n", viper.GetString("app.version"))
}

In this code snippet, two configuration files (config1.yaml and config2.yaml) are merged. If there are overlapping keys, values from the latter configuration will override the former.

 

Remote Configurations

Golang Viper facilitates working with remote configuration systems such as etcd or Consul, enabling dynamic and real-time configuration management. This functionality allows applications to be more adaptable and responsive to configuration changes. Below are detailed examples illustrating how to work with remote configurations using Golang Viper.

 

Working with Remote Configuration Systems like etcd or Consul

Connecting and accessing configurations from a remote system is simplified with Viper. Here’s an example of how you could work with configurations stored in etcd:

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
	_ "github.com/spf13/viper/remote" // This blank import enables the remote features
)

func main() {
	// Establish a connection to the remote configuration system (etcd)
	err := viper.AddRemoteProvider("etcd", "http://127.0.0.1:2379", "/config/hello.json")
	if err != nil {
		log.Fatalf("Error connecting to remote provider: %s", err)
	}

	viper.SetConfigType("json") // because there is no file extension in a stream of bytes, you must explicitly set it

	err = viper.ReadRemoteConfig()
	if err != nil {
		log.Fatalf("Error reading remote config: %s", err)
	}

	// Accessing a value from the remote configuration
	message := viper.GetString("message")
	fmt.Printf("Message: %s\n", message)
}

In this example, Viper connects to an etcd instance and reads a JSON configuration. The configuration is then accessed as usual using Viper’s getter methods. This just gives you a template and you can modify it as per your environment.

 

Watching Changes in Real-Time

Viper enables watching configuration changes in real-time, which can be quite useful for dynamic configuration adaptations. Here’s how to implement it:

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/spf13/viper"
	_ "github.com/spf13/viper/remote"
)

func main() {
	err := viper.AddRemoteProvider("etcd", "http://127.0.0.1:2379", "/config/hello.json")
	if err != nil {
		log.Fatalf("Error connecting to remote provider: %s", err)
	}

	viper.SetConfigType("json")
	err = viper.ReadRemoteConfig()
	if err != nil {
		log.Fatalf("Error reading remote config: %s", err)
	}

	// Watching for changes in real-time
	err = viper.WatchRemoteConfig()
	if err != nil {
		log.Fatalf("Error watching remote config: %s", err)
	}

	for {
		time.Sleep(time.Second * 5)

		// Accessing the updated value
		message := viper.GetString("message")
		fmt.Printf("Updated Message: %s\n", message)
	}
}

In this case, Viper watches for changes in the remote configuration, and the application continuously accesses the updated values, allowing for real-time configuration updates.

 

Encryption and Security with Golang Viper

When dealing with configurations, ensuring the security of sensitive data is paramount. Golang Viper, while not providing built-in encryption support, can work alongside encryption libraries to safeguard your configuration data. Below are guidelines and examples that illustrate best practices for securing sensitive data and using encryption in your configurations.

Best Practices for Securing Sensitive Data

  • Environment Variables: Store sensitive data such as API keys or database passwords in environment variables, rather than hardcoding them in configuration files.
  • File Permissions: Ensure that your configuration files have strict file permissions, limiting who can read or modify them.

While Viper itself doesn’t provide encryption mechanisms, you can use external libraries such as crypto/aes alongside Viper to encrypt and decrypt sensitive configuration data. Here is an example of how you could do this:

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
)

// Encrypt text using AES
func Encrypt(text string, key []byte) (string, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}

	b := base64.StdEncoding.EncodeToString([]byte(text))
	ciphertext := make([]byte, aes.BlockSize+len(b))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		return "", err
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))

	return base64.StdEncoding.EncodeToString(ciphertext), nil
}

// Decrypt text using AES
func Decrypt(text string, key []byte) (string, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}

	ciphertext, _ := base64.StdEncoding.DecodeString(text)
	if len(ciphertext) < aes.BlockSize {
		return "", err
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(ciphertext, ciphertext)

	data, _ := base64.StdEncoding.DecodeString(string(ciphertext))
	return string(data), nil
}

func main() {
	key := []byte("a very very very very secret key") // 32 bytes
	encrypted, err := Encrypt("sensitive_data", key)
	if err != nil {
		fmt.Println(err)
		return
	}

	decrypted, err := Decrypt(encrypted, key)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Encrypted: %s\n", encrypted)
	fmt.Printf("Decrypted: %s\n", decrypted)
}

In this example, the Encrypt and Decrypt functions utilize the AES algorithm from Go’s crypto/aes package to perform encryption and decryption respectively. AES (Advanced Encryption Standard) is utilized for encrypting sensitive data using a symmetric key. The key should be securely managed, avoiding hard-coding. Instead, leverage environment variables or secret management services for enhanced security. Note that you should choose a secure key and manage it securely.

 

Conclusion

Throughout this guide, we've delved into various facets of managing configurations using Golang Viper. From the basics such as reading, writing, and managing configuration files and environment variables, to advanced features such as working with remote configurations and ensuring encryption and security, we've covered a comprehensive range of topics to equip you with the knowledge to effectively utilize Viper in your Golang projects.

Key Takeaways:

  • Flexibility: Viper offers a flexible approach to configuration management, supporting various file formats and allowing the combination of multiple configuration sources.
  • Real-Time Updates: With features like watching remote configurations, Viper facilitates dynamic and real-time configuration updates.
  • Security: While Viper doesn’t offer built-in encryption, it can be paired with encryption libraries to ensure the security of sensitive configuration data.

Additional Resources:

  • Viper GitHub Repository: For a deeper dive into its functionalities and to stay updated with the latest features and improvements, refer to the official Viper GitHub repository .
  • Official Documentation: The official documentation is a comprehensive resource to explore the full spectrum of capabilities that Viper offers.

 

Tuan Nguyen

He is proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms. You can connect with him on LinkedIn.

Categories GO

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment