talk2me/setup_databases.sh
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

156 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# Setup script for Redis and PostgreSQL
echo "Talk2Me Database Setup Script"
echo "============================="
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "Please do not run this script as root"
exit 1
fi
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for PostgreSQL
echo "Checking PostgreSQL installation..."
if command_exists psql; then
echo "✓ PostgreSQL is installed"
psql --version
else
echo "✗ PostgreSQL is not installed"
echo "Please install PostgreSQL first:"
echo " Ubuntu/Debian: sudo apt-get install postgresql postgresql-contrib"
echo " MacOS: brew install postgresql"
exit 1
fi
# Check for Redis
echo ""
echo "Checking Redis installation..."
if command_exists redis-cli; then
echo "✓ Redis is installed"
redis-cli --version
else
echo "✗ Redis is not installed"
echo "Please install Redis first:"
echo " Ubuntu/Debian: sudo apt-get install redis-server"
echo " MacOS: brew install redis"
exit 1
fi
# Check if Redis is running
echo ""
echo "Checking Redis server status..."
if redis-cli ping > /dev/null 2>&1; then
echo "✓ Redis server is running"
else
echo "✗ Redis server is not running"
echo "Starting Redis server..."
if command_exists systemctl; then
sudo systemctl start redis
else
redis-server --daemonize yes
fi
sleep 2
if redis-cli ping > /dev/null 2>&1; then
echo "✓ Redis server started successfully"
else
echo "✗ Failed to start Redis server"
exit 1
fi
fi
# Create PostgreSQL database
echo ""
echo "Setting up PostgreSQL database..."
read -p "Enter PostgreSQL username (default: $USER): " PG_USER
PG_USER=${PG_USER:-$USER}
read -p "Enter database name (default: talk2me): " DB_NAME
DB_NAME=${DB_NAME:-talk2me}
# Check if database exists
if psql -U "$PG_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
echo "Database '$DB_NAME' already exists"
read -p "Do you want to drop and recreate it? (y/N): " RECREATE
if [[ $RECREATE =~ ^[Yy]$ ]]; then
echo "Dropping database '$DB_NAME'..."
psql -U "$PG_USER" -c "DROP DATABASE $DB_NAME;"
echo "Creating database '$DB_NAME'..."
psql -U "$PG_USER" -c "CREATE DATABASE $DB_NAME;"
fi
else
echo "Creating database '$DB_NAME'..."
psql -U "$PG_USER" -c "CREATE DATABASE $DB_NAME;"
fi
# Create .env file if it doesn't exist
if [ ! -f .env ]; then
echo ""
echo "Creating .env file..."
cat > .env << EOF
# Database Configuration
DATABASE_URL=postgresql://$PG_USER@localhost/$DB_NAME
SQLALCHEMY_DATABASE_URI=postgresql://$PG_USER@localhost/$DB_NAME
# Redis Configuration
REDIS_URL=redis://localhost:6379/0
REDIS_DECODE_RESPONSES=false
REDIS_MAX_CONNECTIONS=50
# Flask Configuration
FLASK_ENV=development
SECRET_KEY=$(openssl rand -base64 32)
ADMIN_TOKEN=$(openssl rand -base64 24)
# TTS Configuration
TTS_SERVER_URL=http://localhost:5050/v1/audio/speech
TTS_API_KEY=your-tts-api-key-here
# Whisper Configuration
WHISPER_MODEL_SIZE=base
WHISPER_DEVICE=auto
# Ollama Configuration
OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=gemma3:27b
EOF
echo "✓ .env file created"
echo "Please update the TTS_API_KEY in .env file"
else
echo "✓ .env file already exists"
fi
# Install Python dependencies
echo ""
echo "Installing Python dependencies..."
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
echo "✓ Python dependencies installed"
else
echo "✗ requirements.txt not found"
fi
# Initialize database
echo ""
echo "Initializing database..."
python database_init.py
echo ""
echo "Setup complete!"
echo ""
echo "Next steps:"
echo "1. Update the TTS_API_KEY in .env file"
echo "2. Run 'python migrations.py init' to initialize migrations"
echo "3. Run 'python migrations.py create \"Initial migration\"' to create first migration"
echo "4. Run 'python migrations.py run' to apply migrations"
echo "5. Backup your current app.py and rename app_with_db.py to app.py"
echo "6. Start the application with 'python app.py'"
echo ""
echo "To run Redis and PostgreSQL as services:"
echo " Redis: sudo systemctl enable redis && sudo systemctl start redis"
echo " PostgreSQL: sudo systemctl enable postgresql && sudo systemctl start postgresql"