Skip to content

mildrk/interview-backend-python

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI Backend - AI Coding Interview Project

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.

πŸš€ Features

  • 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

πŸ“‹ Requirements

  • Python 3.10+
  • pip (Python package installer)

πŸ› οΈ Installation

  1. Clone the repository (if applicable):

    git clone <repository-url>
    cd interview-backend-python
  2. Create a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

πŸƒβ€β™‚οΈ Running the Server

Development Mode (with auto-reload)

uvicorn app.main:app --reload --port 3001

Production Mode

uvicorn app.main:app --host 0.0.0.0 --port 3001

Direct Python Execution

python -m app.main

🌐 API Endpoints

Root Endpoints

  • GET / - Application information and available endpoints
  • GET /health - Simple health check for load balancers
  • GET /favicon.ico - Favicon handling

API Endpoints (prefixed with /api)

  • GET /api/ping - Quick health check
  • GET /api/health - Detailed health check with system information

Documentation

  • GET /docs - Swagger UI documentation
  • GET /redoc - ReDoc documentation
  • GET /openapi.json - OpenAPI schema

πŸ§ͺ Testing

Run all tests

pytest

Run tests with coverage

pytest --cov=app

Run specific test file

pytest tests/test_ping.py

Run tests with verbose output

pytest -v

πŸ“ Project Structure

interview-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

πŸ”§ Configuration

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=["*"]

πŸ“Š Database

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

Database Operations

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")

πŸ›‘οΈ Security Features

  • 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

πŸ“ API Documentation

Once the server is running, you can access:

πŸ§ͺ Example API Calls

Quick Health Check

curl http://localhost:3001/api/ping

Expected response:

{
  "message": "pong",
  "timestamp": "2024-01-01T00:00:00Z"
}

Simple Health Check

curl http://localhost:3001/health

Expected response:

{
  "status": "ok"
}

Detailed Health Check

curl http://localhost:3001/api/health

Expected response:

{
  "message": "healthy",
  "timestamp": "2024-01-01T00:00:00Z"
}

Application Info

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"
  }
}

πŸ” Development

Adding New Endpoints

  1. Create a new route file in app/api/routes/ (for API endpoints) or add to app/api/routes/root.py (for root endpoints)
  2. Define your endpoint with proper FastAPI router syntax and documentation
  3. Include the router in app/main.py with appropriate prefix (use /api prefix for API endpoints)
  4. Add tests in tests/

Adding New Models

  1. Define Pydantic models in app/models/schemas.py
  2. Use them in your endpoints for validation
  3. Add corresponding tests

Database Operations

  1. Use the database service in app/services/database.py
  2. Add proper error handling
  3. Include validation with Pydantic models

πŸš€ Deployment

Docker (Optional)

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"]

Environment Variables

Set production environment variables:

ENVIRONMENT=production
DEBUG=false
PORT=3001

🀝 Contributing

  1. Follow the existing code structure
  2. Add comprehensive tests for new features
  3. Update documentation as needed
  4. Use type hints and docstrings
  5. Follow PEP 8 style guidelines

πŸ“„ License

This project is for AI coding interview purposes.

πŸ†˜ Support

For issues or questions:

  1. Check the API documentation at /docs
  2. Review the test files for usage examples
  3. Check the configuration in app/config/settings.py

Happy Coding! πŸŽ‰

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%