Environment methods
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ClassroomCopilot Startup Script
|
||||
# Usage: ./start.sh [start_mode]
|
||||
# start_mode options: init, dev, prod
|
||||
|
||||
set -e
|
||||
|
||||
# Function to show help
|
||||
show_help() {
|
||||
echo "ClassroomCopilot Startup Script"
|
||||
echo ""
|
||||
echo "Usage: ./start.sh [start_mode]"
|
||||
echo ""
|
||||
echo "Start modes:"
|
||||
echo " init - Run initialization scripts (database setup, etc.)"
|
||||
echo " dev - Run development server with auto-reload"
|
||||
echo " prod - Run production server (for Docker/containerized deployment)"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " ./start.sh # Run in dev mode (default)"
|
||||
echo " ./start.sh init # Run initialization only"
|
||||
echo " ./start.sh dev # Run development server"
|
||||
echo " ./start.sh prod # Run production server"
|
||||
echo ""
|
||||
echo "For more information, see README_STARTUP.md"
|
||||
}
|
||||
|
||||
# Check for help flag
|
||||
if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "help" ]]; then
|
||||
show_help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Default to dev mode if no argument provided
|
||||
START_MODE=${1:-dev}
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to print colored output
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Function to check if Python is available
|
||||
check_python() {
|
||||
if command -v python3 &> /dev/null; then
|
||||
PYTHON_CMD="python3"
|
||||
elif command -v python &> /dev/null; then
|
||||
PYTHON_CMD="python"
|
||||
else
|
||||
print_error "Python is not installed or not in PATH"
|
||||
exit 1
|
||||
fi
|
||||
print_status "Using Python: $PYTHON_CMD"
|
||||
}
|
||||
|
||||
# Function to check if virtual environment is activated
|
||||
check_venv() {
|
||||
if [[ "$VIRTUAL_ENV" == "" ]]; then
|
||||
print_warning "No virtual environment detected. Consider activating one for production use."
|
||||
else
|
||||
print_status "Virtual environment activated: $VIRTUAL_ENV"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check environment variables
|
||||
check_env() {
|
||||
if [[ ! -f ".env" ]]; then
|
||||
print_warning ".env file not found. Make sure environment variables are set."
|
||||
else
|
||||
print_status ".env file found"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to run initialization
|
||||
run_init() {
|
||||
print_status "Running initialization mode..."
|
||||
print_status "This will set up databases and initial data structures."
|
||||
|
||||
# Check if we should proceed
|
||||
read -p "Do you want to continue with initialization? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
print_status "Initialization cancelled."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
print_status "Starting initialization process..."
|
||||
$PYTHON_CMD main.py --mode init
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
print_success "Initialization completed successfully!"
|
||||
else
|
||||
print_error "Initialization failed!"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to run development mode
|
||||
run_dev() {
|
||||
print_status "Running development mode..."
|
||||
print_status "Starting uvicorn server with auto-reload..."
|
||||
|
||||
export BACKEND_DEV_MODE=true
|
||||
$PYTHON_CMD main.py --mode dev
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
print_success "Development server stopped successfully!"
|
||||
else
|
||||
print_error "Development server failed!"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to run production mode
|
||||
run_prod() {
|
||||
print_status "Running production mode..."
|
||||
print_status "Starting uvicorn server in production mode..."
|
||||
|
||||
export BACKEND_DEV_MODE=false
|
||||
$PYTHON_CMD main.py --mode prod
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
print_success "Production server stopped successfully!"
|
||||
else
|
||||
print_error "Production server failed!"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
print_status "ClassroomCopilot Startup Script"
|
||||
print_status "Start mode: $START_MODE"
|
||||
echo
|
||||
|
||||
# Pre-flight checks
|
||||
check_python
|
||||
check_venv
|
||||
check_env
|
||||
echo
|
||||
|
||||
# Execute based on start mode
|
||||
case $START_MODE in
|
||||
"init")
|
||||
run_init
|
||||
;;
|
||||
"dev")
|
||||
run_dev
|
||||
;;
|
||||
"prod")
|
||||
run_prod
|
||||
;;
|
||||
*)
|
||||
print_error "Invalid start mode: $START_MODE"
|
||||
print_status "Valid modes: init, dev, prod"
|
||||
print_status "Usage: ./start.sh [start_mode]"
|
||||
print_status "Use './start.sh --help' for more information"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user