config

package module
v0.0.0-...-e59bd1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

go-config

License GitHub all releases GitHub issues GitHub commit activity Go Report Card GitHub release GitHub stars GitHub forks

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Referrer string
	Stage    string
	File     string
	TmpDir   string
}

type Configurator

type Configurator struct {
	Storage    *storage.Storage
	ConfigFile string
	TmpDir     string
}

func NewConfigurator

func NewConfigurator(cfg *Config, store *storage.Storage) *Configurator

func (*Configurator) Load

func (c *Configurator) Load(config interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL