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>
104 lines
3.0 KiB
Python
Executable File
104 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Example: Using .env configuration file with py-wikijs.
|
|
|
|
This example shows how to use environment variables (.env file) to configure
|
|
the WikiJS client.
|
|
|
|
Prerequisites:
|
|
pip install python-dotenv
|
|
|
|
Setup:
|
|
1. Copy config.env.example to .env
|
|
2. Update .env with your Wiki.js credentials
|
|
3. Run this script
|
|
|
|
Usage:
|
|
python using_env_config.py
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add examples directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
try:
|
|
from dotenv import load_dotenv
|
|
except ImportError:
|
|
print("❌ python-dotenv is required for .env files")
|
|
print("Install with: pip install python-dotenv")
|
|
sys.exit(1)
|
|
|
|
from config_helper import create_client_from_config, load_config
|
|
|
|
|
|
def main():
|
|
"""Main function."""
|
|
print("=" * 60)
|
|
print("Using .env Configuration with py-wikijs")
|
|
print("=" * 60)
|
|
|
|
# Method 1: Load .env explicitly
|
|
print("\n📁 Method 1: Load .env explicitly")
|
|
env_file = Path(__file__).parent / ".env"
|
|
|
|
if not env_file.exists():
|
|
print(f"⚠️ .env file not found at {env_file}")
|
|
print(" Copy config.env.example to .env and update it")
|
|
print("\n📁 Method 2: Using config helper")
|
|
|
|
# Load the .env file
|
|
load_dotenv(env_file)
|
|
|
|
# Method 2: Use config helper (recommended)
|
|
try:
|
|
config = load_config(".env")
|
|
print(f"✅ Loaded configuration from .env")
|
|
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")
|
|
|
|
# Create client
|
|
print("\n🔌 Creating WikiJS client...")
|
|
client = create_client_from_config(config)
|
|
print(f"✅ Client created successfully")
|
|
print(f" Base URL: {client.base_url}")
|
|
|
|
# Test connection
|
|
print("\n🔍 Testing connection...")
|
|
pages = client.pages.list()
|
|
print(f"✅ Connected! Found {len(pages)} page(s)")
|
|
|
|
if pages:
|
|
print(f"\n📄 First page:")
|
|
print(f" ID: {pages[0].id}")
|
|
print(f" Title: {pages[0].title}")
|
|
print(f" Path: {pages[0].path}")
|
|
|
|
# Show metrics
|
|
metrics = client.get_metrics()
|
|
print(f"\n📊 Metrics:")
|
|
print(f" Total requests: {metrics['total_requests']}")
|
|
print(f" Successful: {metrics['successful_requests']}")
|
|
print(f" Failed: {metrics['failed_requests']}")
|
|
|
|
except FileNotFoundError:
|
|
print("❌ .env file not found")
|
|
print(" 1. Copy config.env.example to .env")
|
|
print(" 2. Update with your Wiki.js credentials")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|
|
return 1
|
|
|
|
print("\n" + "=" * 60)
|
|
print("✅ Example completed successfully!")
|
|
print("=" * 60)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|