Configuration

The config module loads configuration from environment variables and .env files.

Setup

import "github.com/0xfurai/gonest/config"

var ConfigModule = config.NewModule(config.ModuleOptions{
    EnvFilePath: ".env",
    IsGlobal:    true,
})

Usage

Inject *config.ConfigService into any provider:

func NewDatabaseService(cfg *config.ConfigService) *DatabaseService {
    return &DatabaseService{
        host: cfg.GetOrDefault("DB_HOST", "localhost"),
        port: cfg.GetIntOrDefault("DB_PORT", 5432),
    }
}

API

MethodReturnsDescription
Get(key)stringGet value (env file, then OS env)
GetOrDefault(key, default)stringWith fallback
GetInt(key)int, errorParse as int
GetIntOrDefault(key, default)intInt with fallback
GetBool(key)bool, errorParse as bool
GetBoolOrDefault(key, default)boolBool with fallback
Has(key)boolCheck existence
Set(key, value)Set at runtime

.env File Format

DB_HOST=localhost
DB_PORT=5432
DB_NAME="mydb"
DB_PASS='secret'
# Comments supported