A modern, scalable Python FastAPI backend designed for AI coding interview projects. This backend provides a solid foundation with best practices for API development, testing, and documentation.
- FastAPI Framework: Modern, fast web framework for building APIs
- In-Memory Database: JSON-based data storage with CRUD operations
- Comprehensive Testing: Pytest-based test suite with fixtures
- API Documentation: Automatic OpenAPI/Swagger documentation
- CORS Support: Cross-origin resource sharing configuration
- Error Handling: Standardized error responses and validation
- Type Safety: Full type hints and Pydantic validation
- Modular Architecture: Clean separation of concerns
- Python 3.10+
- pip (Python package installer)
-
Clone the repository (if applicable):
git clone <repository-url> cd interview-backend-python
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
uvicorn app.main:app --reload --port 3001uvicorn app.main:app --host 0.0.0.0 --port 3001python -m app.mainGET /- Application information and available endpointsGET /health- Simple health check for load balancersGET /favicon.ico- Favicon handling
GET /api/ping- Quick health checkGET /api/health- Detailed health check with system information
GET /docs- Swagger UI documentationGET /redoc- ReDoc documentationGET /openapi.json- OpenAPI schema
pytestpytest --cov=apppytest tests/test_ping.pypytest -vinterview-backend-python/
βββ app/ # Main application package
β βββ __init__.py
β βββ main.py # FastAPI application entry point
β βββ config/ # Configuration management
β β βββ __init__.py
β β βββ settings.py # Application settings
β βββ api/ # API layer
β β βββ __init__.py
β β βββ dependencies.py # API dependencies
β β βββ routes/ # API route definitions
β β βββ __init__.py
β β βββ ping.py # Health check endpoints
β β βββ root.py # Root-level endpoints
β βββ models/ # Data models
β β βββ __init__.py
β β βββ schemas.py # Pydantic schemas
β βββ services/ # Business logic
β β βββ __init__.py
β β βββ database.py # In-memory database
β βββ utils/ # Utility functions
β βββ __init__.py
β βββ helpers.py # Helper functions
βββ tests/ # Test suite
β βββ __init__.py
β βββ conftest.py # Pytest configuration
β βββ test_ping.py # Ping endpoint tests
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
βββ plan.md # Project planning document
The application uses environment variables for configuration. Create a .env file in the root directory:
# Server Configuration
PORT=3001
HOST=0.0.0.0
DEBUG=true
# Environment
ENVIRONMENT=development
# CORS Settings
CORS_ORIGINS=["*"]The application includes an in-memory database service with the following collections:
- users: User records with authentication data
- sessions: Session management and tokens
- data: General application data
from app.services.database import get_database
db = get_database()
# Create a record
user = db.create("users", {"username": "john", "email": "john@example.com"})
# Get all records
users = db.get_all("users")
# Get by ID
user = db.get_by_id("users", "user_id")
# Update record
updated_user = db.update("users", "user_id", {"is_active": False})
# Delete record
success = db.delete("users", "user_id")- Input Validation: Pydantic models for request/response validation
- CORS Configuration: Configurable cross-origin resource sharing
- Error Handling: Standardized error responses
- Type Safety: Full type hints throughout the codebase
Once the server is running, you can access:
- Swagger UI: http://localhost:3001/docs
- ReDoc: http://localhost:3001/redoc
- OpenAPI Schema: http://localhost:3001/openapi.json
curl http://localhost:3001/api/pingExpected response:
{
"message": "pong",
"timestamp": "2024-01-01T00:00:00Z"
}curl http://localhost:3001/healthExpected response:
{
"status": "ok"
}curl http://localhost:3001/api/healthExpected response:
{
"message": "healthy",
"timestamp": "2024-01-01T00:00:00Z"
}curl http://localhost:3001/Expected response:
{
"app_name": "FastAPI Backend",
"version": "1.0.0",
"description": "AI Coding Interview Backend API",
"environment": "development",
"endpoints": {
"health_check": "/api/ping",
"detailed_health": "/api/health",
"documentation": "/docs",
"redoc": "/redoc"
}
}- Create a new route file in
app/api/routes/(for API endpoints) or add toapp/api/routes/root.py(for root endpoints) - Define your endpoint with proper FastAPI router syntax and documentation
- Include the router in
app/main.pywith appropriate prefix (use/apiprefix for API endpoints) - Add tests in
tests/
- Define Pydantic models in
app/models/schemas.py - Use them in your endpoints for validation
- Add corresponding tests
- Use the database service in
app/services/database.py - Add proper error handling
- Include validation with Pydantic models
Create a Dockerfile:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "3001"]Set production environment variables:
ENVIRONMENT=production
DEBUG=false
PORT=3001- Follow the existing code structure
- Add comprehensive tests for new features
- Update documentation as needed
- Use type hints and docstrings
- Follow PEP 8 style guidelines
This project is for AI coding interview purposes.
For issues or questions:
- Check the API documentation at
/docs - Review the test files for usage examples
- Check the configuration in
app/config/settings.py
Happy Coding! π