#!/bin/bash # ============================================================================= # Ebook Coupon Management System - Startup Script # ============================================================================= # This script starts the application and automatically initializes the database # ============================================================================= set -e # Exit on error echo "======================================================================" echo " EBOOK COUPON MANAGEMENT SYSTEM - STARTUP" echo "======================================================================" echo "" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored messages print_success() { echo -e "${GREEN}✅ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_info() { echo -e "${NC}ℹ️ $1${NC}" } # Check if .env file exists if [ ! -f ".env" ]; then print_warning ".env file not found!" print_info "Copying .env.example to .env..." if [ -f ".env.example" ]; then cp .env.example .env print_success ".env file created from .env.example" print_warning "Please update .env with your configuration!" echo "" read -p "Press Enter to continue or Ctrl+C to exit and configure .env..." else print_error ".env.example not found! Cannot create .env file." exit 1 fi fi # Navigate to admin-backend directory cd admin-backend print_info "Checking virtual environment..." # Check if virtual environment exists if [ ! -d "../.venv" ]; then print_warning "Virtual environment not found. Creating one..." cd .. python3 -m venv .venv print_success "Virtual environment created" cd admin-backend fi # Activate virtual environment print_info "Activating virtual environment..." source ../.venv/bin/activate print_success "Virtual environment activated" # Install/update requirements print_info "Installing/updating dependencies..." pip install -q --upgrade pip pip install -q -r ../requirements.txt print_success "Dependencies installed" echo "" echo "======================================================================" print_info "Starting application server..." print_info "The database will be automatically initialized on startup:" print_info " - Tables will be created if they don't exist" print_info " - Admin user will be created if none exists" echo "======================================================================" echo "" # Check if port is already in use if lsof -Pi :8000 -sTCP:LISTEN -t >/dev/null 2>&1 ; then print_warning "Port 8000 is already in use!" print_info "Killing existing process..." kill -9 $(lsof -t -i:8000) 2>/dev/null || true sleep 2 fi # Start the server print_success "Starting uvicorn server on http://0.0.0.0:8000" echo "" echo "======================================================================" print_info "Access the application:" print_info " - API: http://localhost:8000" print_info " - Health Check: http://localhost:8000/health" print_info " - API Docs: http://localhost:8000/docs" print_info " - Admin Login: http://localhost:8000/login" echo "======================================================================" echo "" print_info "Default Admin Credentials (from .env):" print_info " Username: admin" print_info " Password: admin123" echo "======================================================================" echo "" # Start uvicorn (init_db.py will run automatically) uvicorn main:app --reload --host 0.0.0.0 --port 8000