Skip to content

haijianyang/gconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gconfig

Go Application Configuration. Simple and practical configuration management.

Inspired by node-config and viper, Gconfig supports some features of both: parse different configuration files and environment variable according to ENV. Currently only supports JSON file。

  • Support both configuration files and environment variables
  • Support multiple environments:development、staging、production etc
  • Support multiple configuration instances

Install

go get github.com/haijianyang/gconfig

Quick Start

type Config struct {
}

var config Config

err := gconfig.Unmarshal(&config)
if err != nil {
	fmt.Println(err)
}

fmt.Println(config)

Document

Setting Overrides

gconfig.SetDefault(key, value) // Set default configuration

gconfig.SetDefault("Env", "development") // Set Go running environment
gconfig.SetDefault("Folder", "./config") // Set the configuration file folder
gconfig.SetDefault("FileType", ".json") // Set the type of configuration file
gconfig.SetDefault("DefaultFile", "default") // Set default configuration file name

Working with Environment Variables

Gconfig uses ENV and GO_ENV environment variables to select configuration files by default. For example: ENV=test will use test.json。

Environment variables take precedence over file variables, and the value of environment variables will override the value of file variables.

Gconfig parses environment variables according to the env tag in struct.

type EnvConfig struct {
	token bool `json:"token" env:"TOKEN"`
}

Best Practices

Create a config folder in the project root directory, and put the configuration file and config.go in the config folder.

- project
  - config
    - config.go
    - default.json
    - development.json
    - production.json
    - staging.json
    - test.json

default.json

{
  "server": {
    "port": 8000
  }
}

test.json

{
  "server": {
    "port": 8000
  }
}

config.go

package config

import (
	"fmt"
	"github.com/haijianyang/gconfig"
)

type ServerConfig struct {
	Port int `json:"port"`
}

type Config struct {
	Server ServerConfig
}

var Kvs Config

func init() {
	err := gconfig.Unmarshal(&Kvs)
	if err != nil {
		fmt.Println(err)
	}
}

code.go

import (
	"fmt"

	"github.com/haijianyang/project/config"
)

fmt.Println(config.Kvs.Server)

About

Golang Application Configuration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages