#!/bin/bash # YourDreamNameHere Local Development Setup Script # This script sets up a complete local development environment set -euo pipefail # Configuration PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" COMPOSE_FILE="$PROJECT_DIR/docker-compose.yml" ENV_FILE="$PROJECT_DIR/configs/.env" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check dependencies check_dependencies() { log_info "Checking dependencies..." # Check Docker if ! command -v docker &> /dev/null; then log_error "Docker is not installed. Please install Docker first." exit 1 fi # Check Docker Compose if ! command -v docker-compose &> /dev/null; then log_error "Docker Compose is not installed. Please install Docker Compose first." exit 1 fi # Check Go if ! command -v go &> /dev/null; then log_error "Go is not installed. Please install Go 1.21 or later." exit 1 fi # Check Node.js (optional, for frontend development) if ! command -v node &> /dev/null; then log_warning "Node.js is not installed. Some frontend tools may not work." fi log_success "All dependencies checked" } # Setup environment setup_environment() { log_info "Setting up environment..." # Create .env file if it doesn't exist if [ ! -f "$ENV_FILE" ]; then log_info "Creating .env file from template..." cp "$PROJECT_DIR/configs/.env.example" "$ENV_FILE" # Generate random secrets sed -i "s/your_jwt_secret_key_here_make_it_long_and_random/$(openssl rand -base64 64)/" "$ENV_FILE" sed -i "s/your_secure_password/$(openssl rand -base64 16)/" "$ENV_FILE" sed -i "s/redis_password_change_me/$(openssl rand -base64 16)/" "$ENV_FILE" log_warning "Please edit $ENV_FILE with your actual API keys and configuration" else log_info "Environment file already exists" fi # Create necessary directories mkdir -p "$PROJECT_DIR/logs" mkdir -p "$PROJECT_DIR/backups" mkdir -p "$PROJECT_DIR/ssl" log_success "Environment setup completed" } # Build application build_application() { log_info "Building application..." cd "$PROJECT_DIR" # Download dependencies go mod download go mod tidy # Build binary go build -o bin/ydn-app cmd/main.go log_success "Application built successfully" } # Start services start_services() { log_info "Starting development services..." cd "$PROJECT_DIR" # Start Docker services docker-compose -f "$COMPOSE_FILE" up -d # Wait for database to be ready log_info "Waiting for database to be ready..." max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if docker-compose -f "$COMPOSE_FILE" exec -T ydn-db pg_isready -U ydn_user -d ydn_db > /dev/null 2>&1; then break fi attempt=$((attempt + 1)) sleep 2 done if [ $attempt -eq $max_attempts ]; then log_error "Database failed to start" exit 1 fi # Wait for Redis to be ready log_info "Waiting for Redis to be ready..." attempt=0 while [ $attempt -lt $max_attempts ]; do if docker-compose -f "$COMPOSE_FILE" exec -T ydn-redis redis-cli ping > /dev/null 2>&1; then break fi attempt=$((attempt + 1)) sleep 2 done if [ $attempt -eq $max_attempts ]; then log_error "Redis failed to start" exit 1 fi log_success "All services started successfully" } # Run database migrations run_migrations() { log_info "Running database migrations..." cd "$PROJECT_DIR" # Run migrations if ./bin/ydn-app migrate; then log_success "Database migrations completed" else log_error "Database migrations failed" exit 1 fi } # Setup development tools setup_dev_tools() { log_info "Setting up development tools..." cd "$PROJECT_DIR" # Install pre-commit hooks if command -v pre-commit &> /dev/null; then pre-commit install log_info "Pre-commit hooks installed" else log_warning "pre-commit not found. Install with: pip install pre-commit" fi # Download test dependencies go mod download log_success "Development tools setup completed" } # Show development URLs show_urls() { log_success "Development environment is ready!" echo echo "🌐 Application URLs:" echo " Main Application: http://localhost:8080" echo " API Documentation: http://localhost:8080/swagger/index.html" echo " Health Check: http://localhost:8080/health" echo " Dolibarr: http://localhost:8081" echo echo "🐳 Docker Services:" echo " PostgreSQL: localhost:5432" echo " Redis: localhost:6379" echo echo "📊 Monitoring (optional):" echo " Grafana: http://localhost:3000 (admin/grafana_admin_change_me)" echo " Prometheus: http://localhost:9090" echo echo "🔧 Development Commands:" echo " Run application: ./bin/ydn-app" echo " Run tests: ./scripts/test.sh" echo " Stop services: docker-compose -f docker-compose.yml down" echo " View logs: docker-compose -f docker-compose.yml logs -f" } # Start development server start_dev_server() { log_info "Starting development server..." cd "$PROJECT_DIR" # Start the application in development mode if [ -f "./bin/ydn-app" ]; then ./bin/ydn-app else log_error "Application binary not found. Run './scripts/dev.sh setup' first." exit 1 fi } # Stop services stop_services() { log_info "Stopping development services..." cd "$PROJECT_DIR" docker-compose -f "$COMPOSE_FILE" down log_success "All services stopped" } # Clean environment clean_environment() { log_info "Cleaning development environment..." cd "$PROJECT_DIR" # Stop and remove containers docker-compose -f "$COMPOSE_FILE" down -v --remove-orphans # Remove images docker-compose -f "$COMPOSE_FILE" down --rmi all # Clean up binaries and logs rm -rf bin/ rm -rf logs/* rm -rf backups/* log_success "Environment cleaned" } # Reset database reset_database() { log_warning "Resetting database..." cd "$PROJECT_DIR" # Stop services docker-compose -f "$COMPOSE_FILE" down # Remove database volume docker volume rm $(docker-compose -f "$COMPOSE_FILE" config --volumes | grep postgres_data) 2>/dev/null || true # Start services again docker-compose -f "$COMPOSE_FILE" up -d # Wait for database sleep 10 # Run migrations run_migrations log_success "Database reset completed" } # Show help show_help() { cat << EOF YourDreamNameHere Development Setup Script Usage: $0 [COMMAND] Commands: setup Set up the complete development environment start Start development services stop Stop development services restart Restart development services dev Start development server test Run test suite clean Clean up development environment reset-db Reset database logs Show service logs status Show service status help Show this help message Examples: $0 setup # Initial setup $0 start # Start services $0 dev # Start development server $0 test # Run tests $0 clean # Clean everything Environment File: Edit $ENV_FILE to configure your API keys and settings EOF } # Main execution case "${1:-setup}" in setup) check_dependencies setup_environment build_application start_services run_migrations setup_dev_tools show_urls ;; start) start_services run_migrations show_urls ;; stop) stop_services ;; restart) stop_services start_services run_migrations show_urls ;; dev) start_dev_server ;; test) "$PROJECT_DIR/scripts/test.sh" ;; clean) clean_environment ;; reset-db) reset_database ;; logs) cd "$PROJECT_DIR" docker-compose -f "$COMPOSE_FILE" logs -f ;; status) cd "$PROJECT_DIR" docker-compose -f "$COMPOSE_FILE" ps ;; help|--help|-h) show_help ;; *) log_error "Unknown command: $1" show_help exit 1 ;; esac