Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 26, 2025

Implements comprehensive YAML configuration support following best practices, while maintaining full backward compatibility with existing environment variable setups.

Overview

This PR adds a modern, production-ready configuration system that supports structured YAML files with strict schema validation, automatic configuration discovery, and seamless environment variable overrides.

Key Features

🎯 YAML Configuration with Strict Validation

  • Structured Schema: All configuration structs now include YAML tags with comprehensive validation rules using Go's validator package
  • Type Safety: Proper validation for required fields, minimum values, conditional requirements, and data types
  • Duration Support: Native Go duration format support in YAML (e.g., 15m, 24h, 168h)
  • Detailed Error Reporting: Field-specific validation errors with clear guidance

🔍 Automatic Configuration Discovery

The system automatically searches for configuration files in standard locations:

  • ./config.yaml / ./config.yml
  • ./configs/config.yaml / ./configs/config.yml
  • $HOME/.config/go-api/config.yaml
  • /etc/go-api/config.yaml

🔄 Environment Variable Override System

  • Precedence: Environment variables override YAML values when present
  • Flexible Deployment: Use YAML for base configuration, environment variables for secrets and deployment-specific overrides
  • Zero Breaking Changes: All existing environment variable functionality preserved

📚 Developer Experience Enhancements

  • Example Configuration: Added config.example.yaml with comprehensive examples and comments
  • Documentation: Complete configuration guide in docs/CONFIGURATION.md with schemas, examples, and troubleshooting
  • Security: Updated .gitignore to prevent accidental commit of sensitive configuration files

Example Usage

# config.yaml
server:
  host: localhost
  port: "8000"

database:
  url: postgres://user:password@localhost/dbname?sslmode=disable

auth:
  jwt_secret: "your-secret-key"
  jwt_issuer: "your-app"
  token_expiration: 15m
  refresh_expiration: 168h

storage:
  enabled: true
  bucket_name: "my-bucket"
  use_public_url: true

Environment variables still work and take precedence:

export DATABASE_URL="postgres://prod-user:prod-pass@prod-host/prod-db"
export JWT_SECRET="production-secret-key"

Migration Path

Existing Deployments: No changes required - continue using environment variables as before

New Deployments: Can use YAML configuration with environment variable overrides for sensitive values

Hybrid Approach: Define base configuration in YAML, override secrets and environment-specific values with environment variables

Implementation Details

  • Backward Compatibility: All existing code continues to work unchanged
  • Validation: Uses github.com/go-playground/validator/v10 for comprehensive validation
  • Error Handling: Graceful fallback from YAML → Environment Variables → Defaults
  • Testing: Complete test suite covering all scenarios including validation, overrides, and error cases

Configuration Schema Validation

The system now validates:

  • Required fields are present
  • Conditional requirements (e.g., storage fields required when storage is enabled)
  • Minimum values for durations and numeric fields
  • Proper format for time durations
  • Database and Redis connection string presence

This prevents common configuration errors and improves application reliability by catching misconfigurations at startup rather than runtime.

Original prompt

Siguiendo buenas practicas, agrega soporte para cargar la configuracion y env variables desde un archivo yaml con una estructura estricta y bien validada


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Siguiendo buenas practicas, agrega soporte para cargar la configuracion y env variables desde un archivo yaml con una estructura estricta y bien validada Add YAML configuration support with strict validation and environment variable overrides Sep 26, 2025
Copilot AI requested a review from imlargo September 26, 2025 05:34
Copilot finished work on behalf of imlargo September 26, 2025 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants