config

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Config Package

Overview

The config package manages configurations in Go applications, ensuring compatibility between config.yaml and setup.env. setup.env is used to setup environment variables.

Note - When both environment variables and config.yaml settings are present, the package prioritizes environment variables, overriding equivalent settings in config.yaml.

Compatible configurations example

The below configurations are compatible in this project.

  • setup.env contains:

    export LOGFILE_PATH=log/terrarium.log
    
  • config.yaml has:

    logfile:
      path: ./log/mc-terrarium.log
    

How to use it

  • Use a blank import in your package (e.g., main, logger, and so on)
  • Get a value using Viper

Note - It's just my preference. config.Init() can be used.

import (
    // other packages

    "github.com/cloud-barista/mc-terrarium/pkg/config"
    "github.com/cloud-barista/mc-terrarium/pkg/logger"
)

func init(){
	// Initialize the configuration from "config.yaml" file or environment variables
	config.Init()

	// Initialize the logger
	logger := logger.NewLogger(logger.Config{
		LogLevel:    config.Terrarium.LogLevel,
		LogWriter:   config.Terrarium.LogWriter,
		LogFilePath: config.Terrarium.LogFile.Path,
		MaxSize:     config.Terrarium.LogFile.MaxSize,
		MaxBackups:  config.Terrarium.LogFile.MaxBackups,
		MaxAge:      config.Terrarium.LogFile.MaxAge,
		Compress:    config.Terrarium.LogFile.Compress,
	})

	// Set the global logger
	log.Logger = *logger
}

func main() {
    // Application logic follows
}

Wrapping Up

This setup illustrates the package's ability to harmonize settings from both setup.env and config.yaml, showcasing its versatility and ease of use.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RuntimeConfig Config
	Terrarium     TerrariumConfig
)

Functions

func Init

func Init()

func NVL added in v0.0.8

func NVL(str string, def string) string

NVL is func for null value logic

Types

type AllowConfig added in v0.0.8

type AllowConfig struct {
	Origins string `mapstructure:"origins"`
}

type ApiConfig added in v0.0.8

type ApiConfig struct {
	Allow    AllowConfig `mapstructure:"allow"`
	Auth     AuthConfig  `mapstructure:"auth"`
	Username string      `mapstructure:"username"`
	Password string      `mapstructure:"password"`
}

type AuthConfig added in v0.0.8

type AuthConfig struct {
	Enabled bool `mapstructure:"enabled"`
}

type AutoControlConfig added in v0.0.8

type AutoControlConfig struct {
	DurationMilliSec int `mapstructure:"duration_ms"`
}

type Config added in v0.0.8

type Config struct {
	Terrarium TerrariumConfig `mapstructure:"terrarium"`
}

type LogfileConfig added in v0.0.8

type LogfileConfig struct {
	Path       string `mapstructure:"path"`
	MaxSize    int    `mapstructure:"maxsize"`
	MaxBackups int    `mapstructure:"maxbackups"`
	MaxAge     int    `mapstructure:"maxage"`
	Compress   bool   `mapstructure:"compress"`
}

type NodeConfig added in v0.0.8

type NodeConfig struct {
	Env string `mapstructure:"env"`
}

type SelfConfig added in v0.0.8

type SelfConfig struct {
	Endpoint string `mapstructure:"endpoint"`
}

type TerrariumConfig added in v0.0.8

type TerrariumConfig struct {
	Root        string            `mapstructure:"root"`
	Self        SelfConfig        `mapstructure:"self"`
	API         ApiConfig         `mapstructure:"api"`
	LogFile     LogfileConfig     `mapstructure:"logfile"`
	LogLevel    string            `mapstructure:"loglevel"`
	LogWriter   string            `mapstructure:"logwriter"`
	Node        NodeConfig        `mapstructure:"node"`
	AutoControl AutoControlConfig `mapstructure:"autocontrol"`
	Tumblebug   TumblebugConfig   `mapstructure:"tumblebug"`
}

type TumblebugApiConfig added in v0.0.8

type TumblebugApiConfig struct {
	Username string `mapstructure:"username"`
	Password string `mapstructure:"password"`
}

type TumblebugConfig added in v0.0.8

type TumblebugConfig struct {
	Endpoint string             `mapstructure:"endpoint"`
	RestUrl  string             `mapstructure:"resturl"`
	API      TumblebugApiConfig `mapstructure:"api"`
}

Jump to

Keyboard shortcuts

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