Newzy Backend API
FastAPI backend for the Newzy news web application.
Overview
This repository contains the backend infrastructure for the Newzy news platform, built using Python FastAPI with SQLAlchemy ORM and SQLite database.
Features
- RESTful API endpoints
- SQLAlchemy database models
- Pydantic request/response validation
- Docker containerization support
- Unit testing with pytest
- Health and version monitoring endpoints
Installation
Local Development
- Create a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables (optional):
export DATABASE_URL="sqlite:///./data/db/newzy.db"
- Run the application:
uvicorn main:app --reload
The API will be available at http://localhost:8000
Using Docker
- Build and run with Docker Compose:
docker-compose up -d backend
- Access the API at
http://localhost:8000
API Endpoints
Health Check
GET /health
Check if the application is running.
Response:
{
"status": "healthy"
}
GET /api/v1/version
Get application version information.
Response:
{
"version": "0.1.0",
"application": "Newzy"
}
Models
The following models are defined in the database:
- Article: News articles with title, content, author, and category
- Author: Authors who write articles (name, email, bio)
- Category: Content categories for organizing articles (name, slug)
Database Schema
Articles Table
id: Integer (primary key)title: String(200), uniquecontent: Textauthor_id: Integer (foreign key to authors.id)category_id: Integer (foreign key to categories.id)created_at: DateTimepublished: Boolean, default False
Authors Table
id: Integer (primary key)name: String(100), uniqueemail: String(255), uniquebio: Text (nullable)
Categories Table
id: Integer (primary key)name: String(100), uniqueslug: String(100), unique
Testing
Run the test suite:
pytest -v --cov=. --cov-report=html
Coverage report will be generated in htmlcov/ directory.
Running Specific Tests
# Run all tests
pytest -v
# Run specific test file
pytest tests/test_models.py -v
# Run with coverage
pytest -v --cov=. --cov-report=term-missing
Configuration
Environment variables available:
| Variable | Description | Default |
|---|---|---|
APP_NAME |
Application name | "Newzy" |
APP_VERSION |
Application version | "0.1.0" |
DEBUG |
Debug mode | False |
DATABASE_URL |
Database connection string | sqlite:///./data/db/newzy.db |
CORS_ORIGINS |
Allowed CORS origins | ["*"] |
Project Structure
backend/
├── api/ # API routes (v1)
│ └── v1/ # Version 1 API
├── core/ # Core functionality
├── db/ # Database configuration
│ └── session.py # Session management
├── models/ # SQLAlchemy models
│ ├── article.py
│ ├── author.py
│ └── category.py
├── schemas/ # Pydantic schemas
│ ├── article.py
│ ├── author.py
│ └── category.py
├── tests/ # Unit tests
│ ├── conftest.py
│ ├── test_health.py
│ ├── test_version.py
│ └── test_models.py
├── main.py # FastAPI application
├── config.py # Configuration settings
├── requirements.txt # Python dependencies
├── Dockerfile # Container configuration
├── pytest.ini # Pytest configuration
└── README.md # This file
Error Handling
The API returns standardized error responses:
Validation Errors (422)
{
"detail": "Validation error",
"message": "..."
}
Internal Server Errors (500)
{
"detail": "Internal server error"
}
Development Notes
- Database uses SQLite with file-based storage
- Foreign key relationships are enforced
- Pydantic schemas provide request/response validation
- Docker Compose enables easy local development setup
License
See parent directory LICENSE file.