config

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: MIT Imports: 7 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-config

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

Installation

go get github.com/neighborly/go-config

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 . 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"
	"github.com/neighborly/go-config"
)

// 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() {
	err := config.Load(packr.NewBox("."), &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
⌂ 换网址