This commit introduces major enhancements to Talk2Me: ## Database Integration - PostgreSQL support with SQLAlchemy ORM - Redis integration for caching and real-time analytics - Automated database initialization scripts - Migration support infrastructure ## User Authentication System - JWT-based API authentication - Session-based web authentication - API key authentication for programmatic access - User roles and permissions (admin/user) - Login history and session tracking - Rate limiting per user with customizable limits ## Admin Dashboard - Real-time analytics and monitoring - User management interface (create, edit, delete users) - System health monitoring - Request/error tracking - Language pair usage statistics - Performance metrics visualization ## Key Features - Dual authentication support (token + user accounts) - Graceful fallback for missing services - Non-blocking analytics middleware - Comprehensive error handling - Session management with security features ## Bug Fixes - Fixed rate limiting bypass for admin routes - Added missing email validation method - Improved error handling for missing database tables - Fixed session-based authentication for API endpoints 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
75 lines
2.5 KiB
Python
Executable File
75 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Initialize all database tables for Talk2Me"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import logging
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def run_script(script_name):
|
|
"""Run a Python script and return success status"""
|
|
try:
|
|
logger.info(f"Running {script_name}...")
|
|
result = subprocess.run([sys.executable, script_name], capture_output=True, text=True)
|
|
|
|
if result.returncode == 0:
|
|
logger.info(f"✓ {script_name} completed successfully")
|
|
return True
|
|
else:
|
|
logger.error(f"✗ {script_name} failed with return code {result.returncode}")
|
|
if result.stderr:
|
|
logger.error(f"Error output: {result.stderr}")
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"✗ Failed to run {script_name}: {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""Initialize all databases"""
|
|
logger.info("=== Talk2Me Database Initialization ===")
|
|
|
|
# Check if DATABASE_URL is set
|
|
if not os.environ.get('DATABASE_URL'):
|
|
logger.error("DATABASE_URL environment variable not set!")
|
|
logger.info("Please set DATABASE_URL in your .env file")
|
|
logger.info("Example: DATABASE_URL=postgresql://postgres:password@localhost:5432/talk2me")
|
|
return False
|
|
|
|
logger.info(f"Using database: {os.environ.get('DATABASE_URL')}")
|
|
|
|
scripts = [
|
|
"database_init.py", # Initialize SQLAlchemy models
|
|
"init_auth_db.py", # Initialize authentication tables
|
|
"init_analytics_db.py" # Initialize analytics tables
|
|
]
|
|
|
|
success = True
|
|
for script in scripts:
|
|
if os.path.exists(script):
|
|
if not run_script(script):
|
|
success = False
|
|
else:
|
|
logger.warning(f"Script {script} not found, skipping...")
|
|
|
|
if success:
|
|
logger.info("\n✅ All database initialization completed successfully!")
|
|
logger.info("\nYou can now:")
|
|
logger.info("1. Create an admin user by calling POST /api/init-admin-user")
|
|
logger.info("2. Or use the admin token to log in and create users")
|
|
logger.info("3. Check /api/test-auth to verify authentication is working")
|
|
else:
|
|
logger.error("\n❌ Some database initialization steps failed!")
|
|
logger.info("Please check the errors above and try again")
|
|
|
|
return success
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1) |