config

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 1 Imported by: 0

README

Configuration Package

Overview

The config package provides a flexible configuration system that supports multiple sources and formats for Go applications. It allows you to manage application configuration in a hierarchical manner with support for various data sources.

Features

  • Multiple Sources:

    • YAML and JSON files
    • Environment variables
    • Command-line flags
    • In-memory values
  • Hierarchical Configuration: Access nested values using dot notation

  • Default Values: Specify fallback values when configuration is missing

  • Type Conversion: Automatic conversion to the appropriate type

  • Configuration Reloading: Watch for changes and reload configuration

  • Validation: Validate configuration against schemas

  • Adapters: Easily create custom adapters for different configuration sources

Installation

go get github.com/abitofhelp/servicelib/config

Quick Start

See the Basic Usage example for a complete, runnable example of how to use the config package.

Configuration

See the Configuration example for a complete, runnable example of how to configure the config package.

API Documentation

Core Types
Config

The Config interface provides methods for accessing configuration values.

See the Basic Usage example for a complete, runnable example of how to use the Config interface.

Options

The Options struct provides configuration options for creating a new config instance.

See the Custom Adapter example for a complete, runnable example of how to use the Options struct.

Key Methods
New

The New function creates a new configuration instance from the specified files.

cfg, err := config.New("config.yaml", "env.yaml")

See the Basic Usage example for a complete, runnable example.

NewWithOptions

The NewWithOptions function creates a new configuration instance with the specified options.

cfg, err := config.NewWithOptions(config.Options{
    Files: []string{"config.yaml"},
    EnvPrefix: "APP_",
    EnvReplacer: "_",
})

See the Custom Adapter example for a complete, runnable example.

Get Methods

The Get* methods retrieve configuration values of different types.

// Get a string value
apiKey := cfg.GetString("api.key")

// Get an int value with default
port := cfg.GetInt("server.port", 8080)

// Get a boolean value
debug := cfg.GetBool("logging.debug", false)

// Get a duration value
timeout := cfg.GetDuration("server.timeout", "30s")

See the Basic Usage example for a complete, runnable example.

Unmarshal

The Unmarshal method binds configuration values to a struct.

var appConfig AppConfig
if err := cfg.Unmarshal(&appConfig); err != nil {
    log.Fatalf("Failed to unmarshal configuration: %v", err)
}

See the App Config example for a complete, runnable example.

Watch

The Watch method watches for configuration changes and calls the specified function when changes are detected.

cfg.Watch(func() {
    // Reload configuration when changes are detected
})

See the Basic Usage example for a complete, runnable example.

Examples

For complete, runnable examples, see the following files in the examples directory:

Best Practices

  1. Use Environment Variables for Secrets: Never store sensitive information like API keys or passwords in configuration files. Use environment variables instead.

  2. Default Values: Always provide sensible default values for configuration that might be missing.

  3. Validation: Validate configuration values to ensure they meet your requirements.

  4. Configuration Hierarchy: Use a hierarchical approach to organize your configuration.

  5. Documentation: Document all configuration options, including their purpose, type, and default values.

Troubleshooting

Common Issues
Missing Configuration Files

Issue: Configuration files are not found at the specified path.

Solution: Ensure that the configuration files exist at the specified path. Use absolute paths or paths relative to the working directory.

Type Conversion Errors

Issue: Type conversion fails when retrieving configuration values.

Solution: Ensure that the configuration values are of the expected type. Use the appropriate Get* method for the value type.

Environment Variables Not Applied

Issue: Environment variables are not being applied to the configuration.

Solution: Ensure that the environment variables are properly formatted with the correct prefix and separator. Check that the EnvPrefix and EnvReplacer options are set correctly.

  • Logging - The logging component uses the config package for configuration.
  • Database - The database component uses the config package for database configuration.
  • Telemetry - The telemetry component uses the config package for telemetry configuration.

Contributing

Contributions to this component are welcome! Please see the Contributing Guide for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package config provides generic configuration interfaces and adapters.

Package config provides generic configuration interfaces that can be used across different applications.

Package config provides generic configuration interfaces that can be used across different applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig = interfaces.AppConfig

AppConfig is an alias for interfaces.AppConfig for backward compatibility

type AppConfigProvider

type AppConfigProvider interface {
	// GetAppVersion returns the application version
	GetAppVersion() string

	// GetAppName returns the application name (optional)
	GetAppName() string

	// GetAppEnvironment returns the application environment (optional)
	GetAppEnvironment() string
}

AppConfigProvider defines the interface for accessing application configuration

type Config

type Config = interfaces.Config

Config is an alias for interfaces.Config for backward compatibility

type ConfigInterface

type ConfigInterface interface {
	// GetApp returns the App configuration
	GetApp() interface{}

	// GetDatabase returns the Database configuration
	GetDatabase() interface{}
}

ConfigInterface is an interface for configuration Deprecated: Use Config interface instead

type DatabaseConfig

type DatabaseConfig = interfaces.DatabaseConfig

DatabaseConfig is an alias for interfaces.DatabaseConfig for backward compatibility

type DatabaseConfigProvider

type DatabaseConfigProvider interface {
	// GetDatabaseType returns the database type
	GetDatabaseType() string

	// GetDatabaseConnectionString returns the database connection string
	GetDatabaseConnectionString(dbType string) string
}

DatabaseConfigProvider defines the interface for accessing database configuration

type GenericAppConfigAdapter

type GenericAppConfigAdapter[T any] struct {
	// contains filtered or unexported fields
}

GenericAppConfigAdapter is a generic adapter for application configuration

func (*GenericAppConfigAdapter[T]) GetEnvironment

func (a *GenericAppConfigAdapter[T]) GetEnvironment() string

GetEnvironment returns the application environment

func (*GenericAppConfigAdapter[T]) GetName

func (a *GenericAppConfigAdapter[T]) GetName() string

GetName returns the application name

func (*GenericAppConfigAdapter[T]) GetVersion

func (a *GenericAppConfigAdapter[T]) GetVersion() string

GetVersion returns the application version

type GenericConfigAdapter

type GenericConfigAdapter[T any] struct {
	// contains filtered or unexported fields
}

GenericConfigAdapter is a generic adapter for any config type that provides the necessary methods

func NewGenericConfigAdapter

func NewGenericConfigAdapter[T any](cfg T) *GenericConfigAdapter[T]

NewGenericConfigAdapter creates a new GenericConfigAdapter with default values

func (*GenericConfigAdapter[T]) GetApp

func (a *GenericConfigAdapter[T]) GetApp() AppConfig

GetApp returns the application configuration

func (*GenericConfigAdapter[T]) GetDatabase

func (a *GenericConfigAdapter[T]) GetDatabase() DatabaseConfig

GetDatabase returns the database configuration

func (*GenericConfigAdapter[T]) WithAppEnvironment

func (a *GenericConfigAdapter[T]) WithAppEnvironment(env string) *GenericConfigAdapter[T]

WithAppEnvironment sets the application environment

func (*GenericConfigAdapter[T]) WithAppName

func (a *GenericConfigAdapter[T]) WithAppName(name string) *GenericConfigAdapter[T]

WithAppName sets the application name

func (*GenericConfigAdapter[T]) WithDatabaseName

func (a *GenericConfigAdapter[T]) WithDatabaseName(name string) *GenericConfigAdapter[T]

WithDatabaseName sets the database name

type GenericDatabaseConfigAdapter

type GenericDatabaseConfigAdapter[T any] struct {
	// contains filtered or unexported fields
}

GenericDatabaseConfigAdapter is a generic adapter for database configuration

func (*GenericDatabaseConfigAdapter[T]) GetCollectionName

func (a *GenericDatabaseConfigAdapter[T]) GetCollectionName(entityType string) string

GetCollectionName returns the collection/table name for a given entity type

func (*GenericDatabaseConfigAdapter[T]) GetConnectionString

func (a *GenericDatabaseConfigAdapter[T]) GetConnectionString() string

GetConnectionString returns the database connection string

func (*GenericDatabaseConfigAdapter[T]) GetDatabaseName

func (a *GenericDatabaseConfigAdapter[T]) GetDatabaseName() string

GetDatabaseName returns the database name

func (*GenericDatabaseConfigAdapter[T]) GetType

func (a *GenericDatabaseConfigAdapter[T]) GetType() string

GetType returns the database type

Directories

Path Synopsis
Package interfaces provides generic configuration interfaces that can be used across different applications.
Package interfaces provides generic configuration interfaces that can be used across different applications.
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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