Skip to content

ribeirohugo/go_config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Config

Go Config is a simple package that allows to load basic data for a Golang application. It supports toml, json, yaml or xml file or content to instantiate environment variables.

1. Config

Before to run application you may setup a supportedconfig.toml with Config values. Check out the following tables to know all Config parameters detailed.

Parameter Description Type Default Required
environment Website environment. string YES
service Website service identifier as string. string YES
[server] Http server config data. Server YES
[token] Token data config data. Token YES
[mongodb] Postgres database config data. Database YES
[mysql] MySql database config data. Database YES
[postgres] Postgres database config data. Database YES
[audit] Auditing options config data. Audit YES
[tracer] Tracing options config data. Tracer YES

1.1. Server type

Parameter Description Type Default Required
host HTTP server host name. string YES
port HTTP server port number. int YES
allowed_origins Allowed hosts to CORS allowance. []string NO

1.2. Database type

To set up [mysql] and [postgres] use the following parameters:

Parameter Description Type Default Required
db Database name. string YES
host Database host. string YES
migrations_path Migrations directory path. string file://migrations/<mongo><mysql><postgres> NO
password Database password. string YES
port Database port. int YES
user Database user with needed privileges over database. string YES

1.3. Token type

Parameter Description Type Default Required
secret Website token secret string. string YES
max_age Maximum duration of a token (in seconds). int 86400 NO

1.4. Audit type

Parameter Description Type Default Required
Enable Enable flag to activate auditing. bool FALSE NO
Host Auditing server host address. string NO

1.5. Tracer type

Parameter Description Type Default Required
Enable Enable flag to activate tracing. bool FALSE NO
JaegerHost ** Deprecated ** Jaeger host address. string http://localhost:14268/api/traces NO
Host Tracer host address. string NO

2. Load data

First you need to get dependency go_config dependency by calling go get, with the wanted release.

go get github.com/ribeirohugo/go_config@latest

2.1. Toml

Then, data can be loaded by calling Load method. It supports a config.toml file properly fulfilled.

cfg, err := toml.Load(configFile)
if err != nil {
    log.Fatal(err)
}

It will return a config.Config struct variable or an error, if anything unexpected occurs.

2.2. YAML

Then, data can be loaded by calling Load method, with a config.yaml file properly fulfilled.

cfg, err := yaml.Load(configFile)
if err != nil {
    log.Fatal(err)
}

It will return a config.Config struct variable or an error, if anything unexpected occurs.

2.3. JSON

Then, data can be loaded by calling Load method, with a config.json file properly fulfilled.

cfg, err := json.Load(configFile)
if err != nil {
    log.Fatal(err)
}

It will return a config.Config struct variable or an error, if anything unexpected occurs.

2.4. XML

Then, data can be loaded by calling Load method, with a config.json file properly fulfilled.

cfg, err := xml.Load(configFile)
if err != nil {
    log.Fatal(err)
}

It will return a config.Config struct variable or an error, if anything unexpected occurs.