
ConfigClient
Config client is a go package that allows go developers to use spring-boot like config client to load configs exposed via config servers.
Installation
go get github.com/oodinga/goconfig
Usage
The following env variables are required when using configClient
app.name
app.config.profiles.active
app.config.server.url
app.config.optional
These can be set in two ways.
- Set environment variables where you are running your application
- Add .env file in the root of your go service
Setting env variables
Follow instructions for setting ENVIROMENT_VARIABLES for your OS.
Example
For linux/MacOs
export app.name="example-service"
export app.config.profiles.active="dev"
export app.config.server.url="https://localhost:8080"
export app.config.optional="true"
Using .env file
Config client variables can also be loaded from .env file. This is an extension of godotenv
In your .env file set the following values.
app.name="example-service"
app.config.profiles.active="dev"
app.config.server.url="https://localhost:8080"
app.config.optional="true"
NOTE: Set values according to your application setting on your config server.
Once these variable are set, all you need to do is to import config client as shown.
package main
import (
"log"
"os"
config "github.com/oodinga/goconfig"
)
func main(){
config.Load()
log.Print("Service port: ", os.GetEnv("server.port"))
}
This can be simplified by importing the autoload package as shown below.
import (
"log"
"os"
_ "github.com/oodinga/goconfig/autoload"
)
func main(){
log.Print("Service port: ", os.GetEnv("server.port"))
}
ConfigClient will autoload the configs from the set config server and set them as environment variables to be used by your application.
Example config file
When using sprin-boot config server, You can use a db or git as the source of the configuration. A sample yaml file can be set as below.
application.yaml
server:
port: 8080
db:
username: name
password: ******
url: localhost:3600
To use these configs
Using this in your go app.
package main
import (
"log"
"os"
_ "github.com/oodinga/goconfig/autoload"
)
func main(){
log.Print("Service port: ", os.GetEnv("server.port"))
log.Print("Database name: ", os.GetEnv("db.name"))
log.Print("Database url: ", os.GetEnv("db.url"))
}
This should log your values to the terminal
Refer to spring documentation on how to set a spring config server.