config

package
v0.0.0-...-73ebcb8 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 6 Imported by: 0

README

Config Package Documentation

The config package is responsible for managing application configuration. It provides a centralized approach to loading, validating, and accessing configuration values from embedded files, environment variables, and defaults.


Overview

This package uses:

  • Viper for flexible configuration management.
  • Go Validator to ensure all required fields are populated and valid.
  • An embedded default configuration file (default.ini) as a baseline, which can be overridden by environment variables.

Features

  1. Centralized Configuration:

    • Loads default values from an embedded default.ini file.
    • Overrides defaults with values from environment variables.
  2. Validation:

    • Ensures critical configuration fields are present and valid using validator.
  3. Flexibility:

    • Uses the ini format for defaults but allows customization via environment variables.
    • Environment variables are automatically mapped by replacing . with _.

Environment Variable Overrides

Environment variables can override default.ini values.

  • Format: Replace . with _ in variable names.
  • Example:
    • app.nameAPP_NAME
    • database.passwordDATABASE_PASSWORD

Best Practices

  1. Keep Secrets Secure:
    Avoid committing sensitive values (e.g., passwords, secrets) to version control. Use environment variables for sensitive data.

  2. Validate Configuration Early:
    Ensure that configuration validation is performed during application startup to catch issues early.

  3. Use Environment Variables for Deployment:
    Rely on environment variables to override default values in different environments (e.g., production, staging).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig struct {
	Name        string `mapstructure:"name" validate:"required"`
	Environment string `mapstructure:"environment" validate:"required"`
	Version     string `mapstructure:"version" validate:"required"`
}

type Config

type Config struct {
	App      AppConfig      `mapstructure:"app"`
	Http     HTTPConfig     `mapstructure:"http"`
	Database DatabaseConfig `mapstructure:"database" validate:"required"`
}

func New

func New() (*Config, error)

type DatabaseConfig

type DatabaseConfig struct {
	Host            string `mapstructure:"host" validate:"required"`
	Port            string `mapstructure:"port" validate:"required"`
	DBName          string `mapstructure:"dbname" validate:"required"`
	Password        string `mapstructure:"password" validate:"required"`
	User            string `mapstructure:"user" validate:"required"`
	SSLModeDisabled bool   `mapstructure:"sslmode_disabled"`
}

type HTTPConfig

type HTTPConfig struct {
	Host         string        `mapstructure:"host"`
	Port         string        `mapstructure:"port" validate:"required"`
	ReadTimeout  time.Duration `mapstructure:"read_timeout"`
	WriteTimeout time.Duration `mapstructure:"write_timeout"`
	IdleTimeout  time.Duration `mapstructure:"idle_timeout"`
	Cors         struct {
		Origins string `mapstructure:"origins"`
		Methods string `mapstructure:"methods"`
		Headers string `mapstructure:"headers"`
	} `mapstructure:"cors"`
}

Jump to

Keyboard shortcuts

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