feat: Add comprehensive configuration system and examples
Added complete configuration support with multiple formats and examples to help users easily configure py-wikijs for their projects. New Features: - Configuration file templates for ENV, YAML, JSON, and INI formats - config_helper.py: Universal configuration loader and client factory * Auto-detects and loads configs from multiple formats * Supports environment variables, YAML, JSON, and INI files * Provides create_client_from_config() for easy client creation * Validates configuration and provides helpful error messages Configuration Templates: - config.env.example: Environment variables (Docker, 12-factor apps) - config.yaml.example: YAML with multi-environment support - config.json.example: JSON for programmatic configuration - config.ini.example: INI for traditional setups Usage Examples: - using_env_config.py: Complete example using .env files - using_yaml_config.py: Complete example using YAML configuration - using_json_config.py: Complete example using JSON configuration Documentation: - docs/CONFIGURATION_GUIDE.md: Comprehensive configuration guide * All configuration methods explained * Security best practices * Environment-specific configurations * Troubleshooting guide Benefits: ✅ Flexible configuration (choose your preferred format) ✅ Keep credentials secure (no hardcoding) ✅ Environment-specific configs (dev/staging/prod) ✅ Docker/container-ready ✅ Full validation and error handling ✅ Comprehensive documentation and examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
104
examples/using_yaml_config.py
Executable file
104
examples/using_yaml_config.py
Executable file
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Example: Using YAML configuration file with py-wikijs.
|
||||
|
||||
This example shows how to use a YAML configuration file to configure
|
||||
the WikiJS client.
|
||||
|
||||
Prerequisites:
|
||||
pip install pyyaml
|
||||
|
||||
Setup:
|
||||
1. Copy config.yaml.example to config.yaml
|
||||
2. Update config.yaml with your Wiki.js credentials
|
||||
3. Run this script
|
||||
|
||||
Usage:
|
||||
python using_yaml_config.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add examples directory to path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
print("❌ PyYAML is required for YAML config files")
|
||||
print("Install with: pip install pyyaml")
|
||||
sys.exit(1)
|
||||
|
||||
from config_helper import create_client_from_config, load_config
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function."""
|
||||
print("=" * 60)
|
||||
print("Using YAML Configuration with py-wikijs")
|
||||
print("=" * 60)
|
||||
|
||||
yaml_file = Path(__file__).parent / "config.yaml"
|
||||
|
||||
if not yaml_file.exists():
|
||||
print(f"\n⚠️ config.yaml not found at {yaml_file}")
|
||||
print(" Copy config.yaml.example to config.yaml and update it")
|
||||
return 1
|
||||
|
||||
try:
|
||||
# Load configuration
|
||||
print("\n📁 Loading configuration from config.yaml...")
|
||||
config = load_config(yaml_file)
|
||||
print("✅ Configuration loaded successfully")
|
||||
|
||||
print(f"\nConfiguration:")
|
||||
print(f" URL: {config.get('wikijs', {}).get('url')}")
|
||||
print(f" Auth method: {config.get('wikijs', {}).get('auth', {}).get('method')}")
|
||||
print(f" Timeout: {config.get('client', {}).get('timeout')}s")
|
||||
print(f" Rate limit: {config.get('client', {}).get('rate_limit')} req/s")
|
||||
print(f" Log level: {config.get('logging', {}).get('level')}")
|
||||
|
||||
# Create client with environment-specific config
|
||||
env = config.get("environments", {}).get("development", {})
|
||||
if env:
|
||||
print(f"\n🔧 Using development environment settings")
|
||||
|
||||
print("\n🔌 Creating WikiJS client...")
|
||||
client = create_client_from_config(config)
|
||||
print(f"✅ Client created successfully")
|
||||
|
||||
# Test connection
|
||||
print("\n🔍 Testing connection...")
|
||||
pages = client.pages.list()
|
||||
print(f"✅ Connected! Found {len(pages)} page(s)")
|
||||
|
||||
if pages:
|
||||
print(f"\n📄 Sample pages:")
|
||||
for page in pages[:3]: # Show first 3
|
||||
print(f" - [{page.id}] {page.title} ({page.path})")
|
||||
|
||||
# Show metrics
|
||||
metrics = client.get_metrics()
|
||||
print(f"\n📊 Client Metrics:")
|
||||
print(f" Total requests: {metrics['total_requests']}")
|
||||
print(f" Error rate: {metrics.get('error_rate', 0):.2f}%")
|
||||
|
||||
except FileNotFoundError as e:
|
||||
print(f"❌ {e}")
|
||||
return 1
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return 1
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✅ Example completed successfully!")
|
||||
print("=" * 60)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user