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:
108
examples/using_json_config.py
Executable file
108
examples/using_json_config.py
Executable file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Example: Using JSON configuration file with py-wikijs.
|
||||
|
||||
This example shows how to use a JSON configuration file to configure
|
||||
the WikiJS client.
|
||||
|
||||
Setup:
|
||||
1. Copy config.json.example to config.json
|
||||
2. Update config.json with your Wiki.js credentials
|
||||
3. Run this script
|
||||
|
||||
Usage:
|
||||
python using_json_config.py
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add examples directory to path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from config_helper import create_client_from_config, load_config
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function."""
|
||||
print("=" * 60)
|
||||
print("Using JSON Configuration with py-wikijs")
|
||||
print("=" * 60)
|
||||
|
||||
json_file = Path(__file__).parent / "config.json"
|
||||
|
||||
if not json_file.exists():
|
||||
print(f"\n⚠️ config.json not found at {json_file}")
|
||||
print(" Copy config.json.example to config.json and update it")
|
||||
return 1
|
||||
|
||||
try:
|
||||
# Load configuration
|
||||
print("\n📁 Loading configuration from config.json...")
|
||||
config = load_config(json_file)
|
||||
print("✅ Configuration loaded successfully")
|
||||
|
||||
# Pretty print config (without sensitive data)
|
||||
safe_config = config.copy()
|
||||
if "wikijs" in safe_config and "auth" in safe_config["wikijs"]:
|
||||
if "api_key" in safe_config["wikijs"]["auth"]:
|
||||
safe_config["wikijs"]["auth"]["api_key"] = "***REDACTED***"
|
||||
|
||||
print(f"\nConfiguration (sanitized):")
|
||||
print(json.dumps(safe_config, indent=2))
|
||||
|
||||
# Create client
|
||||
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)")
|
||||
|
||||
# Create a new page (example)
|
||||
if input("\n❓ Create a test page? (y/N): ").lower() == "y":
|
||||
from wikijs.models import PageCreate
|
||||
|
||||
test_page = PageCreate(
|
||||
title="Test Page from JSON Config",
|
||||
path="test/json-config-example",
|
||||
content="# Test Page\n\nCreated using JSON configuration!",
|
||||
description="Example page created from JSON config",
|
||||
is_published=False,
|
||||
locale="en",
|
||||
tags=["test", "json", "config"],
|
||||
)
|
||||
|
||||
print("\n📝 Creating test page...")
|
||||
created = client.pages.create(test_page)
|
||||
print(f"✅ Page created: {created.title} (ID: {created.id})")
|
||||
print(f" Path: {created.path}")
|
||||
|
||||
# Show metrics
|
||||
metrics = client.get_metrics()
|
||||
print(f"\n📊 Client Metrics:")
|
||||
print(f" Total requests: {metrics['total_requests']}")
|
||||
print(f" Successful: {metrics['successful_requests']}")
|
||||
print(f" Failed: {metrics['failed_requests']}")
|
||||
|
||||
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