Rustelo/scripts/utils/demo_root_path.sh

142 lines
4.1 KiB
Bash
Raw Normal View History

2025-07-07 23:53:50 +01:00
#!/bin/bash
# ROOT_PATH Configuration Demo Script
# This script demonstrates how ROOT_PATH affects path resolution
set -e
echo "🚀 ROOT_PATH Configuration Demo"
echo "================================"
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE} $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# Check if we're in the right directory
if [ ! -f "config.toml" ]; then
print_error "Please run this script from the project root directory (where config.toml is located)"
exit 1
fi
# Build the project first
print_info "Building the project..."
cd server
cargo build --release --quiet
cd ..
print_success "Project built successfully!"
echo
# Demo 1: Default behavior (current directory)
print_info "Demo 1: Default ROOT_PATH behavior"
echo "Current directory: $(pwd)"
echo "Running: cargo run --bin config_tool -- show"
cd server
cargo run --release --bin config_tool -- show 2>/dev/null | grep -E "(Assets|Public|Logs|Site)" | head -4
cd ..
echo
# Demo 2: Custom ROOT_PATH
print_info "Demo 2: Custom ROOT_PATH"
DEMO_PATH="/tmp/rustelo-demo"
mkdir -p "$DEMO_PATH"
echo "Created demo directory: $DEMO_PATH"
echo "Running: ROOT_PATH=$DEMO_PATH cargo run --bin config_tool -- show"
cd server
ROOT_PATH="$DEMO_PATH" cargo run --release --bin config_tool -- show 2>/dev/null | grep -E "(Assets|Public|Logs|Site)" | head -4
cd ..
echo
# Demo 3: Show path resolution in action
print_info "Demo 3: Path resolution comparison"
echo "Default (relative paths):"
cd server
cargo run --release --bin config_tool -- show 2>/dev/null | grep -E "Assets Dir:|Public Dir:" | sed 's/^/ /'
echo
echo "With ROOT_PATH=/opt/myapp (absolute paths):"
ROOT_PATH="/opt/myapp" cargo run --release --bin config_tool -- show 2>/dev/null | grep -E "Assets Dir:|Public Dir:" | sed 's/^/ /' 2>/dev/null || echo " (Note: /opt/myapp doesn't exist, so validation would fail)"
cd ..
echo
# Demo 4: Environment variable combinations
print_info "Demo 4: Environment variable combinations"
echo "You can combine ROOT_PATH with other environment variables:"
echo
echo "Example commands:"
echo " ROOT_PATH=/app ENVIRONMENT=production ./target/release/server"
echo " ROOT_PATH=/var/www/myapp SERVER_PORT=8080 ./target/release/server"
echo " ROOT_PATH=/opt/myapp DATABASE_URL=postgresql://... ./target/release/server"
echo
# Demo 5: Docker example
print_info "Demo 5: Docker deployment example"
echo "In a Docker container, you might use:"
echo
cat << 'EOF'
# Dockerfile
FROM rust:latest
WORKDIR /app
COPY . .
ENV ROOT_PATH=/app
ENV ENVIRONMENT=production
ENV SERVER_PORT=3030
RUN cargo build --release
EXPOSE 3030
CMD ["./target/release/server"]
EOF
echo
# Demo 6: Configuration file examples
print_info "Demo 6: Configuration file setup"
echo "Your config.toml should contain:"
echo
echo "root_path = \".\" # Default to current directory"
echo "# or"
echo "root_path = \"/app\" # Absolute path for production"
echo
echo "All relative paths in the config will be resolved against this root_path:"
echo " public_dir = \"public\" # becomes /app/public"
echo " logs_dir = \"logs\" # becomes /app/logs"
echo " uploads_dir = \"uploads\" # becomes /app/uploads"
echo
# Demo 7: Validation
print_info "Demo 7: Path validation"
echo "The system validates that ROOT_PATH exists:"
ROOT_PATH="/nonexistent/path" cargo run --release --bin config_tool -- show 2>&1 | grep -E "(Failed to load|Root path)" | head -1 || true
echo
# Clean up
rm -rf "$DEMO_PATH"
print_success "Demo completed!"
echo
print_info "Key takeaways:"
echo " 1. ROOT_PATH sets the base directory for all relative paths"
echo " 2. Use environment variables to override configuration"
echo " 3. Absolute paths are preserved as-is"
echo " 4. Path validation ensures directories exist"
echo " 5. Perfect for containerized deployments"
echo
print_info "For more details, see: docs/ROOT_PATH_CONFIG.md"