#!/bin/bash # Smart Parking Finder - Development Setup Script # This script sets up the complete development environment set -e # 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 command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Check prerequisites check_prerequisites() { print_status "Checking prerequisites..." local missing_deps=() if ! command_exists docker; then missing_deps+=("docker") fi if ! command_exists docker-compose; then missing_deps+=("docker-compose") fi if ! command_exists node; then missing_deps+=("node") fi if ! command_exists npm; then missing_deps+=("npm") fi if [ ${#missing_deps[@]} -ne 0 ]; then print_error "Missing required dependencies: ${missing_deps[*]}" echo "" echo "Please install the following:" echo "- Docker: https://docs.docker.com/get-docker/" echo "- Docker Compose: https://docs.docker.com/compose/install/" echo "- Node.js 18+: https://nodejs.org/" exit 1 fi print_success "All prerequisites are installed!" } # Create directory structure create_structure() { print_status "Creating project structure..." # Create main directories mkdir -p {frontend,backend,valhalla/custom_files} # Create subdirectories mkdir -p frontend/{src,public,components,pages} mkdir -p backend/{src,test,database} print_success "Project structure created!" } # Download OSM data download_osm_data() { print_status "Setting up OSM data for Valhalla..." if [ ! -f "valhalla/custom_files/vietnam-latest.osm.pbf" ]; then read -p "Do you want to download Vietnam OSM data now? (y/N): " download_osm if [[ $download_osm =~ ^[Yy]$ ]]; then print_status "Downloading Vietnam OSM data (~100MB)..." cd valhalla ./download-osm-data.sh cd .. else print_warning "OSM data not downloaded. Valhalla may not work properly." print_warning "You can download it later by running: cd valhalla && ./download-osm-data.sh" fi else print_success "OSM data already exists!" fi } # Setup environment files setup_environment() { print_status "Setting up environment files..." # Frontend environment if [ ! -f "frontend/.env.local" ]; then cat > frontend/.env.local << EOF # Frontend Environment Variables NEXT_PUBLIC_API_URL=http://localhost:3001 NEXT_PUBLIC_MAP_TILES_URL=https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png NEXT_PUBLIC_VALHALLA_URL=http://localhost:8002 NEXT_PUBLIC_DEFAULT_LAT=10.7769 NEXT_PUBLIC_DEFAULT_LNG=106.7009 EOF print_success "Created frontend/.env.local" fi # Backend environment if [ ! -f "backend/.env" ]; then cat > backend/.env << EOF # Backend Environment Variables NODE_ENV=development PORT=3001 DATABASE_URL=postgresql://parking_user:parking_pass@localhost:5432/parking_db REDIS_URL=redis://localhost:6379 VALHALLA_URL=http://localhost:8002 JWT_SECRET=your-development-jwt-secret-$(date +%s) JWT_EXPIRATION=24h CORS_ORIGIN=http://localhost:3000 API_PREFIX=api EOF print_success "Created backend/.env" fi } # Setup Docker services setup_docker() { print_status "Setting up Docker services..." # Check if Docker is running if ! docker info >/dev/null 2>&1; then print_error "Docker is not running. Please start Docker first." exit 1 fi # Pull images print_status "Pulling Docker images..." docker-compose pull postgres redis # Start infrastructure services print_status "Starting infrastructure services..." docker-compose up -d postgres redis # Wait for services to be ready print_status "Waiting for services to be ready..." sleep 10 # Check if services are healthy if docker-compose ps postgres | grep -q "healthy\|Up"; then print_success "PostgreSQL is ready!" else print_warning "PostgreSQL may still be starting..." fi if docker-compose ps redis | grep -q "healthy\|Up"; then print_success "Redis is ready!" else print_warning "Redis may still be starting..." fi } # Setup Valhalla setup_valhalla() { print_status "Setting up Valhalla routing engine..." if [ ! -f "valhalla/custom_files/vietnam-latest.osm.pbf" ]; then print_warning "No OSM data found. Skipping Valhalla setup." print_warning "Download OSM data first: cd valhalla && ./download-osm-data.sh" return fi print_status "Building and starting Valhalla (this may take 10-30 minutes)..." docker-compose up -d valhalla print_status "Valhalla is processing OSM data. This may take a while..." print_status "You can check progress with: docker-compose logs -f valhalla" } # Install dependencies install_dependencies() { print_status "Installing Node.js dependencies..." # Frontend dependencies if [ -f "frontend/package.json" ]; then print_status "Installing frontend dependencies..." cd frontend && npm install && cd .. print_success "Frontend dependencies installed!" else print_warning "No frontend/package.json found. Skipping frontend dependencies." fi # Backend dependencies if [ -f "backend/package.json" ]; then print_status "Installing backend dependencies..." cd backend && npm install && cd .. print_success "Backend dependencies installed!" else print_warning "No backend/package.json found. Skipping backend dependencies." fi } # Setup database setup_database() { print_status "Setting up database..." # Wait for PostgreSQL to be ready print_status "Waiting for PostgreSQL to be ready..." timeout=60 while ! docker-compose exec -T postgres pg_isready -U parking_user -d parking_db >/dev/null 2>&1; do if [ $timeout -le 0 ]; then print_error "PostgreSQL is not ready after 60 seconds" exit 1 fi sleep 1 ((timeout--)) done print_success "PostgreSQL is ready!" # Run migrations (if backend exists) if [ -f "backend/package.json" ]; then print_status "Running database migrations..." cd backend # npm run migration:run # Uncomment when migrations exist cd .. print_success "Database migrations completed!" fi } # Main setup function main() { echo "" echo "🚗 Smart Parking Finder - Development Setup" echo "===========================================" echo "" check_prerequisites create_structure setup_environment download_osm_data setup_docker install_dependencies setup_database setup_valhalla echo "" echo "🎉 Setup completed successfully!" echo "" echo "Next steps:" echo "1. Start the development servers:" echo " - Frontend: cd frontend && npm run dev" echo " - Backend: cd backend && npm run start:dev" echo "" echo "2. Access the applications:" echo " - Frontend: http://localhost:3000" echo " - Backend API: http://localhost:3001" echo " - Database (pgAdmin): http://localhost:5050 (with --profile tools)" echo " - Redis (Commander): http://localhost:8081 (with --profile tools)" echo " - Valhalla: http://localhost:8002/status" echo "" echo "3. Useful commands:" echo " - View logs: docker-compose logs -f [service]" echo " - Stop services: docker-compose down" echo " - Restart services: docker-compose restart [service]" echo " - Start with tools: docker-compose --profile tools up -d" echo "" echo "💡 If Valhalla is still processing data, wait for it to complete" echo " Check status: curl http://localhost:8002/status" } # Run main function main "$@"