config

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 10 Imported by: 1

README

Golang Microservice configuration module

CircleCI codecov

Configuration module for microservices written on Go. Preserves corporate standards for services configuration.

Installation

Import in your configuration file

package main

import (
	config "github.com/spacetab-io/configuration-go"
)

Usage

Some agreements:

  1. Configuration must be declared as struct and reveals yaml structure
  2. Default config folder: ./configuration. If you need to override, pass your path in ReadConfig function
  3. Stage is passed as stage.Interface implementation. In example below stageEnv is used to pass stage through env variable STAGE.

Code example:

package main

import (
	"fmt"
	"log"

	config "github.com/spacetab-io/configuration-go"
	"github.com/spacetab-io/configuration-go/stage"
	"gopkg.in/yaml.v2"
)

// ConfigStruct is your app config structure. This must be related to yaml config file structure. 
// Everything that is not in this struct will be passed and will not be processed.
// Keep in mind that inheritance must be implemented with `struct{}`
type ConfigStruct struct {
	Log struct {
		Level  string `yaml:"level"`
		Debug  bool   `yaml:"debug"`
		Format string `yaml:"yaml"`
	} `yaml:"log"`
	Database struct {
		Host           string `yaml:"host"`
		Port           string `yaml:"port"`
		User           string `yaml:"user"`
		Pass           string `yaml:"password"`
		Name           string `yaml:"name"`
		SslMode        string `yaml:"sslMode"`
		Logs           bool   `yaml:"logs"`
		MigrateOnStart bool   `yaml:"migrateOnStart"`
		MigrationPath  string `yaml:"migrationsPath"`
	} `yaml:"db"`
	Http struct {
		Host string `yaml:"host"`
		Port string `yaml:"port"`
	} `yaml:"ws"`
}

func main() {
	// config.Read receives stage as stage.Interface implementation.
	// You can use envStage to pass stage name via ENV param STAGE.
	// In NewEnvStage you can pass fallback value if STAGE param is empty.
	envStage := stage.NewEnvStage("development")
	// Reading ALL config files in defaults configuration folder and recursively merge them with STAGE configs
	configBytes, err := config.Read(envStage, "./configuration", true)
	if err != nil {
		log.Fatalf("config reading error: %+v", err)
	}

	cfg := ConfigStruct{}
	// unmarshal config into Config structure 
	err = yaml.Unmarshal(configBytes, &cfg)
	if err != nil {
		log.Fatalf("config unmarshal error: %+v", err)
	}

	fmt.Printf("config: %+v", cfg)
} 

License

The MIT License

Copyright © 2021 SpaceTab.io, Inc. https://spacetab.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDeprecatedMethod = errors.New("function ReadConfigs(cfgPath) was deprecated, use Read(stage, cfgPath, verbose) instead")
View Source
var ErrNoDefaults = errors.New("no default config")

Functions

func Read added in v1.2.0

func Read(stageI stage.Interface, cfgPath string, verbose bool) ([]byte, error)

Read Reads yaml files from configuration directory with sub folders as application stage and merges config files in one configuration per stage.

func ReadConfigs

func ReadConfigs(_ string) ([]byte, error)

ReadConfigs deprecated

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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