Imported from mtykhenko/cloudflare-crawl-endpoint (
AGENTS.md). Install upstream withnpx skills add mtykhenko/cloudflare-crawl-endpoint. Copyright stays with the author.
AGENTS.md
This file provides guidance to agents when working with code in this repository.
Project Overview
cloudflare-crawl-site is a web application for crawling websites using Cloudflare's Browser Rendering API. It consists of a React frontend and Python FastAPI backend, both containerized with Docker for easy deployment.
Technology Stack
Backend
- Language: Python 3.11
- Framework: FastAPI
- HTTP Client: httpx (async)
- Validation: Pydantic
- Configuration: pydantic-settings
- Server: Uvicorn
Frontend
- Framework: React 18
- Build Tool: Vite
- HTTP Client: Axios
- Markdown Rendering: react-markdown
- Styling: CSS Modules
Infrastructure
- Containerization: Docker
- Orchestration: Docker Compose
- Web Server: Nginx (for frontend)
Project Structure
crawl-site/
├── backend/ # Python FastAPI backend
│ ├── app/
│ │ ├── __init__.py
│ │ ├── main.py # FastAPI application entry point
│ │ ├── models.py # Pydantic models for validation
│ │ ├── cloudflare_client.py # Cloudflare API integration
│ │ ├── config.py # Configuration management
│ │ ├── api/ # API layer (routes and handlers)
│ │ │ ├── __init__.py
│ │ │ ├── routes.py # API route definitions
│ │ │ └── exception_handlers.py # Custom exception handlers
│ │ └── services/ # Business logic layer
│ │ ├── __init__.py
│ │ └── crawl_service.py # Crawl operations service
│ ├── tests/ # Backend tests
│ │ ├── __init__.py
│ │ ├── conftest.py # Shared test fixtures
│ │ ├── test_*.py # Test files
│ │ ├── api/ # API layer tests
│ │ ├── services/ # Service layer tests
│ │ ├── reports/ # Test reports (gitignored)
│ │ │ ├── coverage/ # HTML coverage reports
│ │ │ ├── junit/ # JUnit XML reports
│ │ │ └── coverage.xml # Coverage XML
│ │ ├── .coveragerc # Coverage configuration
│ │ └── README.md # Testing documentation
│ ├── requirements.txt # Python dependencies
│ ├── requirements-dev.txt # Development dependencies
│ ├── pytest.ini # Pytest configuration
│ ├── Dockerfile # Production container definition
│ ├── Dockerfile.test # Test container definition
│ └── .env.example # Environment variables template
├── frontend/ # React frontend
│ ├── src/
│ │ ├── components/ # React components
│ │ │ ├── CrawlForm.jsx # URL/depth input form
│ │ │ ├── CrawlForm.css # Form styles
│ │ │ ├── StatusIndicator.jsx # Crawl progress display
│ │ │ ├── StatusIndicator.css # Status styles
│ │ │ ├── MarkdownViewer.jsx # Results viewer
│ │ │ ├── MarkdownViewer.css # Viewer styles
│ │ │ ├── ErrorDisplay.jsx # Error handling UI
│ │ │ └── ErrorDisplay.css # Error styles
│ │ ├── services/
│ │ │ └── api.js # Backend API client
│ │ ├── App.jsx # Main application component
│ │ ├── App.css # Global styles
│ │ └── main.jsx # React entry point
│ ├── public/ # Static assets
│ ├── index.html # HTML template
│ ├── package.json # Node dependencies
│ ├── vite.config.js # Vite configuration
│ ├── Dockerfile # Frontend container definition
│ ├── nginx.conf # Nginx configuration
│ └── .env.example # Environment variables template
├── .bob/ # Bob AI configuration
├── docker-compose.yml # Multi-container orchestration
├── .gitignore # Git ignore patterns
├── README.md # User documentation
├── ARCHITECTURE.md # Architecture documentation
└── AGENTS.md # This file
Total Files: 40+
Key Components
Backend Components
-
main.py: FastAPI application entry point
- Configures CORS middleware
- Registers API routes from
routes.py - Registers exception handlers from
exception_handlers.py - Sets up logging configuration
-
api/routes.py: API route definitions
GET /api/health: Health check endpointPOST /api/crawl: Initiate crawl job (returns 202 Accepted)GET /api/crawl/{job_id}: Get job status and results- Uses
CrawlServicefor business logic - Comprehensive error handling and logging
-
api/exception_handlers.py: Custom exception handlers
cloudflare_api_error_handler(): Maps Cloudflare errors to HTTP status codesvalidation_exception_handler(): Handles request validation errorsglobal_exception_handler(): Catches unhandled exceptions- Provides consistent error response format
-
services/crawl_service.py: Business logic layer
CrawlServiceclass manages crawl operationsinitiate_crawl(): Initiates new crawl jobsget_crawl_status(): Retrieves job status and results- Separates business logic from API layer
-
cloudflare_client.py: Async client for Cloudflare Browser Rendering API
- Handles API authentication
- Manages crawl initiation and status polling
- Implements error handling and retry logic
- Custom
CloudflareAPIErrorexception
-
models.py: Pydantic models for request/response validation
CrawlRequest: Input validation (URL, depth)CrawlResponse: Crawl initiation responseJobStatusResponse: Status and results structureCrawlResult: Individual page resultHealthResponse: Health check responseErrorResponse: Error response format
-
config.py: Configuration management using pydantic-settings
- Loads environment variables
- Validates required credentials
- Provides configuration properties
Frontend Components
-
CrawlForm.jsx: User input form
- URL validation
- Depth selection (1-100)
- Form submission handling
-
StatusIndicator.jsx: Real-time status display
- Progress bar
- Status badges
- Browser time tracking
-
MarkdownViewer.jsx: Results display
- Collapsible page sections
- Markdown rendering
- Copy to clipboard functionality
-
ErrorDisplay.jsx: Error handling UI
- User-friendly error messages
- Retry functionality
- Error dismissal
-
api.js: Backend communication
- Axios instance configuration
- API method wrappers
- Error transformation
Development Guidelines
Backend Development
-
Adding New Endpoints:
- Define Pydantic models in
models.py - Add route handler in
api/routes.py - Add business logic in
services/if needed - Update API documentation with response models
- Add appropriate error handling
- Define Pydantic models in
-
Adding Business Logic:
- Create service classes in
services/ - Keep services focused on single responsibility
- Use dependency injection for clients
- Add comprehensive logging
- Handle exceptions appropriately
- Create service classes in
-
Modifying Cloudflare Integration:
- Update
cloudflare_client.py - Maintain async/await pattern
- Add comprehensive logging
- Handle API errors gracefully with
CloudflareAPIError
- Update
-
Adding Exception Handlers:
- Add handlers in
api/exception_handlers.py - Register in
main.py - Map to appropriate HTTP status codes
- Provide consistent error response format
- Add handlers in
-
Configuration Changes:
- Update
config.pyfor new settings - Add to
.env.example - Document in
README.md
- Update
Frontend Development
-
Adding New Components:
- Create component in
src/components/ - Create corresponding CSS file (e.g.,
ComponentName.css) - Import and use in
App.jsx - Follow existing patterns (functional components with hooks)
- Create component in
-
API Integration:
- Add methods to
api.js - Use async/await pattern
- Handle errors consistently
- Transform errors to user-friendly messages
- Update state management in components
- Add methods to
-
Styling:
- Use separate CSS files for each component
- Follow existing color scheme and design patterns
- Ensure responsive design
- Test on mobile devices
- Use CSS variables for consistency
Docker Development
-
Backend Container (
Dockerfile):- Multi-stage build for optimization
- Non-root user for security
- Health check configured
- Alpine base for small size
- Production dependencies only
-
Backend Test Container (
Dockerfile.test):- Based on production Dockerfile
- Includes dev dependencies (pytest, coverage, etc.)
- Configured for test execution
- Outputs reports to mounted volumes
-
Frontend Container (
Dockerfile):- Build stage with Node
- Serve stage with Nginx
- Static asset optimization
- Security headers configured
-
Docker Compose (
docker-compose.yml):- Service dependencies defined
- Health checks configured
- Network isolation
- Volume mounts for development
- Test profile for isolated test execution
Common Tasks
Running Locally
# Backend
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Edit .env with credentials
uvicorn app.main:app --reload
# Frontend
cd frontend
npm install
cp .env.example .env
npm run dev
Running with Docker
# Create .env file
cp backend/.env.example .env
# Edit .env with credentials
# Build and run
docker-compose up --build
# Run in background
docker-compose up -d
# View logs
docker-compose logs -f
# Stop
docker-compose down
Testing
# Backend health check
curl http://localhost:8000/api/health
# Initiate crawl
curl -X POST http://localhost:8000/api/crawl \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "depth": 2}'
# Check status
curl http://localhost:8000/api/crawl/{job_id}
API Integration Details
Cloudflare Browser Rendering API
- Base URL:
https://api.cloudflare.com/client/v4/accounts/{account_id}/browser-rendering - Authentication: Bearer token in Authorization header
- Rate Limits: 10 minutes browser time/day (free tier)
- Max Pages: 100,000 per crawl
- Max Runtime: 7 days per job
Request Flow
- User submits URL and depth via frontend
- Frontend calls
POST /api/crawl - Backend initiates Cloudflare crawl job
- Backend returns job_id to frontend
- Frontend polls
GET /api/crawl/{job_id}every 3 seconds - Backend fetches status from Cloudflare
- Results displayed when status is "completed"
Security Considerations
-
API Credentials:
- Never commit
.envfiles - Use environment variables
- Rotate tokens regularly
- Never commit
-
CORS Configuration:
- Restrict to known origins
- Update for production domains
-
Input Validation:
- Pydantic models validate all inputs
- URL scheme validation (http/https only)
- Depth range validation (1-100)
-
Container Security:
- Non-root users
- Minimal base images (Alpine)
- No secrets in images
- Health checks configured
Troubleshooting
Common Issues
-
CORS Errors:
- Check
CORS_ORIGINSin backend.env - Ensure frontend URL matches
- Restart backend after changes
- Check
-
Cloudflare API Errors:
- Verify API token permissions
- Check account ID is correct
- Monitor rate limits
-
Docker Build Failures:
- Clear cache:
docker-compose build --no-cache - Check Dockerfile syntax
- Verify all files exist
- Clear cache:
-
Container Networking:
- Ensure services on same network
- Check port mappings
- Verify health checks pass
Future Enhancements
Potential improvements:
- Add authentication and user management
- Implement job history with database
- Add WebSocket for real-time updates
- Support custom crawl configurations
- Export results to multiple formats
- Scheduled crawls
- Crawl comparison tools
- Unit and integration tests
- CI/CD pipeline
- Monitoring and alerting
Dependencies
Backend Dependencies (requirements.txt)
- fastapi==0.109.0
- uvicorn[standard]==0.27.0
- httpx==0.26.0
- pydantic==2.5.3
- pydantic-settings==2.1.0
- python-dotenv==1.0.0
Backend Dev Dependencies (requirements-dev.txt)
- pytest==7.4.3
- pytest-asyncio==0.21.1
- pytest-cov==4.1.0
- pytest-mock==3.12.0
- httpx==0.26.0
- respx==0.20.2
Frontend Dependencies (package.json)
- react@^18.2.0
- react-dom@^18.2.0
- axios@^1.6.5
- react-markdown@^9.0.1
- vite@^5.0.11
- @vitejs/plugin-react@^4.2.1
Documentation
README.md: User-facing documentation with setup instructionsARCHITECTURE.md: Detailed architecture and design decisionsAGENTS.md: This file - developer guidance for AI agentsbackend/tests/README.md: Comprehensive testing documentation
Notes for AI Agents
When working with this codebase:
- Maintain Consistency: Follow existing patterns and conventions
- Update Documentation: Keep all documentation files in sync
- Test Changes: Verify both locally and in Docker
- Run Tests in Container: Use
docker-compose --profile test run --rm backend-testfor consistent test execution - Security First: Never expose credentials or tokens
- Error Handling: Add comprehensive error handling
- Logging: Use appropriate log levels
- Type Safety: Use Pydantic models and TypeScript where applicable
- Async Patterns: Maintain async/await in backend
- Component Structure: Keep components focused and reusable
- Docker Best Practices: Multi-stage builds, non-root users, health checks
- Test Coverage: Maintain >90% coverage, reports saved to
backend/tests/reports/
Contact & Support
For questions or issues:
- Check README.md troubleshooting section
- Review Cloudflare API documentation
- Check application logs
- Verify environment configuration
Last Updated: 2026-03-13 Version: 1.1.0