configManager
is a Go module that simplifies the management of configuration data from various sources such as environment variables, JSON, YAML, and .env
files. It provides a flexible way to handle complex configurations, including nested structs, validation, default values, and custom parsing. This module also caches configuration values in memory for efficient access.
- Multiple Sources: Supports
.env
, JSON, and YAML files. - Default Values: Automatically applies default values when environment variables are missing.
- Validation: Supports required fields and throws errors for missing variables.
- Nested Structs: Handles deeply nested structs with ease.
- Custom Parsing: Allows custom parsing (e.g., JSON strings).
- Environment-Specific Files: Supports app-specific configurations based on the
APP_ENV
variable (e.g.,.dev.env
,.prod.env
). - Caching: Caches frequently accessed configuration data to improve performance.
- Recursive Directory Search: Searches up to 3 levels of subdirectories to find configuration files(configs dir).
- Automatic Binding: Automatically binds configuration data to struct fields.
To install the module, use the following command:
go get github.com/syntaxLabz/configManager
package main
import (
"fmt"
"log"
"github.com/syntaxLabz/configManager"
)
func main() {
// Create a new config manager
cm := configManager.New()
// Define a struct to hold your configuration
var config AppConfig
// Unmarshal data from environment variables into the config struct
err := cm.Unmarshal(&config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Config Loaded: %+v\n", config)
}
type AppConfig struct {
DBUrl string `env:"DB_URL" default:"postgres://localhost:5432" required:"true"`
APIToken string `env:"API_TOKEN"`
ServerPort int `env:"SERVER_PORT" default:"8080"`
DebugMode bool `env:"DEBUG_MODE" default:"false"`
}
The Unmarshal
function automatically populates your struct fields based on environment variables or configuration file data.
err := cm.Unmarshal(&config)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
To optimize access to frequently used configuration data, the module provides in-memory caching. Data is stored in a small in-memory cache to avoid repeatedly accessing the OS or reading from files.
You can clear the in-memory cache if needed:
cm.ClearCache()
This module supports three main configuration file types:
-
.env
Files- Each line in the
.env
file contains a key-value pair. - Example:
DB_URL=postgres://localhost:5432 API_TOKEN=my-api-token SERVER_PORT=8080 DEBUG_MODE=true
- Each line in the
-
JSON Files
- Configuration data can be in standard JSON format.
- Example:
{ "DB_URL": "postgres://localhost:5432", "API_TOKEN": "my-api-token", "SERVER_PORT": 8080, "DEBUG_MODE": true }
-
YAML Files
- Configuration data in YAML format.
- Example:
DB_URL: postgres://localhost:5432 API_TOKEN: my-api-token SERVER_PORT: 8080 DEBUG_MODE: true
The configManager
module performs the following actions:
- It searches for configuration files (up to 3 levels deep) in a specified directory (
basePath
). - It loads configuration files based on a priority order: first
.env
, then.json
, and lastly.yaml
. If a configuration file is found, it will not search for other file types. - It supports environment-specific configurations using the
APP_ENV
environment variable. For example, ifAPP_ENV
is set todev
, the module will look for.dev.env
,.dev.json
, or.dev.yaml
files in the specified directory. - It parses the configuration files and environment variables.
- It binds the data to a provided struct using reflection.
- It supports caching to avoid re-reading the same configuration multiple times, ensuring better performance.
- Search for Config Files: The module scans the specified directory for valid
.env
,.json
, or.yaml
files.- First, it looks for
.env
,.json
, or.yaml
files in the base directory in the order of priority. - Then, if the
APP_ENV
environment variable is set, it looks for environment-specific files, such as.dev.env
,.prod.env
,.dev.json
, or.prod.json
, in the same priority order.
- First, it looks for
- Load Data: It reads the file content, parses the data, and loads it into memory.
- Environment Variables: Configuration values are loaded into environment variables, and the struct fields are populated from these values using reflection.
- Cache: Frequently accessed configuration data is cached in memory to avoid reloading it repeatedly, improving performance.
- Validation and Defaults: It ensures that required fields are set and assigns default values to fields that are missing.
- File Priority: The module loads configuration files based on a defined priority:
.env
>.json
>.yaml
. If a file is found in one of these formats, it will stop searching for the other formats. - Environment-Specific Files: Supports loading different configuration files based on the environment (e.g.,
.dev.env
,.prod.env
). If theAPP_ENV
environment variable is set, it will attempt to load corresponding environment-specific files. - Nested Structs: Supports nested structs, allowing for more complex configuration structures (e.g., YAML, JSON files with nested fields).
- Custom Parsing: Supports custom types by implementing the
Unmarshal
interface. This allows for more advanced data manipulation during the unmarshalling process. - Validation: Ensures that required configuration fields are set and validates their values, ensuring that no required configurations are missing.
- Cache Management: The cache can be cleared manually or set to expire after a certain period, ensuring that configuration data remains up-to-date.
- Flexible Configuration Sources: The module supports loading configuration data from
.env
,.json
, and.yaml
files. The order of loading is flexible based on whetherAPP_ENV
is set or not.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to fork the repository, make changes, and create pull requests! We welcome contributions that improve functionality or fix bugs.