#!/bin/bash # Knowledge Universe API - Automated Setup Script # This script sets up the complete development/production environment set -e # Exit on error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Banner echo -e "${GREEN}" cat << "EOF" ╔═══════════════════════════════════════════════════════════╗ ║ ║ ║ KNOWLEDGE UNIVERSE API - AUTOMATED SETUP ║ ║ Cache-First Educational Content Aggregation ║ ║ ║ ╚═══════════════════════════════════════════════════════════╝ EOF echo -e "${NC}" # Functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } check_command() { if ! command -v $1 &> /dev/null; then log_error "$1 is not installed" return 1 else log_info "$1 is installed ✓" return 0 fi } # ============================================================================ # STEP 1: Check Prerequisites # ============================================================================ echo "" log_info "Step 1: Checking prerequisites..." MISSING_DEPS=0 if ! check_command python3; then log_error "Python 3.11+ is required" MISSING_DEPS=1 fi if ! check_command docker; then log_error "Docker is required. Install from: https://docs.docker.com/get-docker/" MISSING_DEPS=1 fi if ! check_command docker-compose; then log_warn "docker-compose not found. Trying 'docker compose' instead..." if ! docker compose version &> /dev/null; then log_error "Docker Compose is required" MISSING_DEPS=1 fi fi if [ $MISSING_DEPS -eq 1 ]; then log_error "Missing dependencies. Please install them first." exit 1 fi # ============================================================================ # STEP 2: Setup Environment # ============================================================================ echo "" log_info "Step 2: Setting up environment..." if [ ! -f .env ]; then log_info "Creating .env file from template..." cp .env.example .env log_warn "⚠️ Please edit .env and add your API keys!" log_warn " Required: GITHUB_TOKEN, YOUTUBE_API_KEY" else log_info ".env file already exists ✓" fi # ============================================================================ # STEP 3: Setup Python Environment # ============================================================================ echo "" log_info "Step 3: Setting up Python virtual environment..." if [ ! -d "venv" ]; then log_info "Creating virtual environment..." python3 -m venv venv log_info "Virtual environment created ✓" else log_info "Virtual environment already exists ✓" fi log_info "Activating virtual environment..." source venv/bin/activate log_info "Installing Python dependencies..." pip install --upgrade pip pip install -r requirements.txt log_info "Python environment ready ✓" # ============================================================================ # STEP 4: Docker Setup # ============================================================================ echo "" log_info "Step 4: Setting up Docker environment..." # Ask user for deployment mode echo "" echo "Select deployment mode:" echo "1) Local Development (Redis + API)" echo "2) Production (with monitoring)" echo "3) Skip Docker setup" read -p "Enter choice [1-3]: " DEPLOY_MODE case $DEPLOY_MODE in 1) log_info "Starting development environment..." docker-compose up -d redis api ;; 2) log_info "Starting production environment with monitoring..." docker-compose --profile monitoring up -d ;; 3) log_info "Skipping Docker setup" ;; *) log_error "Invalid choice" exit 1 ;; esac # ============================================================================ # STEP 5: Verify Installation # ============================================================================ echo "" log_info "Step 5: Verifying installation..." # Wait for services to start if [ "$DEPLOY_MODE" != "3" ]; then log_info "Waiting for services to start (30 seconds)..." sleep 30 # Check Redis log_info "Checking Redis connection..." if docker-compose exec -T redis redis-cli ping | grep -q "PONG"; then log_info "Redis is running ✓" else log_error "Redis connection failed" fi # Check API log_info "Checking API health..." HEALTH_CHECK=$(curl -s http://localhost:8000/health || echo "FAILED") if echo "$HEALTH_CHECK" | grep -q "healthy"; then log_info "API is running ✓" else log_error "API health check failed" log_info "Check logs with: docker-compose logs api" fi fi # ============================================================================ # STEP 6: Run Tests # ============================================================================ echo "" read -p "Run test suite? [y/N]: " RUN_TESTS if [ "$RUN_TESTS" = "y" ] || [ "$RUN_TESTS" = "Y" ]; then log_info "Running test suite..." pytest tests/ -v fi # ============================================================================ # STEP 7: GitHub Student Pack Setup Reminder # ============================================================================ echo "" log_info "Step 7: GitHub Student Pack Resources" echo "" echo "To deploy to production, you'll need API keys from:" echo "" echo "1. GitHub API Token" echo " → https://github.com/settings/tokens" echo " Required scopes: public_repo, read:org" echo "" echo "2. YouTube Data API" echo " → https://console.cloud.google.com/apis/credentials" echo "" echo "3. Kaggle API" echo " → https://www.kaggle.com/settings/account" echo "" echo "4. DigitalOcean (\$200 credit via Student Pack)" echo " → https://www.digitalocean.com/github-students" echo "" echo "5. Apply for GitHub Student Pack:" echo " → https://education.github.com/pack" echo "" # ============================================================================ # FINAL SUMMARY # ============================================================================ echo "" echo -e "${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ SETUP COMPLETE! ║${NC}" echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}" echo "" if [ "$DEPLOY_MODE" != "3" ]; then echo "Services running at:" echo " → API: http://localhost:8000" echo " → API Docs: http://localhost:8000/docs" echo " → Metrics: http://localhost:9090/metrics" echo " → Health: http://localhost:8000/health" echo "" echo "View logs:" echo " → docker-compose logs -f api" echo "" echo "Stop services:" echo " → docker-compose down" echo "" fi echo "Next steps:" echo " 1. Edit .env with your API keys" echo " 2. Test API: curl http://localhost:8000/health" echo " 3. Read docs: cat README.md" echo " 4. Deploy: cat DEPLOYMENT.md" echo "" # ============================================================================ # CREATE EXAMPLE REQUEST SCRIPT # ============================================================================ log_info "Creating example request script..." cat > test_api.sh << 'EOFSCRIPT' #!/bin/bash # Test Knowledge Universe API API_URL="http://localhost:8000" echo "Testing Knowledge Universe API..." echo "" # Health check echo "1. Health Check" curl -s $API_URL/health | jq . echo "" # Discovery request echo "2. Discovery Request (Machine Learning)" curl -s -X POST "$API_URL/v1/discover" \ -H "Content-Type: application/json" \ -d '{ "topic": "machine learning", "difficulty": 2, "formats": ["pdf", "video"], "max_results": 3 }' | jq . echo "" # Cache stats echo "3. Cache Statistics" curl -s "$API_URL/v1/cache/stats" | jq . echo "" echo "✓ Test complete!" EOFSCRIPT chmod +x test_api.sh log_info "Created test_api.sh - run it to test the API" echo "" log_info "Setup complete! 🎉"