config

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: MIT Imports: 10 Imported by: 0

README

go-config

This package loads config information into a struct. It uses viper.

Installation

go get github.com/nrfta/go-config/v2

Usage

To load the config data into a struct you will need two parameters

  1. a variable of type box: To get the box type you will need to import github.com/gobuffalo/packr/v2 . The box variable will hold the config data in the binary
  2. a variable of type "customStruct" where customStruct is a struct you define to mirror the key values of your config
Example

Consider this example config content

// config/config.json
{
  "Meta": {
    "environment": "development",
    "service_name": "my-app"
  },
  "port": 6002,
  "bugsnag_api_key": "",
  "segment_write_key": "",
  "log_level": "error"
}

In the file you want to load the config in do the following:

// config/config.go

import (
	"github.com/gobuffalo/packr/v2"
	"github.com/nrfta/go-config/v2"
)

// this MyAppConfig struct is the "custom struct" it has the same attributes that mirror the config json above
type MyAppConfig struct {
	Meta config.MetaConfig

	Port            int    `mapstructure:"port"`
	BugsnagAPIKey   string `mapstructure:"bugsnag_api_key"`
	LogLevel        string `mapstructure:"log_level"`
	PrometheusPort  int    `mapstructure:"prometheus_port"`
	SegmentWriteKey string `mapstructure:"segment_write_key"`
}

// the definition of our two paramaters
var (
	Config MyAppConfig
)

func init() {
	box := packr.New("config", ".")
	err := config.Load(box, &Config) // now the config data has been loaded into appConfig
	if err != nil {
		panic(err)
	}
}

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(box *packr.Box, config interface{}) error

Load config from file then from environment variables

Types

type MetaConfig

type MetaConfig struct {
	Environment string
	ServiceName string `mapstructure:"service_name"`
}

MetaConfig holds configuration for environment name and service name

Jump to

Keyboard shortcuts

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