FastAPI tutorial for serving Rwanda night-time lights and population data
# Rwanda Night-Time Lights API
A FastAPI tutorial project demonstrating how to build a REST API for serving Rwanda's night-time lights and demographic data.
## Project Structure
```
rwanda-ntl-api/
├── main.py # Main FastAPI application entry point
├── src/
│ ├── __init__.py # Makes src a Python package
│ ├── database.py # Database connection and utilities
│ └── models.py # Pydantic data models for API responses
├── .env # Environment variables (database credentials)
├── .gitignore # Git ignore file
├── requirements.txt # Python dependencies
└── README.md # This file
```
## Module Documentation
### `main.py` - FastAPI Application Entry Point
The main application file that defines all API endpoints and starts the FastAPI server.
**Key Components:**
- **FastAPI App Instance**: Creates the main application with metadata
- **Health Check Endpoints**: Basic endpoints to verify API and database connectivity
- **CRUD Endpoints**: Full Create, Read, Update, Delete operations for each data type
- **Analytics Endpoints**: Complex queries for data analysis and insights
**Selected Functions Defining Endpoints:**
```python
# Health check endpoints
@app.get("/") # Basic API status
@app.get("/health") # Database connectivity check
# Administrative data endpoints
@app.get("/cells") # Get all administrative cells with filtering
@app.get("/cells/{cell_id}") # Get specific cell by ID
# Population data endpoints
@app.get("/population") # Get population data with filtering options
@app.get("/population/{cell_id}") # Get population for specific cell
```
### `src/database.py` - Database Connection Module
Handles PostgreSQL database connections using psycopg2 with connection management utilities.
**Key Functions:**
```python
def get_db_cursor():
"""
Creates a database connection and returns cursor with RealDictCursor.
Returns:
tuple: (cursor …