go-config

go-config
is a module which a YAML configuration file from a "remote" source. It supports multiple storage backends through storage and go-storage.
The intended usage is to load the configuration from a YAML file, for a web application written in Go. The file could be local or remote (from any source supported by go-storage).
Installation
go get github.com/rgglez/go-config
Configuration
c := config.NewConfigurator(&config.Config{
Referrer: "",
Stage: "",
File: "config.yaml",
}, s)
The configuration config.Config
struct taken as the first parameter has this properties:
Referrer
string, optional, the referrer as provided by the web framework you are using.
Stage
string, an optional prefix for the path of the configuration. For example: "dev" or "production".
File
string, required, the file name of the configuration file.
TmpDir
string, optional path for a temporary directory where the remote file will be downloaded into a local temporary file.
The second parameter, s
in the example, must be a storage object:
s := storage.NewStorage(cnn)
Where cnn
is a valid connection string as specified by go-storage.
For example, for a bucket named test
in an ossemulator server running at localhost
, the connection string would be:
cnn := "oss://test/?credential=hmac:Secret123:Secret123&endpoint=http://127.0.0.1:9090&name=test"
The configuration file path is formed by these components:
domain + "/" + stage + "/" + file
where the domain
is obtained from the Referrer
.
Since domain
and stage
are optional, the file could be in the root of the remote path. For instance, if the source is a S3 bucket, the key could:
example.com/config.yaml
or
prod/config.yaml
or even
config.yaml
Usage
The configuration file could be loaded into a struct (which should reflect the structure of your YAML file) or a map. For example:
var cfgMap map[string]interface{}
err := c.Load(&cfgMap)
var cfgStruct Configuracion
err = c.Load(&cfgStruct)
See the sample code.
Dependencies
This module uses:
and their respective dependencies.
License
Copyright 2024 Rodolfo González González.
Released under Apache 2.0 license.