talk2me/admin_loader.py
Adolfo Delorenzo fa951c3141 Add comprehensive database integration, authentication, and admin dashboard
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>
2025-06-03 18:21:56 -06:00

47 lines
1.6 KiB
Python

"""
Dynamic admin module loader that chooses between full and simple admin based on service availability
"""
import os
import logging
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
logger = logging.getLogger(__name__)
def load_admin_module():
"""
Dynamically load admin module based on service availability
Returns (admin_bp, init_admin) tuple
"""
# Check if we should force simple mode
if os.environ.get('ADMIN_SIMPLE_MODE', '').lower() in ('1', 'true', 'yes'):
logger.info("Simple admin mode forced by environment variable")
from admin_simple import admin_bp, init_admin
return admin_bp, init_admin
# Try to import full admin module
try:
# Quick check for Redis
import redis
r = redis.Redis.from_url(os.environ.get('REDIS_URL', 'redis://localhost:6379/0'))
r.ping()
# Quick check for PostgreSQL
from sqlalchemy import create_engine, text
db_url = os.environ.get('DATABASE_URL', 'postgresql://localhost/talk2me')
engine = create_engine(db_url, pool_pre_ping=True)
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
# If we get here, both services are available
from admin import admin_bp, init_admin
logger.info("Using full admin module with Redis and PostgreSQL support")
return admin_bp, init_admin
except Exception as e:
logger.warning(f"Cannot use full admin module: {e}")
logger.info("Falling back to simple admin module")
from admin_simple import admin_bp, init_admin
return admin_bp, init_admin